如何实现"DOM准备"事件中的油猴子的剧本吗?
-
09-06-2019 - |
题
我要修改我的油猴子的脚本从发射窗口。加载到窗口。DOMContentLoaded,但这种事件从未发生火灾。
我使用的火狐2.0.0.16/油猴子的0.8.20080609
此 是完整的脚本我在试着修改、改变:
window.addEventListener ("load", doStuff, false);
要
window.addEventListener ("DOMContentLoaded", doStuff, false);
解决方案
所以我用google搜索 油猴子的dom准备好 和 第一个结果 似乎在说,油猴子的脚本实际运行,在"DOM准备",所以你只是需要去除的加载电话和运行的脚本。
我删除了 window.addEventListener ("load", function() {
和 }, false);
包装和它的完美工作。它的 很多 更多的响应这种方式,页面会出现直离开你的脚本应用于它和它的所有看不见的问题突出显示,没有闪烁。并有多欣喜。...是啊。
其他提示
油猴子的脚本是自己的执行DOMContentLoaded,所以没有必要添加一个载的事件的处理程序-只有你的脚做什么就需要立即.
@Sam:是的,我试图相同:
// ==UserScript==
// @name Stack Overflow highlight viewed questions
// @namespace *
// @include http://stackoverflow.com/questions
// @include http://stackoverflow.com/questions?*
// @include http://stackoverflow.com/questions
// @include http://stackoverflow.com/questions?*
// @version 0.55 (DOM-Ready instead of onload)
// ==/UserScript==
(function() {
// Customizable items
// var fav_tags = ["python", "database", "mysql"]; // Your favorite tags
const UNSEEN_BACK_COLOR = "rgb(225,210,210)"; // Backcolor for the question already seen
const FAV_TAG_BACK_COLOR = "rgb(210,210,225)"; // Backcolor for the favorite tags
// Internal to the DOM
// const QUESTION_URL = "http:\/\/stackoverflow.com\/questions\/([0-9]+)\/";
const QUESTION_URL = "http:\/\/stackoverflow.com\/questions\/([0-9]+)\/";
const TAG_PREFIX = "show questions tagged ";
const SEEN_MARK = "x";
//
var seen_q = [];
var seen_q_str = "";
var seen_q_str = GM_getValue ("seen_q", "");
var seen_q = seen_q_str.split("|");
var fav_tags_str = GM_getValue ("fav_tags", "")
var fav_tags = fav_tags_str.split(" ")
var already_run = false;
GM_registerMenuCommand ("Set favorite tags", askTags);
// window.addEventListener ("DOMContentLoaded", doStuff, false);
if (! doStuff()) {
window.addEventListener ("load", doStuff, false);
}
function doStuff() {
var elements = window.document.getElementsByTagName('A');
if (! elements || already_run) {
return false;
} else {
already_run = true;
}
GM_log ("here");
for (elem = 0; elem < elements.length; elem++) {
if (elements[elem].href.match (QUESTION_URL)) {
curr_q = RegExp.$1;
// Already seen?
if ((seen_q.length < curr_q) || (seen_q [curr_q] != SEEN_MARK)) {
elements[elem].style.backgroundColor = UNSEEN_BACK_COLOR;
seen_q [curr_q] = SEEN_MARK;
}
// Is a favorite tag?
node = elements[elem].parentNode.parentNode;
for (tag = 0; tag <= fav_tags.length; tag++) {
if (node.innerHTML.match ("'" + fav_tags[tag] + "'")) {
node.style.backgroundColor = FAV_TAG_BACK_COLOR;
break;
}
}
// return (0);
}
}
seen_q_str = seen_q.join("|");
GM_setValue ("seen_q", seen_q_str);
return true;
}
function askTags() {
fav_tags_str = prompt("Favorite tags (separated by spaces)", fav_tags_str);
GM_setValue ("fav_tags", fav_tags_str)
}
})();
不隶属于 StackOverflow