强制Tampermonkey延迟运行/执行脚本 [英] Force Tampermonkey to run/execute script late
问题描述
在AJAX加载每个文档之后,如何强制Tampermonkey延迟运行/执行脚本?
How do I force Tampermonkey to run/execute a script late after every document loaded by AJAX?
我希望访问脚本中的那些元素并进行更改.但是,即使我在设置页面中将 @ run-at
设置为 document-end
,它也会在未完全加载文档的情况下执行.而且,它发生在这个特定的网站上!
I wish to access those elements in my script and change them. But, even though I set @run-at
to document-end
in the setting page, it executes while the document wasn't loaded fully.
And, it happens at this specific website !!
我尝试了这些方法,但没有成功:
I tried these ways but was unsuccessful:
- 加载事件.
- 我尝试了
while
语句来检查是否所有文档都已加载,然后继续执行我的脚本,但该脚本崩溃并陷入无限循环. - 我尝试通过控制台执行功能(但无法通过控制台访问).
- Onload event.
- I tried
while
statement to check if all documents were loaded and then continue executing my script but it crashed and fell into infinite loop. - I tried console to execute a function (but inaccessible by console).
那我该怎么办?
推荐答案
The content you want is being loaded by AJAX, so you need to use AJAX-compensation techniques in your script.
最简单的方法是使用 waitForKeyElements().像这样:
The easiest way to do that is to use waitForKeyElements(). Something like:
// ==UserScript==
// @name _YOUR_SCRIPT_NAME
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
waitForKeyElements (
"jQUERY SELECTOR TO THE NODE(S) YOU WANT",
changeFontColor
);
function changeFontColor (jNode) {
jNode.css ("color", "red");
}
这篇关于强制Tampermonkey延迟运行/执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!