- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
// ==UserScript==
// @name no horses
// @match *://govnokod.ru/*
// @grant none
// @run-at document-start
// ==/UserScript==
var CONFIG = {
horses: [
"Horse2",
"PragramistOtBoga",
"anonimb84a2f6fd141",
],
autoDownVote: true,
};
var observer = new MutationObserver(observeCallback);
var config = {
childList: true,
subtree: true,
};
observer.observe(document, config);
function observeCallback(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes) {
Array.prototype.forEach.call(mutation.addedNodes, function(node) {
try {
if (node.nodeType === 1 && /^comments_\d+$/.test(node.id)) {
handleComments(node);
}
} catch (e) {
console && console.warn && console.warn(e);
}
});
}
});
}
function downVote(node, type) {
var sel;
switch (type) {
case "post": sel = ".vote-against"; break;
case "comment": sel = ".comment-vote-against"; break;
default: throw 42; break;
}
var el = node.querySelector(sel);
if (el) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, unsafeWindow,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
el.dispatchEvent(evt);
}
}
function handleComments(node) {
var comments = node.querySelectorAll(".entry-comment-wrapper");
Array.prototype.forEach.call(comments, function(comment) {
try {
handleComment(comment);
} catch (e) {
console && console.warn && console.warn(e);
}
});
}
function handleComment(node) {
var author = node.querySelector(".entry-author").textContent.trim();
if (CONFIG.horses.indexOf(author) != -1) {
node.style.opacity = 0.3;
node.style.maxHeight = "4em";
node.style.overflow = "scroll";
if (CONFIG.autoDownVote) {
downVote(node, "comment");
}
}
}
function handlePosts(node) {
var posts = node.querySelectorAll(".hentry");
var i;
for (i = 0; i < posts.length; i++) {
try {
handlePost(posts[i]);
} catch (e) {
console && console.warn && console.warn(e);
}
}
}
function handlePost(node) {
var author = node.querySelector(".author a:nth-child(2)").textContent.trim();
if (CONFIG.horses.indexOf(author) != -1) {
if (!/^\/\d+$/.test(document.location.pathname)) {
node.style.opacity = 0.3;
node.style.maxHeight = "4em";
node.style.overflow = "scroll";
}
if (CONFIG.autoDownVote) {
downVote(node, "post");
}
}
}
document.addEventListener("DOMContentLoaded", function() {
handleComments(document.body);
handlePosts(document.body);
});
Я так и не смог заставить MutationObserver срабатывать на новые элементы, появляющиеся во время загрузки страницы. Отсюда и костыль в последних строчках.