gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:16:42 26 4
gpt4 key购买 nike

我目前有以下 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com