Window.AddeventListener不在Chrome上工作
我有以下我试图在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论