2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-10-13 19:25:57 +02:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-12-22 19:35:46 +01:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2014-10-19 13:11:27 +02:00
|
|
|
'use strict';
|
|
|
|
|
2022-11-12 15:51:22 +01:00
|
|
|
import { dom } from './dom.js';
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-08-12 18:17:39 +02:00
|
|
|
self.uBlockDashboard = self.uBlockDashboard || {};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Helper for client panes:
|
|
|
|
// Remove literal duplicate lines from a set based on another set.
|
|
|
|
|
2015-08-15 15:24:55 +02:00
|
|
|
self.uBlockDashboard.mergeNewLines = function(text, newText) {
|
2015-08-12 18:17:39 +02:00
|
|
|
// Step 1: build dictionary for existing lines.
|
2019-04-21 00:57:16 +02:00
|
|
|
const fromDict = new Map();
|
2018-12-22 19:35:46 +01:00
|
|
|
let lineBeg = 0;
|
|
|
|
let textEnd = text.length;
|
2015-08-12 18:17:39 +02:00
|
|
|
while ( lineBeg < textEnd ) {
|
2018-12-22 19:35:46 +01:00
|
|
|
let lineEnd = text.indexOf('\n', lineBeg);
|
2015-08-12 18:17:39 +02:00
|
|
|
if ( lineEnd === -1 ) {
|
|
|
|
lineEnd = text.indexOf('\r', lineBeg);
|
|
|
|
if ( lineEnd === -1 ) {
|
|
|
|
lineEnd = textEnd;
|
|
|
|
}
|
|
|
|
}
|
2019-04-21 00:57:16 +02:00
|
|
|
const line = text.slice(lineBeg, lineEnd).trim();
|
2015-08-12 18:17:39 +02:00
|
|
|
lineBeg = lineEnd + 1;
|
2019-04-21 00:57:16 +02:00
|
|
|
if ( line.length === 0 ) { continue; }
|
2018-12-22 19:35:46 +01:00
|
|
|
const hash = line.slice(0, 8);
|
2019-04-21 00:57:16 +02:00
|
|
|
const bucket = fromDict.get(hash);
|
2015-08-12 18:17:39 +02:00
|
|
|
if ( bucket === undefined ) {
|
2019-04-21 00:57:16 +02:00
|
|
|
fromDict.set(hash, line);
|
2015-08-12 18:17:39 +02:00
|
|
|
} else if ( typeof bucket === 'string' ) {
|
2019-04-21 00:57:16 +02:00
|
|
|
fromDict.set(hash, [ bucket, line ]);
|
2015-08-12 18:17:39 +02:00
|
|
|
} else /* if ( Array.isArray(bucket) ) */ {
|
|
|
|
bucket.push(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: use above dictionary to filter out duplicate lines.
|
2018-12-22 19:35:46 +01:00
|
|
|
const out = [ '' ];
|
2015-08-12 18:17:39 +02:00
|
|
|
lineBeg = 0;
|
|
|
|
textEnd = newText.length;
|
|
|
|
while ( lineBeg < textEnd ) {
|
2018-12-22 19:35:46 +01:00
|
|
|
let lineEnd = newText.indexOf('\n', lineBeg);
|
2015-08-12 18:17:39 +02:00
|
|
|
if ( lineEnd === -1 ) {
|
|
|
|
lineEnd = newText.indexOf('\r', lineBeg);
|
|
|
|
if ( lineEnd === -1 ) {
|
|
|
|
lineEnd = textEnd;
|
|
|
|
}
|
|
|
|
}
|
2019-04-21 00:57:16 +02:00
|
|
|
const line = newText.slice(lineBeg, lineEnd).trim();
|
2015-08-12 18:17:39 +02:00
|
|
|
lineBeg = lineEnd + 1;
|
|
|
|
if ( line.length === 0 ) {
|
|
|
|
if ( out[out.length - 1] !== '' ) {
|
|
|
|
out.push('');
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-21 00:57:16 +02:00
|
|
|
const bucket = fromDict.get(line.slice(0, 8));
|
2015-08-12 18:17:39 +02:00
|
|
|
if ( bucket === undefined ) {
|
|
|
|
out.push(line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( typeof bucket === 'string' && line !== bucket ) {
|
|
|
|
out.push(line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( bucket.indexOf(line) === -1 ) {
|
|
|
|
out.push(line);
|
|
|
|
/* continue; */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 00:57:16 +02:00
|
|
|
const append = out.join('\n').trim();
|
|
|
|
if ( text !== '' && append !== '' ) {
|
|
|
|
text += '\n\n';
|
|
|
|
}
|
|
|
|
return text + append;
|
2015-08-12 18:17:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-10-13 19:25:57 +02:00
|
|
|
self.uBlockDashboard.dateNowToSensibleString = function() {
|
2018-12-22 19:35:46 +01:00
|
|
|
const now = new Date(Date.now() - (new Date()).getTimezoneOffset() * 60000);
|
2016-10-13 19:25:57 +02:00
|
|
|
return now.toISOString().replace(/\.\d+Z$/, '')
|
|
|
|
.replace(/:/g, '.')
|
|
|
|
.replace('T', '_');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-03-28 01:10:31 +02:00
|
|
|
self.uBlockDashboard.patchCodeMirrorEditor = (function() {
|
2018-12-22 19:35:46 +01:00
|
|
|
let grabFocusTarget;
|
|
|
|
|
|
|
|
const grabFocus = function() {
|
2018-03-28 15:11:55 +02:00
|
|
|
grabFocusTarget.focus();
|
2023-04-09 19:38:16 +02:00
|
|
|
grabFocusTarget = undefined;
|
2018-03-28 15:11:55 +02:00
|
|
|
};
|
2023-04-09 19:38:16 +02:00
|
|
|
|
|
|
|
const grabFocusTimer = vAPI.defer.create(grabFocus);
|
|
|
|
|
2018-12-22 19:35:46 +01:00
|
|
|
const grabFocusAsync = function(cm) {
|
2018-03-28 15:11:55 +02:00
|
|
|
grabFocusTarget = cm;
|
2023-04-09 19:38:16 +02:00
|
|
|
grabFocusTimer.on(1);
|
2018-03-28 15:11:55 +02:00
|
|
|
};
|
|
|
|
|
2018-03-28 01:10:31 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3646
|
2018-12-22 19:35:46 +01:00
|
|
|
const patchSelectAll = function(cm, details) {
|
2018-03-28 01:10:31 +02:00
|
|
|
var vp = cm.getViewport();
|
|
|
|
if ( details.ranges.length !== 1 ) { return; }
|
|
|
|
var range = details.ranges[0],
|
|
|
|
lineFrom = range.anchor.line,
|
|
|
|
lineTo = range.head.line;
|
2018-03-28 15:43:48 +02:00
|
|
|
if ( lineTo === lineFrom ) { return; }
|
2018-03-28 01:10:31 +02:00
|
|
|
if ( range.head.ch !== 0 ) { lineTo += 1; }
|
|
|
|
if ( lineFrom !== vp.from || lineTo !== vp.to ) { return; }
|
|
|
|
details.update([
|
|
|
|
{
|
|
|
|
anchor: { line: 0, ch: 0 },
|
|
|
|
head: { line: cm.lineCount(), ch: 0 }
|
|
|
|
}
|
|
|
|
]);
|
2018-03-28 15:11:55 +02:00
|
|
|
grabFocusAsync(cm);
|
|
|
|
};
|
|
|
|
|
2018-12-22 19:35:46 +01:00
|
|
|
let lastGutterClick = 0;
|
|
|
|
let lastGutterLine = 0;
|
2018-03-28 15:11:55 +02:00
|
|
|
|
2020-07-10 14:01:39 +02:00
|
|
|
const onGutterClicked = function(cm, line, gutter) {
|
|
|
|
if ( gutter !== 'CodeMirror-linenumbers' ) { return; }
|
|
|
|
grabFocusAsync(cm);
|
2018-12-22 19:35:46 +01:00
|
|
|
const delta = Date.now() - lastGutterClick;
|
2020-07-10 14:01:39 +02:00
|
|
|
// Single click
|
2018-03-28 15:11:55 +02:00
|
|
|
if ( delta >= 500 || line !== lastGutterLine ) {
|
|
|
|
cm.setSelection(
|
2020-07-10 14:01:39 +02:00
|
|
|
{ line, ch: 0 },
|
2018-03-28 15:11:55 +02:00
|
|
|
{ line: line + 1, ch: 0 }
|
|
|
|
);
|
|
|
|
lastGutterClick = Date.now();
|
|
|
|
lastGutterLine = line;
|
2020-07-10 14:01:39 +02:00
|
|
|
return;
|
2018-03-28 15:11:55 +02:00
|
|
|
}
|
2020-07-10 14:01:39 +02:00
|
|
|
// Double click: select fold-able block or all
|
|
|
|
let lineFrom = 0;
|
|
|
|
let lineTo = cm.lineCount();
|
|
|
|
const foldFn = cm.getHelper({ line, ch: 0 }, 'fold');
|
|
|
|
if ( foldFn instanceof Function ) {
|
|
|
|
const range = foldFn(cm, { line, ch: 0 });
|
|
|
|
if ( range !== undefined ) {
|
|
|
|
lineFrom = range.from.line;
|
|
|
|
lineTo = range.to.line + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cm.setSelection(
|
|
|
|
{ line: lineFrom, ch: 0 },
|
|
|
|
{ line: lineTo, ch: 0 },
|
|
|
|
{ scroll: false }
|
|
|
|
);
|
|
|
|
lastGutterClick = 0;
|
2018-03-28 01:10:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(cm) {
|
|
|
|
if ( cm.options.inputStyle === 'contenteditable' ) {
|
|
|
|
cm.on('beforeSelectionChange', patchSelectAll);
|
|
|
|
}
|
2018-03-28 15:11:55 +02:00
|
|
|
cm.on('gutterClick', onGutterClicked);
|
2018-03-28 01:10:31 +02:00
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-05-25 14:31:06 +02:00
|
|
|
self.uBlockDashboard.openOrSelectPage = function(url, options = {}) {
|
|
|
|
let ev;
|
|
|
|
if ( url instanceof MouseEvent ) {
|
|
|
|
ev = url;
|
2022-11-12 15:51:22 +01:00
|
|
|
url = dom.attr(ev.target, 'href');
|
2019-05-25 14:31:06 +02:00
|
|
|
}
|
|
|
|
const details = Object.assign({ url, select: true, index: -1 }, options);
|
2019-09-17 21:15:01 +02:00
|
|
|
vAPI.messaging.send('default', {
|
|
|
|
what: 'gotoURL',
|
|
|
|
details,
|
|
|
|
});
|
2019-05-25 14:31:06 +02:00
|
|
|
if ( ev ) {
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-08-15 15:24:55 +02:00
|
|
|
// Open links in the proper window
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.attr('a', 'target', '_blank');
|
|
|
|
dom.attr('a[href*="dashboard.html"]', 'target', '_parent');
|