创建小书签

发布于 2024-10-13 04:38:19 字数 1002 浏览 3 评论 0原文

(问题已改进)

您好, 我正在尝试制作一个小书签。小书签将被覆盖并在页面的右上角打开。您可以在[此处][1]查看书签类型,我想制作覆盖界面。

这是我的 bookmarklet 的 javascript 代码:

javascript:void((function(){var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('src','http://www.girmiyor.co.cc/bookmarklet.js');document.body.appendChild(e)})())

Bookmarklet 将调用 bookmarklet.js 文件。 bookmarklet.js 的内容:

document.body.innerHTML += "<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'></script>";

document.body.innerHTML += "<div class='result' style='background-color:grey;z-index:1000;position:absolute;right:0;top:0' width='300' height='250'></div>";



var site = location.href;

    $.get("http://www.girmiyor.co.cc/c.php",{ q: site}, function(data){
                        $('.result').html(data);
                });

发送 GET 请求,我可以通过 Firebug 看到它。但什么也没回来。

你能帮助我吗?

(Question improved)

Hello,
I'm trying to make a bookmarklet. Bookmarklet will be overlayed and will open right-top corner of page . You can see bookmarklet types [here][1] i want to make overlay interface.

Here is my javascript code for bookmarklet :

javascript:void((function(){var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('src','http://www.girmiyor.co.cc/bookmarklet.js');document.body.appendChild(e)})())

Bookmarklet will call bookmarklet.js file. Content of bookmarklet.js :

document.body.innerHTML += "<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'></script>";

document.body.innerHTML += "<div class='result' style='background-color:grey;z-index:1000;position:absolute;right:0;top:0' width='300' height='250'></div>";



var site = location.href;

    $.get("http://www.girmiyor.co.cc/c.php",{ q: site}, function(data){
                        $('.result').html(data);
                });

GET request sending i can see it via Firebug. But nothing returned.

Can you help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想挽留 2024-10-20 04:38:19

仔细看看你提到的friendfeed书签;它所做的只是创建并附加一个新的脚本标签,该标签包含具有“真实”函数的 js 文件。
你提到的那篇文章也是这样做的。基本的(非常不言自明的)方法是:

e=document.createElement('script');
e.type='text/javascript';
e.src='http://domain.tld/file.js'; //optionally pass GET params
document.getElementsByTagName('head')[0].appendChild(e); //optionally, you could append to the <body>

如果您仔细查看此方法包含的文件,您可以看到覆盖层是如何构造的。我粘贴了 friendfeed 文件 中的重要片段,覆盖层 div 就位于其中,查找该部分并研究它:

// Create the share dialog in the corner of the window
var container = div();
container.id = "ff__container";
container.style.position = "absolute";
container.style.top = scrollPos().y + "px";
container.style.right = "0";
container.style.width = "auto";
container.style.zIndex = 100000;

所以我建议你创建一个单独的 js 文件来保存构建覆盖层的函数,然后让你的书签加载该文件。

Take a closer look at the friendfeed bookmarklet you mentioned; all it does is create and append a new script tag that holds a js file with the 'real' functions.
The article you mentioned does the same. The basic (pretty selfexplaining) method is:

e=document.createElement('script');
e.type='text/javascript';
e.src='http://domain.tld/file.js'; //optionally pass GET params
document.getElementsByTagName('head')[0].appendChild(e); //optionally, you could append to the <body>

if you then take a closer look at the files that are included by this method you can see how the overlay is constructed. I pasted the important snippet from the friendfeed file, where the overlay div gets positioned, look for that section and study it:

// Create the share dialog in the corner of the window
var container = div();
container.id = "ff__container";
container.style.position = "absolute";
container.style.top = scrollPos().y + "px";
container.style.right = "0";
container.style.width = "auto";
container.style.zIndex = 100000;

so i would suggest that you make a separate js file that holds the functions for constructing the overlay and you let your bookmarklet load that file.

我的鱼塘能养鲲 2024-10-20 04:38:19

当我直接添加此代码到 Firefox 的书签,然后单击“返回页面”和[object XMLHttpRequest]

这是书签的常见问题。要解决此问题,请将 void(0); 添加到小书签代码的末尾。这样,小书签就不会返回浏览器中显示的任何值。

when i add this code directly firefox's bookmarks and then click return a page and [object XMLHttpRequest]

This is a common problem with bookmarklets. To fix that add a void(0); to the end of the bookmarklet code. That way, the bookmarklet won't return any value which would be displayed in the browser.

素食主义者 2024-10-20 04:38:19

当前版本的 bookmarklet.js 已损坏(当我写这个答案时),因为 var data = 后面缺少引号。

除此之外,我对您的问题有几个未经测试的理论

  1. 您正在替换您自己的脚本运行的bodyinnerHTML。当脚本完成时,jQuery 甚至还没有加载。您可能必须暂停脚本的执行并等待 jQuery 加载。尝试将 $.get() 放入在 window.setTimeout() 之后调用的函数中,最好检查 $< 是否存在/code> (使用 typeof($) != 'undefined'),然后等待更多时间或运行您的 $.get()
  2. 您通过 HTTPS 加载 jQuery,因此浏览器策略可能会阻止 $.get 从 HTTP 获取内容
  3. 设置 jQuery

The current version of your bookmarklet.js is broken (as I'm writing this answer) because there's a missing quote after var data =.

Appart from that, I have several untested theories regarding your problem:

  1. You're replacing the body's innerHTML where your own script is running. And by the time the script finishes, jQuery isn't even loaded yet. You probably have to suspend the script's execution and wait for jQuery to load. Try something like putting your $.get() in a function that's called after a window.setTimeout(), and preferably that checks for the existence of $ (with typeof($) != 'undefined') and either waits more or runs your $.get().
  2. You're loading jQuery through HTTPS, so browser policies may prevent $.get from getting stuff from HTTP
  3. Setting the jQuery <script> reference's type attribute to text/javascript shouldn't be your problem, but it definitely wouldn't hurt doing.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文