我尝试制作一个删除页面上另一个脚本的脚本,但没有成功。
该页面在 <body>
中加载了 2 个脚本我不想执行:
<div id="myindex">
<div class="wrap">
<div id="idone"></div>
<div id="idtwo"></div>
<script type="text/javascript"></script>
<script type="text/javascript"></script>
<script type="text/javascript"></script> //To Remove
<script type="text/javascript"></script> //To Remove
</div>
</div>
如何只阻止那些脚本运行?
这与新的 Greasemonkey @run-at 指令配合得很好!
这是一个示例用户脚本:
http://userscripts.org/scripts/show/125936
重要引述:
This user script will run right at the document beginning, as for
Scriptish, it even runs before getting any HTTP response ( which is
excellent for redirecting ).
它展示了如何从网站中删除恶意 JS。
这是完整的脚本示例:
// ==UserScript==
// @name editscript
// @version 0.0.1
// @namespace example@example.com
// @include http://*
// @run-at document-start
// ==/UserScript==
var changed = 0; // script need to be edited with
window.addEventListener('beforescriptexecute', function(e) {
///for external script:
src = e.target.src;
if (src.search(/bad\.js/) != -1) {
changed++;
e.preventDefault();
e.stopPropagation();
append(NewScript1);
};
///for inline script:
if(e.target===document.getElementsByTagName("script")[0]){
changed++;
e.stopPropagation();
e.preventDefault();
//todo
}
//tips: you could also run a regex search for the e.target.innerHTML
//if the position of the inline script is not fixed.
///when done, remove the listener:
if(changed == 2) window.removeEventListener(e.type, arguments.callee, true);
}, true);
////// append with new block function:
function append(s) {
document.head.appendChild(document.createElement('script'))
.innerHTML = s.toString().replace(/^function.*{|}$/g, '');
}
////////////////////////////////////////////////
function NewScript1(){
/* insert new block here, like: */
function load_good_stuff(){
/* we still got it!!
* only without the evil thing.
*/
}
};