![](/img/trans.png)
[英]Why doesn't jQuery().remove() and unbind() work in GreaseMonkey?
[英]Why doesn't this Greasemonkey script work with this jQuery plugin?
我在Safari中使用NinjaKit(与Greasemonkey相同)。 代码是这样的
// ==UserScript==
// @name demo
// @namespace http://dailymed.nlm.nih.gov/
// @include http://dailymed.nlm.nih.gov/dailymed/*
// @require http://code.jquery.com/jquery-1.11.0.min.js
// @require http://johannburkard.de/resources/Johann/jquery.highlight-4.closure.js
// ==/UserScript==
$(document).ready(function () {
document.title = 'Hello!' + document.title;
alert("ZaiJian");
$("body p").highlight(["a"]);
});
当我访问此页面时 , alert
可以很好地显示,但是依赖于jQuery.highlight
和jQuery
的.highlight
函数不起作用。 它说:
TypeError: 'undefined' is not a function (evaluating 'c.toUpperCase()')
而且我发现调试起来非常困难。有人对此有想法吗?
我相信NinjaKit目前不执行@require。 这是我制作的示例,可在Firefox / GreaseMonkey中使用,而不能在Safari / Ninjakit中使用:
// ==UserScript==
// @name DEBUG
// @include http://localhost/Library.html
// @require file:///Users/#######/Sites/hello_world.js
// @require http://localhost/~#######/hello_world.js // EITHER WAY
// ==/UserScript==
alert('activated');
hello_world();
# hello_world.js
function hello_world(){
alert('Hello World!');
}
作为“远程”地址或本地文件,它在GreaseMonkey中都可以正常工作,而在Safari中则失败。 根据我的经验,目前很难获得NinjaKit的来龙去脉。
使用jQuery插件之前,您需要阅读相关文档 。
第一,
在样式表中为突出显示类创建一个条目。
.highlight {background-color:yellow}
在Greasemonkey中,等效项是GM_addStyle('.highlight { background-color: yellow }');
。
第二,
要突出显示所有li元素中所有“ bla”(不区分大小写)的出现,请使用以下代码:
$( '礼')亮点(以 'bla');
您应该省略括号,即$("body p").highlight("a");
。
第三,我认为您不需要$(document).ready()
,因为默认情况下,Greasemonkey脚本是在DOMContentLoaded事件时执行的。
放在一起:
// ==UserScript==
// @name demo
// @namespace http://dailymed.nlm.nih.gov/
// @include http://dailymed.nlm.nih.gov/dailymed/*
// @require http://code.jquery.com/jquery-1.11.0.min.js
// @require http://johannburkard.de/resources/Johann/jquery.highlight-4.closure.js
// @grant GM_addStyle
// ==/UserScript==
GM_addStyle('.highlight { background-color: yellow }');
document.title = 'Hello!' + document.title;
$("body p").highlight("a");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.