代码在浏览器控制台中工作,但不在 Tampermonkey

Code working in browser console but not in tampermonkey(代码在浏览器控制台中工作,但不在 Tampermonkey 中)
本文介绍了代码在浏览器控制台中工作,但不在 Tampermonkey 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在

使用脚本

我想要什么

我已经检查了 这个 但它并没有解决我的问题.

有什么帮助吗?提前致谢.

解决方案

大部分页面都是动态加载的(AJAX 驱动),这意味着您的脚本通常会在您感兴趣的节点之前完成运行,出现在页面中/页面上.

您必须使用 AJAX 感知技术,例如 waitForKeyElementsMutationObserver.

这里是一个完整的 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 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)