Force Tampermonkey to run/execute script late

自古美人都是妖i 提交于 2019-12-07 10:58:48

问题


How do I force Tampermonkey to run/execute a script late after every document loaded by AJAX?

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:

  1. Onload event.
  2. 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.
  3. I tried console to execute a function (but inaccessible by console).

So what do I do?


回答1:


The content you want is being loaded by AJAX, so you need to use AJAX-compensation techniques in your script.

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


来源:https://stackoverflow.com/questions/17995540/force-tampermonkey-to-run-execute-script-late

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!