Greasemonkey:停止嵌入式 JS 运行

发布于 2024-09-09 08:53:32 字数 488 浏览 3 评论 0原文

页面的 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 吗?

A page has the following in the html:

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

My greasemonkey script needs to prevent that script from running. How can I do this?


Update: I understand that in the general case this is impossible. However, in my specific case, I may have a loophole?

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

Is there some way I can define window.devicePixelRatio before the embedded script runs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心碎的声音 2024-09-16 08:53:32

现在可以通过 @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 拒绝,并且< a href="https://www.chromestatus.com/feature/5711871906676736" rel="nofollow noreferrer">Chrome 不太可能实现它。

它不会针对其他脚本插入的

This is now possible with @run-at document-start and Firefox's beforescriptexecute. Tested only in 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 was rejected for HTML 5 in 2016, and Chrome is unlikely to implement it.

It does not run for <script> nodes inserted by other scripts.

星星的軌跡 2024-09-16 08:53:32

关于:

有什么方法可以在嵌入脚本运行之前定义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 上实现这一点。使用checkForBadJavascripts实用程序来利用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]
] );

这会阻止第一个内联脚本,其中包含window.devicePixelRatio完全。如果您想有选择地修改该脚本的部分内容,请参阅此答案和/或这个答案

Re:

Is there some way I can define window.devicePixelRatio before the embedded script runs?

There is now. Like so:

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


In the general case:

Since Firefox version 4, this is now possible on Firefox only. Use the checkForBadJavascripts utility to leverage the power of beforescriptexecute. Like so:

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

This blocks the first inline script, that contains window.devicePixelRatio, entirely. If you want to selective modify parts of that script, see this answer and/or this answer.

债姬 2024-09-16 08:53:32

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

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

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

User scripts run after the page is loaded, so you aren't able to.

Unless, the code uses the "onload" event.

User scripts are executed after the
DOM is fully loaded, but before onload
occurs. This means that your scripts
can begin immediately and don't need
to wait for onload.

与他有关 2024-09-16 08:53:32

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

Another option is to use Privoxy instead of GreaseMonkey. You just use Privoxy as your proxy (on localhost) and search/replace the strings you don't like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文