如何使Greasemonkey在页面上的“其他"之前运行? [英] How does one have Greasemonkey run before *anything* else in the page?

查看:74
本文介绍了如何使Greasemonkey在页面上的“其他"之前运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在页面上的其他之前运行Greasemonkey脚本?

Is it possible to have Greasemonkey scripts run before anything else on the page?

我知道@run-at document-start,但这似乎在<HTML>标记后立即运行.通常这不是问题,但是如果页面格式错误(如以下示例所示),则似乎无能为力.

I'm aware of @run-at document-start, but this appears to run immediately after the <HTML> tag. Normally this isn't a problem, but if the page is misformatted as in the example below, there doesn't seem to be anything I can do.

我将不胜感激任何建议或想法. 谢谢!

I'd appreciate any suggestions or ideas. Thanks!

<script>alert('This is an annoying message.');</script>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
        <HEAD>
...etc...

推荐答案

这是不可能的,因为HTML要求脚本是在解析过程中执行或根本不执行,例如这是允许的:

It's not possible, because HTML requres that scripts are executed during parsing or not at all, e.g. this is allowed:

<script> document.write('<!'+'--'); </script>

如果浏览器不执行该脚本而经过该脚本,它将看到完全不同的文档,因此您无法在脚本运行之前分析HTML文档的DOM.

If browser goes past this script without executing it, it will see completely different document, therefore you can't analyze DOM of HTML document before scripts run.

Opera通过触发BeforeScript事件来解决 UserJS 中的问题,允许UserJS在最后一刻更改/删除脚本.

Opera solves this problem in UserJS by firing BeforeScript events, allowing UserJS to change/remove scripts at the very last moment.

这篇关于如何使Greasemonkey在页面上的“其他"之前运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆