0
我的目标是用本地拥有的自制文件替换网页中的原始JavaScript文件。我找到了一个有趣的脚本here,我用它来适应我的目标。使用Greasemonkey替换/交换html文件中的JavaScript文件
// ==UserScript==
// @name JS_tamper
// @namespace namespace
// @include http://192.168.1.1/*
// @version 1
// @grant none
// @run-at document-start
// ==/UserScript==
var newJSFile = "http://localhost:8080/tamper.js"; //The JS file to load in replacment of old JS file
var oldJSFile = "http://192.168.1.1/menuBcm.js"; //The old JS file as it is named in inspect element (make sure its spelled EXACTLY the same)
var pattern = new RegExp(oldJSFile, "i"); //Create the RegExp pattern with the /i switch to make it case-insensitive
function injectScript(originalPage) { //Function injectScript replaces the file
console.log('Replace stage 2: Replace text matching', oldJSFile, 'with', newJSFile);
var moddedPage = originalPage.replace(pattern, newJSFile); //Modify the HTML code that we got, replacing the old JS file with the new one
document.open();
console.log('Replace stage 3: Write new HTML to page...');
document.write(moddedPage); //Write to the page the new HTML code
document.close();
}
setTimeout(function() { //Wait a bit for the page's HTML to load...
console.log('Replace stage 1: target HTML');
injectScript(document.documentElement.outerHTML); //Run function injectScript with the page's HTML as oldPage in the function
}, 1111);
但是控制台日志中从未达到3级大概是由于某种错误开口(document.open()
)文件或更换(.replace
)的脚本。
输出:
//Replace stage 1: target HTML JS_tamper.user.js:29:5
//Replace stage 2: Replace text matching "http://192.168.1.1/menuBcm.js" with "http://localhost:8080/tamper.js"
有谁知道为什么我有这个问题或解决使用Greasemonkey的(或其它扩展名)这个问题也许是另一种方式?
如果您对某些相关文件感兴趣,我已经在同一个项目上创建了另一个thread另一个(不同)问题。
谢谢您的回答!我应该用'window.a'替换一个东西吗? – Clone
@Clone这只是一个例子。这取决于'oldJSFile'的内容。你甚至可以重写一些本地API来提出一个错误。 – blackmiaool