我正在尝试在
使用脚本
我想要什么
我已经检查了 这个 但它并没有解决我的问题.
有什么帮助吗?提前致谢.
大部分页面都是动态加载的(AJAX 驱动),这意味着您的脚本通常会在您感兴趣的节点之前完成运行,出现在页面中/页面上.
您必须使用 AJAX 感知技术,例如 waitForKeyElements
或 MutationObserver
.
这里是一个完整的 Tampermonkey 脚本,它说明了这个过程:
//==UserScript==//@name _Lichess.org,荣耀选择用户//@match *://lichess.org/*//@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//==/用户脚本==//- 需要@grant 指令来恢复正确的沙箱.waitForKeyElements ("a[href*='WaisKamal']", spiffifyLink);函数 spiffifyLink (jNode) {var oldHtml = jNode.html ();var newHtml = '<span class="title" data-title="GM" title="Grandmaster">GM</span>' + oldHtml;jNode.html (newHtml);}
<小时>
有关详细信息,请参阅 其他答案关于选择和使用 waitForKeyElements
和/与 jQuery 选择器.
I am trying to run the following block of code on https://lichess.org/uZIjh0SXxnt5.
var x = document.getElementsByTagName("a");
for(var i = 0; i < x.length; i++) {
if(x[i].href.includes("WaisKamal") && x[i].classList.contains("user_link")) {
x[i].innerHTML = '<span class="title" data-title="GM" title="Grandmaster">GM</span> ' + x[i].innerHTML;
}
if(x[i].href.includes("WaisKamal") && x[i].classList.contains("text")) {
x[i].innerHTML = '<span class="title" data-title="GM" title="Grandmaster">GM</span> ' + x[i].innerHTML;
console.log(x[i]);
}
}
I am using tampermonkey to automate the process. When the page loads, the first if statement runs correctly, but not the second one. However, when I run the second one from the browser console, it works fine.
Here is what the script does in more detail (I want to add those orange "GM"s):
Without the script
With the script
What I want
I have checked this but it didn't solve my problem.
Any help? Thanks in advance.
Most of that page is loaded dynamically (AJAX-driven), which means that your script will normally finish running long before the nodes, that you are interested in, appear in/on the page.
You must use AJAX-aware techniques such as waitForKeyElements
or MutationObserver
.
Here's a complete Tampermonkey script that illustrates the process:
// ==UserScript==
// @name _Lichess.org, Glorify select users
// @match *://lichess.org/*
// @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 ("a[href*='WaisKamal']", spiffifyLink);
function spiffifyLink (jNode) {
var oldHtml = jNode.html ();
var newHtml = '<span class="title" data-title="GM" title="Grandmaster">GM</span> ' + oldHtml;
jNode.html (newHtml);
}
See this other answer for more information about choosing and using waitForKeyElements
and/with jQuery selectors.
这篇关于代码在浏览器控制台中工作,但不在 Tampermonkey 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!