Javascript greasemonkey:停止运行嵌入式JS

Javascript greasemonkey:停止运行嵌入式JS,javascript,greasemonkey,Javascript,Greasemonkey,页面在html中包含以下内容: <script type="text/javascript"> // some code </script> //一些代码 我的greasemonkey脚本需要阻止该脚本运行。我该怎么做 更新:我理解在一般情况下这是不可能的。然而,在我的具体情况下,我可能有一个漏洞 <script type="text/javascript"> if (!window.devicePixelRatio) { // som

页面在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){
//一些我不想运行的代码,不管浏览器是什么
}

是否有某种方法可以在嵌入式脚本运行之前定义
window.devicePixelRatio

用户脚本在加载页面后运行,因此您无法执行此操作

除非另有说明,否则代码使用“onload”事件

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


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

这现在可以通过和Firefox实现。仅在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年针对HTML5的

它不会为其他脚本插入的节点运行。

Re:

在嵌入式脚本运行之前,是否有某种方法可以定义
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上使用。用于在脚本执行之前利用
的功能。像这样:

// ==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";
// ==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]
] );


这将完全阻止第一个内联脚本,该脚本包含
window.devicePixelRatio
。如果要选择性地修改该脚本的某些部分,请参阅和/或。

谢谢。我想我会尽我最大的努力撤销脚本的功能。我已经用一些东西更新了这个问题,可能会使它成为可能。。。还是说当我的GM脚本运行时,已经太晚了?不是用用户脚本。嵌入式JavaScript在浏览器加载后立即运行。用户脚本在整个页面加载后运行。+1但请注意,这将仅在FF中工作,并且自FF版本4以来一直受支持。此外,在这种情况下,最好使用
.textContent
而不是
.text