Window.AddeventListener不在Chrome上工作

发布于 2025-01-24 17:47:00 字数 1179 浏览 2 评论 0原文

我有以下我试图在Chrome和Firefox上运行的用户标记。我的结果不一致。脚本是匹配的(警报火灾),但是在Chrome上,AddeventListener似乎无效。在Firefox上,它运行了,尽管我尚未弄清楚修改后的.JS的插入仍然存在某种问题。 FWIW,我的最终目标是将端口标签功能添加到以太网开关中,如果我可以使其正常工作,那将很酷。此脚本只是为了查看我是否可以修改开关作为其Web GUI的一部分运行的.js文件。

// ==UserScript==
// @name        Test
// @match       http://192.168.3.21/System.html
// @description TEST
// @version     1
// @grant       GM_xmlhttpRequest
// @run-at      document-start
// ==/UserScript==

function addScript(text) {
    //text = text.replace(/Normal/g, "Abnormal");
    alert(text);
    var newScript = document.createElement('script');
    newScript.type = "text/javascript";
    newScript.textContent = text;
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(newScript);
}

window.addEventListener('beforescriptexecute', function(e) {
    var src = e.target.src;
    if (src.search(/system_data\.js/) != -1) {
        e.preventDefault();
        e.stopPropagation();
        GM_xmlhttpRequest({
            method: "GET",
            url: e.target.src,
            onload: function(response) {
                addScript(response.responseText);
            }

        });
    }
});

I have the following userscript which I am trying to run on Chrome and on Firefox. I am getting inconsistent results. The script is matched (the alert fires) but on Chrome the addEventListener does not appear to work. On Firefox, it runs although there is still a problem of some kind with the insertion of the modified .js which I have yet to figure out. FWIW, my ultimate objective is to add port labeling functionality to an Ethernet switch which would be cool if I could get it to work. This script is just meant to see if I can modify the .js file that the switch runs as part of its web GUI.

// ==UserScript==
// @name        Test
// @match       http://192.168.3.21/System.html
// @description TEST
// @version     1
// @grant       GM_xmlhttpRequest
// @run-at      document-start
// ==/UserScript==

function addScript(text) {
    //text = text.replace(/Normal/g, "Abnormal");
    alert(text);
    var newScript = document.createElement('script');
    newScript.type = "text/javascript";
    newScript.textContent = text;
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(newScript);
}

window.addEventListener('beforescriptexecute', function(e) {
    var src = e.target.src;
    if (src.search(/system_data\.js/) != -1) {
        e.preventDefault();
        e.stopPropagation();
        GM_xmlhttpRequest({
            method: "GET",
            url: e.target.src,
            onload: function(response) {
                addScript(response.responseText);
            }

        });
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文