为什么这个Greasemonkey脚本不能与这个jQuery插件一起工作?

Why doesn't this Greasemonkey script work with this jQuery plugin?

本文关键字:插件 一起 jQuery 工作 不能 Greasemonkey 脚本 为什么      更新时间:2023-09-26

我在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.highlightjQuery.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插件之前阅读相关文档。

,

在样式表中为突出显示类创建一个条目。

。高亮{background-color: yellow}

在Greasemonkey中,等价的是GM_addStyle('.highlight { background-color: yellow }');

,

要突出显示所有li元素中出现的" bla "(不区分大小写),请使用以下代码:

$(李).highlight (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");