gpt4 book ai didi

javascript - 将 Firefox 30 之前的 Greasemonkey 脚本迁移到 GM4+ 时,如何替换 unsafeWindow?

转载 作者:行者123 更新时间:2023-11-30 14:37:23 28 4
gpt4 key购买 nike

我试图在一个 Greasemonkey 脚本中获取对我网页上存在的 jQuery 版本的引用,该脚本在 Firefox 30 之前一直有效。在我的定义下方的评论中,我可以找到另外两个引用,但我只是得到 ReferenceError: $ is not definedReferenceError: 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 从来都不是一个好主意,而且很少奏效。另请参阅 Error: Permission denied to access property 'handler' .

处理问题代码的聪明做法是使用@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() );
}

优点:

  • 简短、明了。
  • 不容易受到更改目标页面上的 javascript 的副作用的影响。特别是,如果目标页面更改了 jQuery 版本。
  • 使用快速的本地版本的 jQuery 和 waitForKeyElements。它们存储在本地磁盘上,通常缓存在内存中。没有服务器获取也没有延迟。

注意:如果session是目标页面的全局变量,您可能需要像unsafeWindow.session.name一样访问它。请参阅:How to access `window` (Target page) objects when @grant values are set? .




您声明要使用页面的 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() );
}

关于javascript - 将 Firefox 30 之前的 Greasemonkey 脚本迁移到 GM4+ 时,如何替换 unsafeWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50222854/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com