我對GreaseMonkey完全陌生,但我正在嘗試製作一個小腳本。
// ==UserScript==
// @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// ==/UserScript==
(function() {
$ = unsafeWindow.jQuery;
alert($); // this gives 'undefined'
}());
爲什麼警報提示undefined
以及如何解決該問題?
UPDATE
我嘗試這樣做:
(function(){
//boilerplate greasemonkey to wait until jQuery is defined...
function GM_wait()
{
alert('ok');
if(typeof unsafeWindow.jQuery == 'undefined')
window.setTimeout(GM_wait,100);
else
unsafeWindow.jQuery(function() { letsJQuery(unsafeWindow.jQuery); });
}
GM_wait();
function letsJQuery($)
{
alert($);
}
})();
但是這給了我OK的警報的無限循環。好像jQuery根本沒有加載。
嘗試這些相關的問題: http://stackoverflow.com/questions/2077714/greasemonkey-require-jquery-not-working-component-not-available http://stackoverflow.com/questions/859024/how-can-i-use-jquery-in -greasemonkey – Kip 2010-03-24 13:14:51