Tampermonkey用户脚本可防止页面加载 [英] Tampermonkey userscript prevents pages from loading

查看:248
本文介绍了Tampermonkey用户脚本可防止页面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用用户脚本在Stackexchange聊天窗口中解释APL编程语言.这是我想出的代码:

 //== UserScript ==//@name APL聊天//@版本2//@grant GM_xmlhttpRequest//@grant GM_listValues//@match https://chat.stackexchange.com/*//@ run-at document-start//@require https://cdn.jsdelivr.net/npm/apl@0.1.15/lib/apl.min.js//==/UserScript ==//感谢@cvzi使其正常工作!var MutationObserver = window.MutationObserver ||window.WebKitMutationObserver;var观察者=新的MutationObserver(函数(变异,观察者){//发生突变时触发console.log(变异,观察者);让代码= document.getElementsByTagName(代码");为(让代码的元素){if(elem.innerText&!(在elem.dataset中为解释")&& elem.innerText [0] =='⋄'){elem.dataset.interpreted = true;//参见https://developer.mozilla.org/zh-CN/docs/Learn/HTML/Howto/Use_data_attributes#JavaScript_access让结果=''让颜色='红色'//捕获apl错误并以橙色显示尝试 {结果= apl(elem.innerText).toString()} catch(e){结果= e.toString()颜色='橙色​​'}让tmp = document.createElement("div");tmp.innerHTML ="pre style = \" color:"+ color +" \> + result.replace(" \ n," br)+"/pre";让parent = elem.parentElement;parent.appendChild(tmp.firstChild);//console.log(result);}}});window.addEventListener('DOMContentLoaded',(event)=> {alert = function(){};//防止⎕←和⍞←触发警报window.alert = function(){};Observer.observe(document,{子树:true,ChildList:是的,属性:true});}); 

当将代码块写入其中带有字符的主体时,应将其执行并以红色显示在代码下方.

apl()函数采用单个字符串,并以APL语言进行解释,并使用)在文件顶部包含一些咖啡脚本助手功能,尤其是一些 Array.声明了prototype.* 方法,该方法也将在页面中可用.这些可能会中断页面​​,请参阅此问题以获取更多信息:为什么要扩展原生对象是不好的做法?

I am currently making a userscript to interpret the APL programming language in a Stackexchange chat window. This is the code I have come up with:

// ==UserScript==
// @name     APL chat
// @version  2
// @grant    GM_xmlhttpRequest
// @grant    GM_listValues
// @match    https://chat.stackexchange.com/*
// @run-at   document-start
// @require  https://cdn.jsdelivr.net/npm/apl@0.1.15/lib/apl.min.js
// ==/UserScript==

// thanks to @cvzi for making it work correctly!

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;

var observer = new MutationObserver(function(mutations, observer) {
    // fired when a mutation occurs
    console.log(mutations, observer);
    let codes = document.getElementsByTagName("code");
        for (let elem of codes) {
            if(elem.innerText && !('interpreted' in elem.dataset) && elem.innerText[0] == '⋄') {
                elem.dataset.interpreted = true;  // see https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes#JavaScript_access
                let result = ''
                let color = 'red'
                // Catch apl error and show it in orange color
                try {
                    result = apl(elem.innerText).toString()
                } catch(e) {
                    result = e.toString()
                    color = 'orange'
                }
                let tmp = document.createElement("div");
                tmp.innerHTML = "<pre style=\"color:"+color+"\">"+result.replace("\n","<br>")+"</pre>";
                let parent = elem.parentElement;
                parent.appendChild(tmp.firstChild);
                //console.log(result);
            }
        }
});

window.addEventListener('DOMContentLoaded', (event) => {
    alert = function() {}; // Prevents ⎕← and ⍞← from trggering alerts
    window.alert = function(){};
      observer.observe(document, {
      subtree: true,
    ChildList:true,
      attributes: true
    });

});

When a code block is written to the body with a character in it, it should be executed and displayed in red, underneath the code.

The apl() function takes a single string, and interprets in in the APL language, and it is imported from the @require statement, using ngn's javascript APL interpreter. After ngn's confirmation, I can say that it does not interfere with any globals.

It works perfectly on firefox(Greasemonkey), but on Chrome's Tampermonkey, any chat.stackexchange.com page I visit never stops loading. It gets stuck at this point:

And these are the console errors:

Uncaught TypeError: o.substr is not a function
    at HTMLLIElement.<anonymous> (master-chat-with-millinery.js?v=93a5b9100f35:1)
    at Function.each (jquery.min.js:2)
    at n.fn.init.each (jquery.min.js:2)
    at o (master-chat-with-millinery.js?v=93a5b9100f35:1)
    at Sidebar (master-chat-with-millinery.js?v=93a5b9100f35:1)
    at Vt (master-chat-with-millinery.js?v=93a5b9100f35:3)
    at StartChat (master-chat-with-millinery.js?v=93a5b9100f35:3)
    at HTMLDocument.<anonymous> (the-nineteenth-byte:182)
    at i (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
master-chat-with-millinery.js?v=93a5b9100f35:7 Uncaught TypeError: Cannot read property 'resolve' of undefined
    at Object.<anonymous> (master-chat-with-millinery.js?v=93a5b9100f35:7)
    at i (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at y (jquery.min.js:4)
    at XMLHttpRequest.c (jquery.min.js:4)
DevTools failed to load SourceMap: Could not load content for chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/sm/66503b0e4799e2375dd4e11269326fc0fcb9a65474ff19f45c7f956fd381cea8.map: HTTP error: status code 404, net::ERR_UNKNOWN_URL_SCHEME

I have my suspicions on the MutationObserver and the @grant, but I'm not sure what exactly is causing this issue. It could be something else.

What is the correct fix for this problem?

解决方案

You set @run-at document-start in your userscript, this makes the script run before the page loaded, if you remove it, the script is injected after DOMContentLoaded was fired.

If that does not help, you could try to use the original source from gitlab @require https://gitlab.com/n9n/apl/-/raw/master/apl.js you would need to change the apl call to result = apl.fmt(apl(elem.innerText)).toString()

I assume the conflict happens because the apl library (https://cdn.jsdelivr.net/npm/apl@0.1.15/lib/apl.js) has some coffee script helper functions included at the top of the file, notably there are some Array.prototype.* methods declared which will also be available in the page. These could break the page, see this question for more information: Why is extending native objects a bad practice?

这篇关于Tampermonkey用户脚本可防止页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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