![](/img/trans.png)
[英]Binding to an event of the unsafeWindow in Firefox 30 with Greasemonkey 2.0
[英]How do I replace unsafeWindow when migrating a pre-Firefox 30 Greasemonkey script to GM4+?
我正在嘗試使用Greasemonkey腳本獲取到我網頁上存在的jQuery版本的引用,該腳本一直運行到Firefox30。在我的定義下面的注釋中,可以找到另外兩個引用,但是我得到的是ReferenceError: $ is not defined
或ReferenceError: jQuery is not defined
當我嘗試訪問窗口對象上的jQuery時ReferenceError: jQuery is not defined
jQuery。
var $ = unsafeWindow.jQuery;
//var jQuery = window.jQuery; // From https://stackoverflow.com/questions/24802606/binding-to-an-event-of-the-unsafewindow-in-firefox-30-with-greasemonkey-2-0
//var jQuery = $ || window.wrappedJSObject.$; // https://github.com/greasemonkey/greasemonkey/issues/2700#issuecomment-345538182
function addAccountNameToTitle(jNode) {
$('title').text(session.name + " | " + $('title').text());
}
waitForKeyElements (".page-breadcrumb", addAccountNameToTitle, false);
/*--- waitForKeyElements(): A handy, utility function that
does what it says.
*/
function waitForKeyElements (
selectorTxt, /* Required: The jQuery selector string that
specifies the desired element(s).
*/
actionFunction, /* Required: The code to run when elements are
found. It is passed a jNode to the matched
element.
*/
bWaitOnce, /* Optional: If false, will continue to scan for
new elements even after the first match is
found.
*/
iframeSelector /* Optional: If set, identifies the iframe to
search.
*/
)
{
var targetNodes, btargetsFound;
if (typeof iframeSelector == "undefined")
targetNodes = $(selectorTxt);
else
targetNodes = $(iframeSelector).contents ()
.find (selectorTxt);
if (targetNodes && targetNodes.length > 0) {
/*--- Found target node(s). Go through each and act if they
are new.
*/
targetNodes.each ( function () {
var jThis = $(this);
var alreadyFound = jThis.data ('alreadyFound') || false;
if (!alreadyFound) {
//--- Call the payload function.
actionFunction (jThis);
jThis.data ('alreadyFound', true);
}
} );
btargetsFound = true;
}
else {
btargetsFound = false;
}
//--- Get the timer-control variable for this selector.
var controlObj = waitForKeyElements.controlObj || {};
var controlKey = selectorTxt.replace (/[^\w]/g, "_");
var timeControl = controlObj [controlKey];
//--- Now set or clear the timer as appropriate.
if (btargetsFound && bWaitOnce && timeControl) {
//--- The only condition where we need to clear the timer.
clearInterval (timeControl);
delete controlObj [controlKey]
}
else {
//--- Set a timer, if needed.
if ( ! timeControl) {
timeControl = setInterval ( function () {
waitForKeyElements ( selectorTxt,
actionFunction,
bWaitOnce,
iframeSelector
);
},
500
);
controlObj [controlKey] = timeControl;
}
}
waitForKeyElements.controlObj = controlObj;
}
我正在使用FF 59.0.2和Greasemonkey 4.3
unsafeWindow.jQuery
從來不是一個好主意,並且很少起作用。 另請參閱錯誤:拒絕訪問屬性“處理程序”的權限 。
與問題代碼相關的聰明的事情是使用@require
,如下所示:
// ==UserScript==
// @name _Changing the title text on some page.
// @match *://YOUR_SERVER.COM/YOUR_PATH/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// @grant GM.getValue
// ==/UserScript==
//- The @grant directives are needed to restore the proper sandbox.
waitForKeyElements (".page-breadcrumb", addAccountNameToTitle);
function addAccountNameToTitle (jNode) {
$('title').text (session.name + " | " + $('title').text() );
}
好處:
注意:如果session
是目標頁面的全局變量,則可能需要像unsafeWindow.session.name
一樣訪問它。 請參閱: 設置@grant值后,如何訪問“窗口”(目標頁面)對象? 。
您聲明要使用頁面的jQuery實例或版本。 很少有理由這樣做。 而且,此問題中顯示的代碼肯定不會從中受益。
但是, 如果您的用戶腳本不使用任何GM函數 ,則可以通過@grant none
模式來實現,例如:
// ==UserScript==
// @name _Changing the title text on some page.
// @match *://YOUR_SERVER.COM/YOUR_PATH/*
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant none
// ==/UserScript==
waitForKeyElements (".page-breadcrumb", addAccountNameToTitle);
function addAccountNameToTitle (jNode) {
$('title').text (session.name + " | " + $('title').text() );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.