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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*******************************************************************************
uBlock Origin - a comprehensive, efficient content blocker
Copyright (C) 2023-present Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/
/* globals CodeMirror, uBlockDashboard, beautifier */
'use strict';
/******************************************************************************/
import { dom, qs$ } from './dom.js';
import { getActualTheme } from './theme.js';
/******************************************************************************/
const urlToDocMap = new Map();
const params = new URLSearchParams(document.location.search);
let currentURL = '';
const cmEditor = new CodeMirror(qs$('#content'), {
autofocus: true,
gutters: [ 'CodeMirror-linenumbers' ],
lineNumbers: true,
lineWrapping: true,
matchBrackets: true,
styleActiveLine: {
nonEmpty: true,
},
});
uBlockDashboard.patchCodeMirrorEditor(cmEditor);
vAPI.messaging.send('dom', { what: 'uiStyles' }).then(response => {
if ( typeof response !== 'object' || response === null ) { return; }
if ( getActualTheme(response.uiTheme) === 'dark' ) {
dom.cl.add('#content .cm-s-default', 'cm-s-night');
dom.cl.remove('#content .cm-s-default', 'cm-s-default');
}
});
// Convert resource URLs into clickable links to code viewer
cmEditor.addOverlay({
re: /\b(?:href|src)=["']([^"']+)["']/g,
match: null,
token: function(stream) {
if ( stream.sol() ) {
this.re.lastIndex = 0;
this.match = this.re.exec(stream.string);
}
if ( this.match === null ) {
stream.skipToEnd();
return null;
}
const end = this.re.lastIndex - 1;
const beg = end - this.match[1].length;
if ( stream.pos < beg ) {
stream.pos = beg;
return null;
}
if ( stream.pos < end ) {
stream.pos = end;
return 'href';
}
if ( stream.pos < this.re.lastIndex ) {
stream.pos = this.re.lastIndex;
this.match = this.re.exec(stream.string);
return null;
}
stream.skipToEnd();
return null;
},
});
urlToDocMap.set('', cmEditor.getDoc());
/******************************************************************************/
async function fetchResource(url) {
let response, text;
const fetchOptions = {
method: 'GET',
referrer: '',
};
if ( urlToDocMap.has(url) ) {
fetchOptions.cache = 'reload';
}
try {
response = await fetch(url, fetchOptions);
text = await response.text();
} catch(reason) {
text = String(reason);
}
let mime = response && response.headers.get('Content-Type') || '';
mime = mime.replace(/\s*;.*$/, '').trim();
const beautifierOptions = {
end_with_newline: true,
indent_size: 3,
js: {
max_preserve_newlines: 3,
}
};
switch ( mime ) {
case 'text/css':
text = beautifier.css(text, beautifierOptions);
break;
case 'text/html':
case 'application/xhtml+xml':
case 'application/xml':
case 'image/svg+xml':
text = beautifier.html(text, beautifierOptions);
break;
case 'text/javascript':
case 'application/javascript':
case 'application/x-javascript':
text = beautifier.js(text, beautifierOptions);
break;
case 'application/json':
text = beautifier.js(text, beautifierOptions);
break;
default:
break;
}
return { mime, text };
}
/******************************************************************************/
function addPastURLs(url) {
const list = qs$('#pastURLs');
let current;
for ( let i = 0; i < list.children.length; i++ ) {
const span = list.children[i];
dom.cl.remove(span, 'selected');
if ( span.textContent !== url ) { continue; }
current = span;
}
if ( url === '' ) { return; }
if ( current === undefined ) {
current = document.createElement('span');
current.textContent = url;
list.prepend(current);
}
dom.cl.add(current, 'selected');
}
/******************************************************************************/
function setInputURL(url) {
const input = qs$('#header input[type="url"]');
if ( url === input.value ) { return; }
dom.attr(input, 'value', url);
input.value = url;
}
/******************************************************************************/
async function setURL(resourceURL) {
// For convenience, remove potentially existing quotes around the URL
if ( /^(["']).+\1$/.test(resourceURL) ) {
resourceURL = resourceURL.slice(1, -1);
}
let afterURL;
if ( resourceURL !== '' ) {
try {
const url = new URL(resourceURL, currentURL || undefined);
url.hash = '';
afterURL = url.href;
} catch(ex) {
}
if ( afterURL === undefined ) { return; }
} else {
afterURL = '';
}
if ( afterURL !== '' && /^https?:\/\/./.test(afterURL) === false ) {
return;
}
if ( afterURL === currentURL ) {
if ( afterURL !== resourceURL ) {
setInputURL(afterURL);
}
return;
}
let afterDoc = urlToDocMap.get(afterURL);
if ( afterDoc === undefined ) {
const r = await fetchResource(afterURL) || { mime: '', text: '' };
afterDoc = new CodeMirror.Doc(r.text, r.mime || '');
urlToDocMap.set(afterURL, afterDoc);
}
swapDoc(afterDoc);
currentURL = afterURL;
setInputURL(afterURL);
const a = qs$('.cm-search-widget .sourceURL');
dom.attr(a, 'href', afterURL);
dom.attr(a, 'title', afterURL);
addPastURLs(afterURL);
// For unknown reasons, calling focus() synchronously does not work...
vAPI.defer.once(1).then(( ) => { cmEditor.focus(); });
}
/******************************************************************************/
function removeURL(url) {
if ( url === '' ) { return; }
const list = qs$('#pastURLs');
let foundAt = -1;
for ( let i = 0; i < list.children.length; i++ ) {
const span = list.children[i];
if ( span.textContent !== url ) { continue; }
foundAt = i;
}
if ( foundAt === -1 ) { return; }
list.children[foundAt].remove();
if ( foundAt >= list.children.length ) {
foundAt = list.children.length - 1;
}
const afterURL = foundAt !== -1
? list.children[foundAt].textContent
: '';
setURL(afterURL);
urlToDocMap.delete(url);
}
/******************************************************************************/
function swapDoc(doc) {
const r = cmEditor.swapDoc(doc);
if ( self.searchThread ) {
self.searchThread.setHaystack(cmEditor.getValue());
}
const input = qs$('.cm-search-widget-input input[type="search"]');
if ( input.value !== '' ) {
qs$('.cm-search-widget').dispatchEvent(new Event('input'));
}
return r;
}
/******************************************************************************/
async function start() {
await setURL(params.get('url'));
dom.on('#header input[type="url"]', 'change', ev => {
setURL(ev.target.value);
});
dom.on('#reloadURL', 'click', ( ) => {
const input = qs$('#header input[type="url"]');
const url = input.value;
const beforeDoc = swapDoc(new CodeMirror.Doc('', ''));
fetchResource(url).then(r => {
if ( urlToDocMap.has(url) === false ) { return; }
const afterDoc = r !== undefined
? new CodeMirror.Doc(r.text, r.mime || '')
: beforeDoc;
urlToDocMap.set(url, afterDoc);
if ( currentURL !== url ) { return; }
swapDoc(afterDoc);
});
});
dom.on('#removeURL', 'click', ( ) => {
removeURL(qs$('#header input[type="url"]').value);
});
dom.on('#pastURLs', 'mousedown', 'span', ev => {
setURL(ev.target.textContent);
});
dom.on('#content', 'click', '.cm-href', ev => {
const target = ev.target;
const urlParts = [ target.textContent ];
let previous = target;
for (;;) {
previous = previous.previousSibling;
if ( previous === null ) { break; }
if ( previous.nodeType !== 1 ) { break; }
if ( previous.classList.contains('cm-href') === false ) { break; }
urlParts.unshift(previous.textContent);
}
let next = target;
for (;;) {
next = next.nextSibling;
if ( next === null ) { break; }
if ( next.nodeType !== 1 ) { break; }
if ( next.classList.contains('cm-href') === false ) { break; }
urlParts.push(next.textContent);
}
setURL(urlParts.join(''));
});
}
start();
/******************************************************************************/
|