创建小书签
(问题已改进)
您好, 我正在尝试制作一个小书签。小书签将被覆盖并在页面的右上角打开。您可以在[此处][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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仔细看看你提到的friendfeed书签;它所做的只是创建并附加一个新的脚本标签,该标签包含具有“真实”函数的 js 文件。
你提到的那篇文章也是这样做的。基本的(非常不言自明的)方法是:
如果您仔细查看此方法包含的文件,您可以看到覆盖层是如何构造的。我粘贴了 friendfeed 文件 中的重要片段,覆盖层 div 就位于其中,查找该部分并研究它:
所以我建议你创建一个单独的 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:
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:
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.
这是书签的常见问题。要解决此问题,请将
void(0);
添加到小书签代码的末尾。这样,小书签就不会返回浏览器中显示的任何值。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.当前版本的 bookmarklet.js 已损坏(当我写这个答案时),因为
var data =
后面缺少引号。除此之外,我对您的问题有几个未经测试的理论:
body
的innerHTML
。当脚本完成时,jQuery 甚至还没有加载。您可能必须暂停脚本的执行并等待 jQuery 加载。尝试将$.get()
放入在window.setTimeout()
之后调用的函数中,最好检查$< 是否存在/code> (使用
typeof($) != 'undefined'
),然后等待更多时间或运行您的$.get()
。$.get
从 HTTP 获取内容引用的
type
> 属性text/javascript
不应该是你的问题,但它绝对不会有什么坏处。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:
body
'sinnerHTML
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 awindow.setTimeout()
, and preferably that checks for the existence of$
(withtypeof($) != 'undefined'
) and either waits more or runs your$.get()
.$.get
from getting stuff from HTTP<script>
reference'stype
attribute totext/javascript
shouldn't be your problem, but it definitely wouldn't hurt doing.