javascript - 如何更改脚本以仅加载页面内容而不加载图像等?

标签 javascript firefox greasemonkey

我目前有以下 greasemonkey 脚本:

// ==UserScript==
// @name        test_script
// @namespace   my.example.com
// @include     http://example.com/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// @version     1
// @grant       none
// ==/UserScript==

(function(window, undefined) {

        // normalized window
        var w;
        if (unsafeWindow != "undefined"){
                w = unsafeWindow
        } else {
                w = window;
        }

        // You can inject almost any javascript library here.
        // Just pass the w as the window reference,
        // e.g. jquery.min.js embedding:
        // (function(a,b){function ci(a) ... a.jQuery=a.$=d})(w);

        // do not run in frames
        if (w.self != w.top){
                return;
        }
        // additional url check.
        // Google Chrome do not treat @match as intended sometimes.
        if (/http:\/\/example.com\//.test(w.location.href)){
                var link = $('span.link').text();
                if (link) {
                    location.replace(link);
                }
        }
})(window);

一旦我打开 example.com 页面,它会在此处查找 URL 并重定向我。它运行良好,但只有在加载所有图像、样式表等时才会发生重定向。我该如何解决? Looks like它应该发生在之前。

最佳答案

显示的代码在图像加载之前触发,但不一定在 CSS 之前触发。
来自Mozilla's DOMContentLoaded doc (和 v erified in Gecko, Webkit and IE ):

Stylesheet loads block script execution, so if you have a <script> after a <link rel="stylesheet" ...>, the page will not finish parsing - and DOMContentLoaded will not fire - until the stylesheet is loaded.

显然,如果 CSS 和/或图像很小或被缓存,它们也可能会在脚本触发之前加载。

可能可以使用 @run-at document-start 解决这个问题.参见 this answer , 以及类似的例子。

关于javascript - 如何更改脚本以仅加载页面内容而不加载图像等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13102225/

相关文章:

javascript - 在 firefox/internet explorer/chrome 中使用 javascript 播放音频标签

javascript - axios : TypeError: Cannot read property 'status' of undefined 错误

css - margin-left 在 Firefox 中不起作用

javascript - json 文件未加载到浏览器

javascript - 为什么我不能直接从我的用户脚本生成 'click' 事件?

Jquery 选择器在用户脚本上返回 Null

jquery - 如何使 JQuery 可调整大小和可拖动与 Greasemonkey 一起使用?

javascript - 如何将 Puppeteer 与 Stripe 元素结合使用

javascript - 使用 JavaScript 查找并替换表中仅过滤的行

css - Firefox 中的输入字段在没有焦点的情况下会发光