forked from plaintextoffenders/pto-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tumblr_userscript.js
224 lines (193 loc) · 7.01 KB
/
tumblr_userscript.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// ==UserScript==
// @name PTO Editing Toolkit
// @locale English
// @namespace http://plaintextoffenders.com/
// @version 0.13
// @description Various tools for PTO editors
// @author Aviem Zur
// @match https://www.tumblr.com/*
// ==/UserScript==
var PTO_FORM_ID = "ptoSearchForm";
var previousOffenders = '';
var reformedOffenders = '';
window.checkIsPreviousOffender = function(offender) {
if (previousOffenders == '') {
window.loadGitHubData('offenders', offender);
}
if (reformedOffenders == '') {
window.loadGitHubData('reformed', offender);
}
if (reformedOffenders != '' && previousOffenders != '') {
window.handleOffenderResult(offender);
}
};
window.loadGitHubData = function(csvName, offender) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
if (xhr.responseText.length > 1000) {
if (csvName == 'offenders') {
previousOffenders = xhr.responseText;
}
if (csvName == 'reformed') {
reformedOffenders = xhr.responseText;
}
if (offender != '') {
window.handleOffenderResult(offender);
}
} else {
if (offender != '') {
window.checkIsPreviousOffender(offender);
}
}
}
};
var url = 'https://raw.githubusercontent.com/plaintextoffenders/plaintextoffenders/master/' + csvName + '.csv';
console.log(url);
xhr.open('GET', url , true);
xhr.send(null);
}
window.handleOffenderResult = function(offender) {
var responseNodeId = "ptoResponse";
var previousResponseNode = document.getElementById(responseNodeId);
if (previousResponseNode != null) {
window.removeNode(previousResponseNode);
}
var newResponseNode = null;
if (previousOffenders.includes(offender) || reformedOffenders.includes(offender)) {
newResponseNode = document.createElement("A");
newResponseNode.innerText = "Previous offense found!";
newResponseNode.href = 'https://github.com/plaintextoffenders/plaintextoffenders';
newResponseNode.target = "_blank";
newResponseNode.style.textDecoration = "underline";
newResponseNode.style.color = "#fc7676";
} else {
newResponseNode = document.createElement("DIV");
newResponseNode.innerText = "Not a previous offender";
newResponseNode.style.color = "#62bc60";
}
newResponseNode.id = responseNodeId;
window.getControls().appendChild(newResponseNode);
};
window.addSearch = function() {
var searchOffender = function() {
setTimeout(function () {
var offender = document.getElementById('ptoDomainInput').value.replace(/[, ]*/g, "");
window.checkIsPreviousOffender(offender);
}, 0);
};
var form = document.createElement("FORM");
form.id = PTO_FORM_ID;
form.innerText = "Offender search:";
var input = document.createElement("INPUT");
input.id = "ptoDomainInput";
input.type = "text";
input.onkeyup = searchOffender;
form.appendChild(document.createElement("BR"));
form.appendChild(input);
window.getControls().appendChild(form);
window.getControls().removeChild(window.getCloseButton());
};
window.clickFacebookBtn = function() {
if (document.getElementsByClassName('facebook checked')[0] == null) {
document.getElementsByClassName('facebook')[0].firstChild.click();
}
};
window.isTwitterChecked = function() {
return document.getElementsByClassName('twitter checked')[0] != null;
};
window.changeToAddToQueue = function() {
document.getElementsByClassName('dropdown-area')[0].click();
window.getByXPathSingle('//span[text()="Add to queue"]').click();
if (!window.isTwitterChecked()) {
window.getActionButton().style.display = "none";
}
};
window.fixTweetText = function() {
var tweetTextArea = document.getElementsByClassName('tweet-textarea')[0];
if (tweetTextArea != null) {
var expectedText = (/[^\n]*\n.*\n.*\n/.exec(window.getEditor().innerText)[0].replace('\n\n','\n') + "[URL]").replace(/[ \t]*\[URL\]/,"[URL]");
tweetTextArea.innerHTML = expectedText;
tweetTextArea.addEventListener("keyup", function() {
if (window.isTwitterChecked()) {
window.getActionButton().style.display = "block";
}
});
}
};
window.alreadyAddedSearch = function() {
return document.getElementById(PTO_FORM_ID) != null;
};
window.getByXPathSingle = function(xpath) {
return window.getByXPath(xpath).snapshotItem(0);
};
window.getByXPath = function(xpath) {
return document.evaluate(xpath,document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
};
window.getControls = function() {
return document.getElementsByClassName('control left')[1];
};
window.getCloseButton = function() {
return document.getElementsByClassName('tx-button')[0];
};
window.getEditor = function() {
return document.getElementsByClassName('editor editor-richtext')[0];
};
window.getActionButton = function() {
return document.getElementsByClassName('post-form--save-button')[0];
};
window.removeNode = function(node) {
node.parentNode.removeChild(node);
};
window.addDomainsLine = function() {
var editor = window.getEditor();
var editorText = editor.innerHTML;
var domains = window.getDomainsFromString(editorText);
if (domains.length > 0) {
editor.innerHTML = '<p>' + domains.join(', ') + '</p>' + editor.innerHTML;
document.getElementById('ptoDomainInput').value = domains[0];
window.checkIsPreviousOffender(domains[0]);
}
}
window.badDomainPrefixes = ['www.', 'http://', 'https://']
window.getDomainsFromString = function(str) {
var res = new Set();
var re1 = /(?=[^a-zA-Z0-9]([a-zA-Z0-9]+\.[a-zA-Z0-9]+))/g;
var re2 = /(?=[^a-zA-Z0-9]([a-zA-Z0-9]+\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+))/g;
str.replace(re1, function(match, g1, g2) { res.add(g1); });
str.replace(re2, function(match, g1, g2) { res.add(g1); });
res = new Set([...res].filter(function(s) {
for (let prefix of window.badDomainPrefixes) {
if (s.startsWith(prefix)) {
return false;
}
}
return true;
}));
for (let result of res) {
for (let result2 of res) {
if (result != result2 && result.includes(result2)) {
console.log(result2);
res.delete(result2);
}
}
}
return [...res];
}
var offender = '';
window.loadGitHubData('offenders', offender);
window.loadGitHubData('reformed', offender);
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutations, observer) {
if (!window.alreadyAddedSearch()) {
window.addSearch();
window.addDomainsLine();
window.changeToAddToQueue();
//window.clickFacebookBtn();
}
window.fixTweetText();
});
observer.observe(document, {
subtree: true,
attributes: true
});