-
Notifications
You must be signed in to change notification settings - Fork 4
/
duolingo_markupcleaner.user.js
141 lines (124 loc) · 3.73 KB
/
duolingo_markupcleaner.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// ==UserScript==
// @name DuoMarkupCleaner
// @namespace https://github.com/liuch/duolingo-scripts
// @include https://forum.duolingo.com/*
// @version 0.1.1
// @grant none
// @description This script removes excessive user markup on the forum (currently only color text).
// @description:ru Этот скрипт удаляет чрезмерную разметку пользователей на форуме (пока только цветной текст).
// @updateURL https://github.com/liuch/duolingo-scripts/raw/master/duolingo_markupcleaner.meta.js
// @downloadURL https://github.com/liuch/duolingo-scripts/raw/master/duolingo_markupcleaner.user.js
// @author FieryCat aka liuch
// ==/UserScript==
// @license MIT License
function inject(f) { //Inject the script into the document
var script = document.createElement('script');
script.type = 'text/javascript';
script.setAttribute('name', 'duomarkupcleaner');
script.textContent = '(' + f.toString() + ')()';
document.head.appendChild(script);
}
inject(f);
function f() {
"use strict";
var MAX_COLOR_PERCENT = 0.3;
var first = true;
var observe = {
observer: null,
root_el: null,
p_func: null,
set: function(func) {
this.root_el = document.getElementsByTagName("body")[0];
if (this.root_el) {
this.p_func = func;
this.observer = new MutationObserver(function(mutations) {
if (observe.p_func) {
observe.stop();
observe.p_func()
observe.start();
}
});
}
},
start: function() {
this.observer.observe(this.root_el, { childList: true, subtree: true });
},
stop: function() {
this.observer.disconnect();
}
};
function switch_attr(el, n1, n2) {
el.setAttribute(n2, el.getAttribute(n1));
el.removeAttribute(n1);
}
function warn_text(count, cleared) {
return "! DuoMarkupCleaner: " + (cleared && "Cleared" || "Found") + " " + count + " color item" + (count > 1 && "s" || "") + ".";
}
function toggle_attr(content, warn) {
var t_res = content.classList.toggle("dmc-cleaned");
var a1 = "color", a2 = "dmc_color";
if (!t_res)
a2 = [a1, a1 = a2][0];
var cnt = 0;
content.querySelectorAll("font[" + a1 + "]").forEach(function(e) {
switch_attr(e, a1, a2);
++cnt;
});
warn.children[0].textContent = warn_text(cnt, t_res);
return t_res;
}
function make_warn_element() {
var i = document.createElement("p");
i.setAttribute("style", "margin:0;");
i.setAttribute("class", "dmc-warn");
var ii = document.createElement("small");
ii.setAttribute("title", "Click to toggle");
ii.setAttribute("style", "color:gray");
i.appendChild(ii);
return i;
}
function scan_element(el) {
var c_len = 0;
var c_arr = el.querySelectorAll("font[color]");
if (c_arr.length > 0) {
var cnt = 0;
c_arr.forEach(function(e) {
c_len += e.textContent.length;
++cnt;
});
var w_el = make_warn_element();
el.appendChild(w_el);
var t_len = el.textContent.length;
if (t_len > 0 && c_len / t_len > MAX_COLOR_PERCENT)
toggle_attr(el, w_el);
else
w_el.children[0].textContent = warn_text(cnt, false);
}
el.classList.add("dmc-handled");
}
function scan_dom() {
var cl = document.querySelectorAll("div[itemprop=text]:not(.dmc-handled");
if (cl.length > 0) {
cl.forEach(function(e) {
scan_element(e);
});
if (first) {
first = false;
var el = cl[0].closest("section[itemtype]");
if (el) {
el.addEventListener("click", function(event) {
var tg = event.target.parentElement;
if (tg.classList.contains("dmc-warn")) {
toggle_attr(tg.closest("div[itemprop=text]"), tg);
}
});
}
}
}
}
setTimeout(function() {
scan_dom();
observe.set(scan_dom);
observe.start();
}, 100);
}