問題描述
我正在嘗試在
使用腳本
我想要什么
我已經(jīng)檢查了 這個(gè) 但它并沒有解決我的問題.
有什么幫助嗎?提前致謝.
大部分頁面都是動(dòng)態(tài)加載的(AJAX 驅(qū)動(dòng)),這意味著您的腳本通常會(huì)在您感興趣的節(jié)點(diǎn)之前完成運(yùn)行,出現(xiàn)在頁面中/頁面上.
您必須使用 AJAX 感知技術(shù),例如 waitForKeyElements
或 MutationObserver
.
這里是一個(gè)完整的 Tampermonkey 腳本,它說明了這個(gè)過程:
//==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 指令來恢復(fù)正確的沙箱.waitForKeyElements ("a[href*='WaisKamal']", spiffifyLink);函數(shù) spiffifyLink (jNode) {var oldHtml = jNode.html ();var newHtml = '<span class="title" data-title="GM" title="Grandmaster">GM</span>' + oldHtml;jNode.html (newHtml);}
<小時(shí)>
有關(guān)詳細(xì)信息,請(qǐng)參閱 其他答案關(guān)于選擇和使用 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.
這篇關(guān)于代碼在瀏覽器控制臺(tái)中工作,但不在 Tampermonkey 中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!