javascript - 删除或阻止选择 JavaScript 的 Greasemonkey 脚本

标签 javascript greasemonkey

<分区>

我尝试制作一个删除页面上另一个脚本的脚本,但没有成功。

该页面在 <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. 
             */
    }
};

关于javascript - 删除或阻止选择 JavaScript 的 Greasemonkey 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7120626/

相关文章:

javascript - 将 <img> 替换为 <object>

javascript - 拒绝iframe javascript滚动父窗口

javascript - 无法在 href 标签上定义点击事件

javascript - 让脚本模拟控件、按键序列?

javascript - 如何让 Greasemonkey 自动点击特定站点上的 "weird"链接?

javascript - 如何使用 jQuery 将 jsp 页面的内容加载到模式窗口中?

javascript - CKEditor插件: Hightlight selected text by hovering on a new item in the toolbar and selecting from a drop down

javascript - 如何获取CSS类中元素的属性集

JavaScript/XPath 错误

javascript - 我的 `head` 在哪里?