-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvandalizer.user.js
212 lines (206 loc) · 9.09 KB
/
vandalizer.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
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
// ==UserScript==
// @name HTML Editing Aid
// @version 8.8.8
// @description Adds a 'Vandalize' button to the bottom of web pages.
// @author weeklyd3
// @match *://*/*
// @grant none
// ==/UserScript==
function userImportStyle(url) {
return new Promise(function(resolve, reject) {
var scr = document.createElement('link');
scr.rel = 'stylesheet';
scr.href = url;
scr.onload = function() {
resolve('Loaded');
}
scr.onerror = function() {
reject('Error');
}
document.querySelector('body').appendChild(scr);
});
}
function userImportScript(url) {
return new Promise(function(resolve, reject) {
var scr = document.createElement('script');
scr.onload = function() {
resolve('Loaded');
}
scr.onerror = function() {
reject('Error');
}
scr.src = url;
document.querySelector('body').appendChild(scr);
});
}
function handleUserscriptConsole(ev) {
if (ev.currentTarget.selectionStart == 0) {
ev.preventDefault();
ev.currentTarget.selectionStart = 1;
}
if (!ev.currentTarget.value.startsWith('>')) {
ev.preventDefault();
ev.currentTarget.value = '>' + ev.currentTarget.value;
}
if (ev.keyCode === 13 && !ev.shiftKey) {
if (ev.currentTarget.value.trim() === '>') return;
event.preventDefault();
var cmd = document.createElement('div');
var promptSpan = document.createElement('span');
promptSpan.textContent = '>> ';
promptSpan.style.color = '#cccccc';
var cmdSpan = document.createElement('span');
cmdSpan.textContent = ev.currentTarget.value.slice(1);
cmd.appendChild(promptSpan);
cmd.appendChild(cmdSpan);
document.getElementById('userscript-terminal-output').appendChild(cmd);
var output = document.createElement('div');
var morePrompt = document.createElement('span');
morePrompt.textContent = '<< ';
morePrompt.style.color = '#cccccc';
output.appendChild(morePrompt);
var res = document.createElement('span');
try {
res.textContent = JSON.stringify(eval(ev.currentTarget.value.slice(1)));
} catch (e) {
res = userscriptError(e);
}
output.appendChild(res);
document.getElementById('userscript-terminal-output').appendChild(output);
ev.currentTarget.value = '>';
}
}
(function() {
'use strict';
var butt = document.createElement('button');
butt.textContent = 'Vandalize';
butt.addEventListener('click', function() {
if (document.body.getAttribute('contenteditable') === 'true') {
this.textContent = 'Vandalize';
document.body.removeAttribute('contenteditable');
} else {
this.textContent = 'AAAHH Stop the vandalism';
document.body.setAttribute('contenteditable', 'true');
}
});
var div = document.createElement('div');
div.setAttribute('contenteditable', 'false');
div.style.width = "100%";
div.style.zIndex = '33283328';
div.style.fontFamily = 'sans-serif';
div.style.padding = '15px';
div.style.border = '1px solid lightgray';
div.style.backgroundColor = 'white';
var h2 = document.createElement('h2');
h2.textContent = 'Vandalize this page';
div.appendChild(h2);
div.appendChild(butt);
var reload = document.createElement('button');
reload.textContent = 'Reset and reload';
reload.addEventListener('click', function() {
if (!confirm('Are you sure you want to lose all your HTML edits?')) return;
location.reload();
});
div.appendChild(reload);
var console = document.createElement('div');
console.innerHTML = `<details id="userscript-jsconsole">
<summary>JavaScript Console</summary>
<button onclick="document.getElementById('userscript-terminal-output').textContent = 'Console cleared';">Clear console</button>
<button onclick="document.getElementById('userscript-jsconsole').open = false;">Close</button>
<br />
<label for="input">Your JS:</label>
<div id="userscript-terminal-output" style="white-space: pre-wrap; font-size: 16px; font-family: Consolas, monospace; max-height: 600px; resize: both; overflow-y: scroll;">Start of console
You can type your commands below. The leading > will not be included. Console methods are supported. console.log, console.warn, console.clear, and console.error also output here.</div>
<textarea spellcheck="false" id="input" onkeydown="handleUserscriptConsole(event)" onmouseup="handleUserscriptConsole(event)" rows="2" style="font-family: Consolas, monospace; padding: 3px; border: 1px solid #cccccc; outline: none; width: 100%;"></textarea>
</details>
<details>
<summary id="userpagesource">Page source</summary>
<div style="position: fixed; top: 0; left: 0; z-index: 2147483647; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5);">
<div style="background-color: white; width: 75%; height: 75%; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); overflow: auto;">
<div style="height:90%; overflow:auto">
<div style="display: table;"><div style="display: table-row; overflow: auto;"><pre style="font-size: 16px; white-space: pre-wrap; display: table-cell; word-break: break-all;"><code id="userpagesourcecode"></code></pre></div>
</div></div>
<div style="margin:20px"><button style="width: 100%" onclick="document.getElementById('userpagesource').click();">Close</button></div></div>
</div>
</details>`;
div.appendChild(console);
document.body.appendChild(div);
document.getElementById("userpagesource").onclick = userscriptRenderSource;
})();
window.handleUserscriptConsole = handleUserscriptConsole;
window.oldLog = console.log;
window.oldError = console.error;
window.oldWarn = console.warn;
window.oldClear = console.clear;
console.clear = function() {
document.querySelector('[id="userscript-terminal-output"]').innerHTML = 'Console cleared using console.clear()';
oldClear();
}
window.console.clear = console.clear;
console.error = function(text, type = "Thrown error", line = "e^((pi)(i))", stack = "idk", filename = "idk") {
oldError(text);
var output = document.createElement('div');
var morePrompt = document.createElement('span');
morePrompt.textContent = '<< ';
morePrompt.style.color = '#cccccc';
output.appendChild(morePrompt);
var res = document.createElement('span');
res = userscriptError({toString: function() { return text; }, stack: stack, lineNumber: line, fileName: filename, name: type});
output.appendChild(res);
var terminal = document.getElementById('userscript-terminal-output')
terminal.appendChild(output);
terminal.scrollTop = terminal.scrollHeight;
}
console.log = function(text) {
oldLog(text);
var output = document.createElement('div');
var morePrompt = document.createElement('span');
morePrompt.textContent = '<< ';
morePrompt.style.color = '#cccccc';
output.appendChild(morePrompt);
var res = document.createElement('span');
res.textContent = text;
res.style.backgroundColor = 'black';
res.style.color = 'white';
output.appendChild(res);
var terminal = document.getElementById('userscript-terminal-output')
terminal.appendChild(output);
terminal.scrollTop = terminal.scrollHeight;
}
console.warn = function(text) {
oldWarn(text);
var output = document.createElement('div');
var morePrompt = document.createElement('span');
morePrompt.textContent = '<< ';
morePrompt.style.color = '#cccccc';
output.appendChild(morePrompt);
var res = document.createElement('span');
res.style.backgroundColor = 'gold';
res.textContent = text;
output.appendChild(res);
var terminal = document.getElementById('userscript-terminal-output')
terminal.appendChild(output);
terminal.scrollTop = terminal.scrollHeight;
}
window.console.log = console.log;
window.console.error = console.error;
window.onerror = function(message, source, lineno, colno, error) {
console.error(error.toString(), error.name, error.lineNumber, error.stack, error.fileName);
};
function userscriptError(e) {
var res = document.createElement('span');
res.textContent = 'ERROR: ' + e.name + "\n" + e.toString() + `\n @file '${e.fileName}', line ${e.lineNumber}\nStack trace:\n${e.stack}`;
res.style.color = 'red';
res.style.backgroundColor = 'pink';
return res;
}
window.userscriptError = userscriptError;
function userscriptRenderSource() {
document.getElementById('userpagesourcecode').innerHTML = '';
document.getElementById('userpagesourcecode').textContent = new XMLSerializer().serializeToString(document);
userImportStyle('https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/vs.min.css');
userImportScript('https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js').then(function() {
hljs.highlightElement(document.getElementById('userpagesourcecode'));
});
}
window.userscriptRenderSource = userscriptRenderSource;