2023-03-01 17:04:40 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
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';
|
2023-03-12 16:20:54 +01:00
|
|
|
import { getActualTheme } from './theme.js';
|
2023-03-01 17:04:40 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-03-13 15:51:55 +01:00
|
|
|
const urlToDocMap = new Map();
|
2023-03-11 21:55:51 +01:00
|
|
|
const params = new URLSearchParams(document.location.search);
|
2023-03-13 15:51:55 +01:00
|
|
|
let currentURL = '';
|
2023-03-11 21:55:51 +01:00
|
|
|
|
|
|
|
const cmEditor = new CodeMirror(qs$('#content'), {
|
|
|
|
autofocus: true,
|
|
|
|
gutters: [ 'CodeMirror-linenumbers' ],
|
|
|
|
lineNumbers: true,
|
|
|
|
lineWrapping: true,
|
|
|
|
matchBrackets: true,
|
|
|
|
styleActiveLine: {
|
|
|
|
nonEmpty: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
uBlockDashboard.patchCodeMirrorEditor(cmEditor);
|
2023-03-12 16:20:54 +01:00
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
});
|
2023-03-11 21:55:51 +01:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
},
|
|
|
|
});
|
2023-03-08 18:31:39 +01:00
|
|
|
|
2023-03-13 15:51:55 +01:00
|
|
|
urlToDocMap.set('', cmEditor.getDoc());
|
|
|
|
|
2023-03-11 21:55:51 +01:00
|
|
|
/******************************************************************************/
|
2023-03-08 18:31:39 +01:00
|
|
|
|
2023-03-11 21:55:51 +01:00
|
|
|
async function fetchResource(url) {
|
|
|
|
let response, text;
|
2023-03-14 13:43:23 +01:00
|
|
|
const fetchOptions = {
|
|
|
|
method: 'GET',
|
|
|
|
referrer: '',
|
|
|
|
};
|
|
|
|
if ( urlToDocMap.has(url) ) {
|
|
|
|
fetchOptions.cache = 'reload';
|
|
|
|
}
|
2023-03-11 21:55:51 +01:00
|
|
|
try {
|
2023-03-14 13:43:23 +01:00
|
|
|
response = await fetch(url, fetchOptions);
|
2023-03-11 21:55:51 +01:00
|
|
|
text = await response.text();
|
|
|
|
} catch(reason) {
|
2023-03-14 13:58:52 +01:00
|
|
|
text = String(reason);
|
2023-03-11 21:55:51 +01:00
|
|
|
}
|
2023-03-14 13:58:52 +01:00
|
|
|
let mime = response && response.headers.get('Content-Type') || '';
|
2023-03-07 19:40:52 +01:00
|
|
|
mime = mime.replace(/\s*;.*$/, '').trim();
|
2023-03-14 13:43:23 +01:00
|
|
|
const beautifierOptions = {
|
2023-03-16 19:54:45 +01:00
|
|
|
end_with_newline: true,
|
|
|
|
indent_size: 3,
|
|
|
|
js: {
|
|
|
|
max_preserve_newlines: 3,
|
|
|
|
}
|
2023-03-13 15:51:55 +01:00
|
|
|
};
|
2023-03-07 19:40:52 +01:00
|
|
|
switch ( mime ) {
|
|
|
|
case 'text/css':
|
2023-03-14 13:43:23 +01:00
|
|
|
text = beautifier.css(text, beautifierOptions);
|
2023-03-01 17:04:40 +01:00
|
|
|
break;
|
2023-03-07 19:40:52 +01:00
|
|
|
case 'text/html':
|
|
|
|
case 'application/xhtml+xml':
|
|
|
|
case 'application/xml':
|
|
|
|
case 'image/svg+xml':
|
2023-03-14 13:43:23 +01:00
|
|
|
text = beautifier.html(text, beautifierOptions);
|
2023-03-01 17:04:40 +01:00
|
|
|
break;
|
2023-03-07 19:40:52 +01:00
|
|
|
case 'text/javascript':
|
|
|
|
case 'application/javascript':
|
|
|
|
case 'application/x-javascript':
|
2023-03-14 13:43:23 +01:00
|
|
|
text = beautifier.js(text, beautifierOptions);
|
2023-03-01 17:04:40 +01:00
|
|
|
break;
|
2023-03-07 19:40:52 +01:00
|
|
|
case 'application/json':
|
2023-03-14 13:43:23 +01:00
|
|
|
text = beautifier.js(text, beautifierOptions);
|
2023-03-07 19:40:52 +01:00
|
|
|
break;
|
2023-03-01 17:04:40 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-03-11 21:55:51 +01:00
|
|
|
return { mime, text };
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2023-03-08 18:31:39 +01:00
|
|
|
|
2023-03-13 15:51:55 +01:00
|
|
|
function addPastURLs(url) {
|
2023-03-11 21:55:51 +01:00
|
|
|
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;
|
2023-03-01 17:04:40 +01:00
|
|
|
}
|
2023-03-13 15:51:55 +01:00
|
|
|
if ( url === '' ) { return; }
|
2023-03-11 21:55:51 +01:00
|
|
|
if ( current === undefined ) {
|
|
|
|
current = document.createElement('span');
|
|
|
|
current.textContent = url;
|
|
|
|
list.prepend(current);
|
|
|
|
}
|
|
|
|
dom.cl.add(current, 'selected');
|
|
|
|
}
|
2023-03-08 18:31:39 +01:00
|
|
|
|
2023-03-11 21:55:51 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-03-13 15:51:55 +01:00
|
|
|
function setInputURL(url) {
|
|
|
|
const input = qs$('#header input[type="url"]');
|
|
|
|
if ( url === input.value ) { return; }
|
|
|
|
dom.attr(input, 'value', url);
|
|
|
|
input.value = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-03-11 21:55:51 +01:00
|
|
|
async function setURL(resourceURL) {
|
2023-03-12 22:47:21 +01:00
|
|
|
// For convenience, remove potentially existing quotes around the URL
|
|
|
|
if ( /^(["']).+\1$/.test(resourceURL) ) {
|
|
|
|
resourceURL = resourceURL.slice(1, -1);
|
|
|
|
}
|
2023-03-13 15:51:55 +01:00
|
|
|
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 = '';
|
2023-03-11 21:55:51 +01:00
|
|
|
}
|
2023-03-13 15:51:55 +01:00
|
|
|
if ( afterURL !== '' && /^https?:\/\/./.test(afterURL) === false ) {
|
|
|
|
return;
|
2023-03-11 21:55:51 +01:00
|
|
|
}
|
2023-03-13 15:51:55 +01:00
|
|
|
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 || '');
|
2023-03-14 13:43:23 +01:00
|
|
|
urlToDocMap.set(afterURL, afterDoc);
|
2023-03-13 15:51:55 +01:00
|
|
|
}
|
2023-03-16 17:36:02 +01:00
|
|
|
swapDoc(afterDoc);
|
2023-03-13 15:51:55 +01:00
|
|
|
currentURL = afterURL;
|
|
|
|
setInputURL(afterURL);
|
2023-03-11 21:55:51 +01:00
|
|
|
const a = qs$('.cm-search-widget .sourceURL');
|
2023-03-13 15:51:55 +01:00
|
|
|
dom.attr(a, 'href', afterURL);
|
|
|
|
dom.attr(a, 'title', afterURL);
|
|
|
|
addPastURLs(afterURL);
|
2023-03-12 22:47:21 +01:00
|
|
|
// For unknown reasons, calling focus() synchronously does not work...
|
2023-04-09 19:38:16 +02:00
|
|
|
vAPI.defer.once(1).then(( ) => { cmEditor.focus(); });
|
2023-03-11 21:55:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-03-13 15:51:55 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-03-16 17:36:02 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-03-14 13:43:23 +01:00
|
|
|
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;
|
2023-03-16 17:36:02 +01:00
|
|
|
const beforeDoc = swapDoc(new CodeMirror.Doc('', ''));
|
2023-03-14 13:43:23 +01:00
|
|
|
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; }
|
2023-03-16 17:36:02 +01:00
|
|
|
swapDoc(afterDoc);
|
2023-03-14 13:43:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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 => {
|
|
|
|
setURL(ev.target.textContent);
|
|
|
|
});
|
|
|
|
}
|
2023-03-13 15:51:55 +01:00
|
|
|
|
2023-03-14 13:43:23 +01:00
|
|
|
start();
|
2023-03-11 21:55:51 +01:00
|
|
|
|
2023-03-14 13:43:23 +01:00
|
|
|
/******************************************************************************/
|