GreaseMonkey 스크립트에서 "DOM Ready" 이벤트를 구현하는 방법은 무엇입니까?
-
09-06-2019 - |
문제
GreaseMonkey 스크립트를 window.onload에서 window.DOMContentLoaded로 실행하도록 수정하려고 하는데 이 이벤트가 실행되지 않습니다.
FireFox 2.0.0.16 / GreaseMonkey 0.8.20080609를 사용하고 있습니다.
이것 수정하려는 전체 스크립트는 다음과 같습니다.
window.addEventListener ("load", doStuff, false);
에게
window.addEventListener ("DOMContentLoaded", doStuff, false);
해결책
그래서 구글링을 해보니 그리스몽키 돔 준비 완료 그리고 첫 번째 결과 Greasemonkey 스크립트는 실제로 "DOM 준비" 상태에서 실행 중이므로 onload 호출을 제거하고 스크립트를 바로 실행하면 된다고 말하는 것 같습니다.
나는 window.addEventListener ("load", function() {
그리고 }, false);
포장하고 완벽하게 작동했습니다.그것은 많이 이렇게 하면 응답성이 더 좋아지고, 페이지에 스크립트가 적용된 상태로 바로 나타나며, 보이지 않는 모든 질문은 강조 표시되어 깜박임도 전혀 발생하지 않습니다.그리고 많은 기쁨이 있었습니다....응.
다른 팁
GreaseMonkey 스크립트는 DOMContentLoaded에서 자체적으로 실행되므로 로드 이벤트 핸들러를 추가할 필요가 없습니다. 스크립트가 필요한 모든 작업을 즉시 수행하도록 하세요.
@샘:응, 나도 똑같이 노력하고 있었어
// ==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