You*_*suf 24 javascript greasemonkey
我想停止从站点执行一行,以便浏览器读取整个页面,但该行除外.或者浏览器可以简单地跳过该javascript函数的执行.
要么
有没有办法我可以以某种方式调整javascript,以便javascript中的随机数生成函数不生成随机数,但我想要的数字...
我无权访问托管脚本的站点,因此所有这些都需要在客户端完成.
Bro*_*ams 37
火狐目前支持的beforescriptexecute事件(截至4版,于2011年3月22日发布)‡.
与该事件和该// @run-at document-start指令,Firefox和Greasemonkey的现在似乎做好拦截特定的<script>标签.
Chrome + Tampermonkey仍然无法做到这一点.除了Firefox + Greasemonkey之外,您还需要使用下面其他答案中所示的技术来编写完整的浏览器扩展.
该checkForBadJavascripts函数封装了这个.例如,假设页面有这样的<script>标记:
<script>
alert ("Sorry, Sucka! You've got no money left.");
</script>
Run Code Online (Sandbox Code Playgroud)
你可以checkForBadJavascripts像这样使用:
checkForBadJavascripts ( [
[ false,
/Sorry, Sucka/,
function () {
addJS_Node ('alert ("Hooray, you\'re a millionaire.");');
}
]
] );
Run Code Online (Sandbox Code Playgroud)
得到一个更好的消息.(^_^)
有关详细信息,请参阅checkForBadJavascripts中的内联文档.
要在完整脚本中查看演示,请首先访问此页面的jsBin.您将看到3行文本,其中两行由JS添加.
现在,安装此脚本(查看源代码 ;它也在下面.)并重新访问该页面.您将看到GM脚本删除了一个坏标签,并用我们的"好"JS替换了另一个.
‡请注意,只有Firefox支持该beforescriptexecute事件.它已从HTML5规范中删除,未指定等效功能.
完整的GM脚本示例(与GitHub和jsBin中的相同):
鉴于此HTML:
<body onload="init()">
<script type="text/javascript" src="http://jsbin.com/evilExternalJS/js"></script>
<script type="text/javascript" language="javascript">
function init () {
var newParagraph = document.createElement ('p');
newParagraph.textContent = "I was added by the old, evil init() function!";
document.body.appendChild (newParagraph);
}
</script>
<p>I'm some initial text.</p>
</body>
Run Code Online (Sandbox Code Playgroud)
使用此Greasemonkey脚本:
// ==UserScript==
// @name _Replace evil Javascript
// @include http://jsbin.com/ogudon*
// @run-at document-start
// ==/UserScript==
/****** New "init" function that we will use
instead of the old, bad "init" function.
*/
function init () {
var newParagraph = document.createElement ('p');
newParagraph.textContent = "I was added by the new, good init() function!";
document.body.appendChild (newParagraph);
}
/*--- Check for bad scripts to intercept and specify any actions to take.
*/
checkForBadJavascripts ( [
[false, /old, evil init()/, function () {addJS_Node (init);} ],
[true, /evilExternalJS/i, null ]
] );
function checkForBadJavascripts (controlArray) {
/*--- Note that this is a self-initializing function. The controlArray
parameter is only active for the FIRST call. After that, it is an
event listener.
The control array row is defines like so:
[bSearchSrcAttr, identifyingRegex, callbackFunction]
Where:
bSearchSrcAttr True to search the SRC attribute of a script tag
false to search the TEXT content of a script tag.
identifyingRegex A valid regular expression that should be unique
to that particular script tag.
callbackFunction An optional function to execute when the script is
found. Use null if not needed.
*/
if ( ! controlArray.length) return null;
checkForBadJavascripts = function (zEvent) {
for (var J = controlArray.length - 1; J >= 0; --J) {
var bSearchSrcAttr = controlArray[J][0];
var identifyingRegex = controlArray[J][1];
if (bSearchSrcAttr) {
if (identifyingRegex.test (zEvent.target.src) ) {
stopBadJavascript (J);
return false;
}
}
else {
if (identifyingRegex.test (zEvent.target.textContent) ) {
stopBadJavascript (J);
return false;
}
}
}
function stopBadJavascript (controlIndex) {
zEvent.stopPropagation ();
zEvent.preventDefault ();
var callbackFunction = controlArray[J][2];
if (typeof callbackFunction == "function")
callbackFunction ();
//--- Remove the node just to clear clutter from Firebug inspection.
zEvent.target.parentNode.removeChild (zEvent.target);
//--- Script is intercepted, remove it from the list.
controlArray.splice (J, 1);
if ( ! controlArray.length) {
//--- All done, remove the listener.
window.removeEventListener (
'beforescriptexecute', checkForBadJavascripts, true
);
}
}
}
/*--- Use the "beforescriptexecute" event to monitor scipts as they are loaded.
See https://developer.mozilla.org/en/DOM/element.onbeforescriptexecute
Note that it does not work on acripts that are dynamically created.
*/
window.addEventListener ('beforescriptexecute', checkForBadJavascripts, true);
return checkForBadJavascripts;
}
function addJS_Node (text, s_URL, funcToRun) {
var D = document;
var scriptNode = D.createElement ('script');
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
//--- Don't error check here. if DOM not available, should throw error.
targ.appendChild (scriptNode);
}
Run Code Online (Sandbox Code Playgroud)
答案取决于未提供的细节(确切的页面和代码行是最好的),但这里是你如何做到的:
如果违规的JS代码没有立即启动(Fires after DOMContentLoaded),那么您可以使用Greasemonkey替换有问题的代码.例如:
var scriptNode = document.createElement ("script");
scriptNode.textContent = "Your JS code here";
document.head.appendChild (scriptNode);
Run Code Online (Sandbox Code Playgroud)
完成.
如果JS代码立即触发,那么它会变得更复杂.
首先,获取脚本的副本并对其进行所需的更改.在本地保存.
是有问题的脚本文件或者是它的主网页HTML(<script src="Some File>对<script>Mess O' Code</script>)?
如果脚本位于文件中,请安装Adblock Plus并使用它来阻止加载该脚本.然后使用Greasemonkey将修改后的代码添加到页面中.例如:
var scriptNode = document.createElement ("script");
scriptNode.setAttribute ("src", "Point to your modified JS file here.");
document.head.appendChild (scriptNode);
Run Code Online (Sandbox Code Playgroud)如果脚本位于主HTML页面中,则安装NoScript(最佳)或YesScript并使用它来阻止来自该站点的JavaScript.
这意味着您将需要使用Greasemonkey替换您想要运行的所有脚本.
beforescriptexecute不再适用于 Firefox,也不适用于 Chrome。幸运的是,还有一种替代方法 using MutationObserver,它得到了广泛的支持。一般的想法是在页面加载开始时添加一个 MutationObserver,每当有新节点添加到 DOM 时,它都会运行一个回调。在回调中,检查<script>您要更改或删除的标签是否存在。如果它存在,您可以篡改它(例如更改它的textContent,或将其src指向其他地方)。新添加的script标签只有在回调完成后才会运行,因此这是一种拦截和更改页面Javascript的有效方式。这是一个实时片段示例:
<script>
// This is the userscript code, which runs at the beginning of pageload
// Say we wanted to prevent the loading of the jQuery script tag below:
new MutationObserver((_, observer) => {
const jqueryScriptTag = document.querySelector('script[src*="jquery"]');
if (jqueryScriptTag) {
console.log('Found jQuery script tag; now removing it!');
jqueryScriptTag.remove();
// We've done what we needed to do, no need for the MutationObserver anymore:
observer.disconnect();
}
})
.observe(document.documentElement, { childList: true, subtree: true });
</script>
<div>Example content</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
console.log('After jQuery script tag. If done right, $ should be undefined:');
console.log(typeof $);
</script>Run Code Online (Sandbox Code Playgroud)
这是一个示例用户脚本,它阻止 jQuery<head>在 Stack Overflow 上的此处加载:
// ==UserScript==
// @name Example jQuery removal
// @include https://stackoverflow.com*
// @run-at document-start
// @grant none
// ==/UserScript==
if (document.head) {
throw new Error('Head already exists - make sure to enable instant script injection');
}
new MutationObserver((_, observer) => {
const jqueryScriptTag = document.querySelector('script[src*="jquery"]');
if (jqueryScriptTag) {
jqueryScriptTag.remove();
observer.disconnect();
}
})
.observe(document.documentElement, { childList: true, subtree: true });
Run Code Online (Sandbox Code Playgroud)
如果你安装这个,你会看到jQuery的加载失败,导致Stack Overflow的JS产生很多错误。
确保尽快附加 MutationObserver - 您需要@run-at document-start在页面加载<head>. (如果使用 Tampermonkey / Chrome,您可能需要启用实验性即时脚本注入才能可靠地实现这一点 - 转到 Tampermonkey 设置,配置模式:高级,滚动到底部,将实验性注入模式设置为即时。)
如果您正在为其他人编写用户脚本,任何您正在使用这种技术,请确保包含即时脚本注入的说明,因为在 Chrome 上默认情况下注入不是即时的。
请注意,观察者是使用附加的
.observe(document.documentElement, { childList: true, subtree: true });
Run Code Online (Sandbox Code Playgroud)
这武官观测到的<html>元素,添加和删除的即时儿童手表childList: true,并添加和删除节点手表及其后代中的任何位置用subtree: true。这种递归侦听器很有用,但它在大型动态页面上的计算成本也很高,因此请确保在达到目的后将其删除。
在一个巨大的页面上,调用querySelector每个突变可能代价高昂,因此您可能希望迭代mutations(观察者回调的第一个参数)和突变的addedNodes替代:
// ==UserScript==
// @name Example jQuery removal
// @include https://stackoverflow.com*
// @run-at document-start
// @grant none
// ==/UserScript==
if (document.head) {
throw new Error('Head already exists - make sure to enable instant script injection');
}
new MutationObserver((_, observer) => {
const jqueryScriptTag = document.querySelector('script[src*="jquery"]');
if (jqueryScriptTag) {
jqueryScriptTag.remove();
observer.disconnect();
}
})
.observe(document.documentElement, { childList: true, subtree: true });
Run Code Online (Sandbox Code Playgroud)
您还可以通过textContent在观察者回调中分配给内联脚本来调整内联脚本。以下代码段展示了如何将随机数生成器函数更改为始终返回 10,而不是 1-6:
.observe(document.documentElement, { childList: true, subtree: true });
Run Code Online (Sandbox Code Playgroud)
方法 1 是您在为他人编写用户脚本时可以使用的技术。但是,如果用户脚本仅供您自己使用,则有一种更简单的方法可以在某些情况下使用。请参阅Chrome Dev Tools - Modify javascript and reload问题的答案:通过转到 Chrome Devtools 中的 Sources -> Overrides 选项卡,并启用本地覆盖,您可以告诉浏览器加载 a 的本地副本,.js而不是下载站点的版本。然后您可以根据需要编辑本地副本,它将运行而不是内置脚本。调整较大的JavaScript文件时,这是非常有用的-这是很多比MutationObserver方法更易于管理。
但是,有一些缺点:
<script>// code here</script>标记,您必须下载页面的本地副本。(因此,如果页面通过 HTML 响应提供动态内容,您要么必须重新保存并重新调整.html覆盖以使用新内容,要么必须返回到 MutationObserver 方法。)Mic*_*Mic -2
您可以使用所谓的小书签。
构建要在其他站点上运行的 js 文件:your.js
使用以下代码制作一个 HTML 页面:
<html>
<body>
<a href="javascript:(function(){var s=document.createElement('SCRIPT');s.src='/url/to/your.js?'+(Math.random());document.getElementsByTagName('head')[0].appendChild(s);})()">
Drag'n Drop this to your bookmarks
</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
替换/url/to/your.js为你的js文件的路径。
在浏览器中加载该小页面,然后将链接拖放到书签栏。
转到您想要破解的网站,然后单击您刚刚创建的书签。
这将加载your.js页面并运行代码。
注意:这?'+(Math.random())部分是为了避免你的js被缓存,这不是强制性的,但在你开发时很有帮助your.js