停止执行Javascript函数(客户端)或调整它

我想停止从一个站点执行一行,以便整个页面被浏览器读取,除了单行。 或者,浏览器可能会简单地跳过执行该JavaScript函数。

要么

有没有办法我可以调整JavaScript以某种方式使JavaScript中的随机数生成函数不生成随机数,但我想要的数字…

我没有访问脚本托管的网站,所以这一切都需要做客户端。

Firefox目前支持beforescriptexecute事件 (截至2011年3月22日发布 , 版本4 )

有了这个事件和// @run-at document-start指令 ,Firefox和Greasemonkey现在好像拦截了特定的<script>标签。

对于Chrome + Tampermonkey,这仍然是不可能的 。 除了Firefox + Greasemonkey以外,您需要使用下面其他答案中所示的技术来编写完整的浏览器扩展。

checkForBadJavascripts函数封装了这个。 例如,假设页面有一个<script>标签,如下所示:

 <script> alert ("Sorry, Sucka! You've got no money left."); </script> 

你可以像这样使用checkForBadJavascripts

 checkForBadJavascripts ( [ [ false, /Sorry, Sucka/, function () { addJS_Node ('alert ("Hooray, you\'re a millionaire.");'); } ] ] ); 

得到一个更好的信息。 (^_^)
有关更多信息,请参见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> 

使用这个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); } 

答案取决于没有提供的细节(确切的页面和代码行将是最好的),但以下是你如何做一般:

  1. 如果有问题的JS代码没有立即启动 (在DOMContentLoaded之后触发),那么您可以使用Greasemonkey替换有问题的代码。 例如:

     var scriptNode = document.createElement ("script"); scriptNode.textContent = "Your JS code here"; document.head.appendChild (scriptNode); 

    完成。

  2. 如果JS代码立即触发 ,则会变得更复杂。
    首先,抓取脚本的副本并对其进行所需的更改。 保存在本地。

  3. 是一个文件中的有问题的脚本还是它在主页面HTML( <script src="Some File><script>Mess O' Code</script> )?

  4. 如果脚本在文件中,请安装Adblock Plus并使用它阻止加载该脚本。 然后使用Greasemonkey将修改后的代码添加到页面中。 例如:

     var scriptNode = document.createElement ("script"); scriptNode.setAttribute ("src", "Point to your modified JS file here."); document.head.appendChild (scriptNode); 
  5. 如果脚本位于主HTML页面中,则安装NoScript (最佳)或YesScript,并使用它阻止该站点的JavaScript。
    这意味着您将需要使用Greasemonkey来替换您希望运行的所有来自该站点的脚本。

使用TamperMonkey? 你需要在TamperMonkey头文件中添加// @grant// @require http://urlofyoursite.com/myfile.js 。 例如,这里是我的TamperMonkey thingie的顶部:

 // ==UserScript== // @name Project // @namespace http://tampermonkey.net/ // @version 0.1 // @description try to take over the world! // @author You // @match https://docs.google.com/presentation/* // @grant none // @require http://cdnjs.cloudflare.com/ajax/libs/annyang/2.1.0/annyang.min.js // ==/UserScript== 

你可以使用所谓的书签。

建立你想在其他网站上运行的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> 

/url/to/your.js替换为您的js文件的路径。

在浏览器中加载该小页面,然后拖放链接到书签栏。

转到您要破解的网站,然后点击刚创建的书签。
这将加载your.js中的your.js并运行代码。

注意?'+(Math.random())部分是为了避免你的js被缓存,这不是强制性的,但是当你开发你的your.js