代码在浏览器控制台中工作,但不在 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 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)
Sending Multiple attachments with Google Script from Google Drive(使用 Google 脚本从 Google Drive 发送多个附件)
Distributing Google Apps Scripts for Sheets in your company network(在您的公司网络中分发适用于表格的 Google Apps 脚本)
Upload file to my google drive from anyone using javascript(使用 javascript 将文件从任何人上传到我的谷歌驱动器)
quot;Shared Drivequot; support in Google Apps Script(“共享驱动器Google Apps 脚本中的支持)
Angular 2+ HTTP POST and GDrive API. Resumable file upload with name(Angular 2+ HTTP POST 和 GDrive API.带名称的可恢复文件上传)