Joe*_*oey 2 javascript greasemonkey
假设有一个站点在html脚本标记中包含一个外部.js文件,如下所示:
<script src="somescript.js">
Run Code Online (Sandbox Code Playgroud)
我希望lublessmonkey拦截每个这样的脚本,并在执行之前更改其中的某些值。例如,我想将其中所有出现的值“ 400”更改为“ 300”,然后继续加载页面,就像脚本使用这些值而不是原始值一样。目前,我在油脂单中使用以下代码:
function replaceTargetJavascript (scriptNode) {
var scriptSrc = scriptNode.textContent;
scriptSrc = scriptSrc.replace (
/'400'/,
"'300'"
);
addJS_Node (scriptSrc);
}
document.addEventListener("beforescriptexecute", function(e) {
checkForBadJavascripts ( [
[false, /'400'/, replaceTargetJavascript]
] );
}, true);
Run Code Online (Sandbox Code Playgroud)
根据我的消息来源,哪种方法是正确的方法,但没有用。谁能帮我解决这个问题?
小智 6
旧问题,但我最近需要这样做。这是我使用GreaseMonkey做到的方法。
您添加beforescriptexecute侦听器,并等待目标脚本加载,然后检查src标记以识别正确的脚本。
然后,停止加载该脚本,并使用GM_xmlhttpRequest自己获取脚本源。
然后,您可以随意修改脚本,然后将其重新插入DOM。
// ==UserScript==
// @name Test
// @namespace Test
// @description TEST
// @include http://the.website.com/*
// @version 1
// @grant GM_xmlhttpRequest
// @run-at document-start
// ==/UserScript==
function addScript(text) {
text = text.replace(/replaceThis();/g, "");
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) {
src = e.target.src;
if (src.search(/script_to_modify\.js/) != -1) {
e.preventDefault();
e.stopPropagation();
GM_xmlhttpRequest({
method: "GET",
url: e.target.src,
onload: function(response) {
addScript(response.responseText);
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1357 次 |
| 最近记录: |