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