【问题标题】:greasemonkey: stop embedded JS from running油脂猴:阻止嵌入式 JS 运行
【发布时间】:2018-08-05 00:13:20
【问题描述】:

一个页面在 html 中有以下内容:

<script type="text/javascript">
  // some code
</script>

我的greasemonkey 脚本需要阻止该脚本运行。我该怎么做?


更新: 我知道在一般情况下这是不可能的。但是,在我的具体情况下,我可能有漏洞?

<script type="text/javascript">
  if (!window.devicePixelRatio) {
    // some code that I -don't- want to be run, regardless of the browser
  }
</script>

有什么方法可以在嵌入式脚本运行之前定义window.devicePixelRatio

【问题讨论】:

    标签: javascript greasemonkey


    【解决方案1】:

    现在可以使用 @run-at document-start 和 Firefox 的 beforescriptexecute。仅在 FF24 中测试。

    // ==UserScript==
    ...
    // @run-at         document-start
    // ==/UserScript==
    
    //a search string to uniquely identify the script
    //for example, an anti-adblock script
    var re = /adblock/i;
    
    window.addEventListener('beforescriptexecute', function(e) {
    
        if(re.test(e.target.text)){
    
            e.stopPropagation();
            e.preventDefault();
        }
    
    }, true);
    

    beforescriptexecute 是 2016 年 HTML 5 的 rejectedChrome is unlikely to implement it

    它不适用于其他脚本插入的&lt;script&gt; 节点。

    【讨论】:

    • +1 但请注意,这仅适用于 FF,并且自 FF 版本 4 起已受支持。此外,在这种情况下,最好使用 .textContent 而不是 .text
    【解决方案2】:

    回复:

    有什么方法可以在嵌入式脚本运行之前定义window.devicePixelRatio

    现在有。像这样:

    // ==UserScript==
    // @name     _Pre set devicePixelRatio
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @run-at   document-start
    // @grant    none
    // ==/UserScript==
    //-- @grant none is imporatant in this case.
    
    window.devicePixelRatio = "Unimportant string or function or whatever";
    


    一般情况下:

    从 Firefox 版本 4 开始,现在只能在 Firefox 上实现。使用the checkForBadJavascripts utility 来利用beforescriptexecute 的力量。像这样:

    // ==UserScript==
    // @name     _Block select inline JS
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @require  https://gist.github.com/raw/2620135/checkForBadJavascripts.js
    // @run-at   document-start
    // @grant    GM_addStyle
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    
    checkForBadJavascripts ( [
        [false, /window\.devicePixelRatio/, null]
    ] );
    


    这会阻止 first 内联脚本,其中包含 window.devicePixelRatio完全。如果您想选择性地修改该脚本的某些部分,请参阅 this answer 和/或 this answer

    【讨论】:

      【解决方案3】:

      用户脚本在页面加载后运行,因此您无法这样做。

      除非代码使用“onload”事件。

      用户脚本在 DOM 已完全加载,但在 onload 之前 发生。这意味着您的脚本 可以立即开始,不需要 等待加载。

      【讨论】:

      • 谢谢。我想我会尽力尝试撤消脚本的作用。我已经更新了这个问题 - 可能 - 使它成为可能......或者当我的 GM 脚本运行时,为时已晚?
      • 不使用用户脚本。嵌入的 JavaScript 在浏览器加载后立即运行。加载整个页面后运行用户脚本。
      【解决方案4】:

      另一种选择是使用 Privoxy 代替 GreaseMonkey。您只需使用 Privoxy 作为您的代理(在本地主机上)并搜索/替换您不喜欢的字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-17
        • 1970-01-01
        • 1970-01-01
        • 2016-10-23
        • 2012-03-31
        • 2013-09-18
        • 1970-01-01
        相关资源
        最近更新 更多