代码在浏览器控制台中有效,但在tampermonkey中无效 [英] Code working in browser console but not in tampermonkey

查看:582
本文介绍了代码在浏览器控制台中有效,但在tampermonkey中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在



使用脚本





我想要的





我已检查这个,但是并不能解决我的问题。



有帮助吗?

解决方案

该页面的大多数内容都是动态加载的(由AJAX驱动),这意味着您的脚本通常会完成



您必须使用支持AJAX的技术,例如 waitForKeyElements MutationObserver



以下是完整的Tampermonkey脚本

  // == UserScript == 
// @name _Lichess.org,Glorify选择用户
// @match *://lichess.org/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min。 js
// @要求https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// @grant GM.getValue
// == / UserScript ==
//-需要@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);
}






请参见此其他答案,以获取有关选择和使用<$ c $的更多信息c> 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中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆