2014-03-25 118 views
0

我在Safari中使用NinjaKit(與Greasemonkey相同)。這些代碼都是這樣爲什麼Greasemonkey腳本不能使用這個jQuery插件?

// ==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"]); 
}); 

當我訪問this page,該alert可以顯示良好,但.highlight功能取決於jQuery.highlightjQuery不起作用。它說:

TypeError: 'undefined' is not a function (evaluating 'c.toUpperCase()') 

而我覺得很難調試此..有沒有人有想法呢?

回答

1

我相信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的入手。

+0

我只針對Safari的NinjaKit,我看到NinjaKit列出的表格,但可能只是Chrome NinjaKit。 http://ss-o.net/safari/extension/NinjaKit.safariextz –

0

在使用jQuery插件之前,您需要閱讀相關docs

首先,

創建您的樣式表的亮點類條目。

.highlight {背景色:黃}

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

其次,

爲了突出「喇嘛」(不區分大小寫)的所有occurrances在所有li元素,使用下面的代碼:

$( '禮')亮點(」 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"); 
+0

它不起作用。該功能仍未定義 –

+0

@Salvatore Di Fazio目前它適用於我。 (在我的控制檯中沒有錯誤。)你得到了什麼樣的完整錯誤信息以及你使用的瀏覽器(包括版本)是什麼? – zanetu

+0

@Salvatore Di Fazio另一個問題:你是在http://dailymed.nlm.nih.gov或其他網站上測試我的代碼嗎? – zanetu

相關問題