mirror of https://github.com/gorhill/uBlock.git
Remember cursor position in "My filters" for next visit
Related issue: - https://github.com/gorhill/uBlock/issues/3706
This commit is contained in:
parent
2be36ffe81
commit
cd597709bb
|
@ -29,10 +29,8 @@
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var messaging = vAPI.messaging;
|
const messaging = vAPI.messaging;
|
||||||
var cachedUserFilters = '';
|
const cmEditor = new CodeMirror(
|
||||||
|
|
||||||
var cmEditor = new CodeMirror(
|
|
||||||
document.getElementById('userFilters'),
|
document.getElementById('userFilters'),
|
||||||
{
|
{
|
||||||
autofocus: true,
|
autofocus: true,
|
||||||
|
@ -44,24 +42,41 @@ var cmEditor = new CodeMirror(
|
||||||
|
|
||||||
uBlockDashboard.patchCodeMirrorEditor(cmEditor);
|
uBlockDashboard.patchCodeMirrorEditor(cmEditor);
|
||||||
|
|
||||||
|
let cachedUserFilters = '';
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
// https://github.com/gorhill/uBlock/issues/3706
|
||||||
|
// Save/restore cursor position
|
||||||
|
//
|
||||||
|
// CoreMirror reference: https://codemirror.net/doc/manual.html#api_selection
|
||||||
|
|
||||||
|
window.addEventListener('beforeunload', ( ) => {
|
||||||
|
vAPI.localStorage.setItem(
|
||||||
|
'myFiltersCursorPosition',
|
||||||
|
JSON.stringify(cmEditor.getCursor().line)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
// This is to give a visual hint that the content of user blacklist has changed.
|
// This is to give a visual hint that the content of user blacklist has changed.
|
||||||
|
|
||||||
function userFiltersChanged(changed) {
|
const userFiltersChanged = function(changed) {
|
||||||
if ( typeof changed !== 'boolean' ) {
|
if ( typeof changed !== 'boolean' ) {
|
||||||
changed = cmEditor.getValue().trim() !== cachedUserFilters;
|
changed = cmEditor.getValue().trim() !== cachedUserFilters;
|
||||||
}
|
}
|
||||||
uDom.nodeFromId('userFiltersApply').disabled = !changed;
|
uDom.nodeFromId('userFiltersApply').disabled = !changed;
|
||||||
uDom.nodeFromId('userFiltersRevert').disabled = !changed;
|
uDom.nodeFromId('userFiltersRevert').disabled = !changed;
|
||||||
}
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
function renderUserFilters(first) {
|
const renderUserFilters = function(first) {
|
||||||
var onRead = function(details) {
|
const onRead = function(details) {
|
||||||
if ( details.error ) { return; }
|
if ( details.error ) { return; }
|
||||||
var content = details.content.trim();
|
let content = details.content.trim();
|
||||||
cachedUserFilters = content;
|
cachedUserFilters = content;
|
||||||
if ( content.length !== 0 ) {
|
if ( content.length !== 0 ) {
|
||||||
content += '\n';
|
content += '\n';
|
||||||
|
@ -69,38 +84,44 @@ function renderUserFilters(first) {
|
||||||
cmEditor.setValue(content);
|
cmEditor.setValue(content);
|
||||||
if ( first ) {
|
if ( first ) {
|
||||||
cmEditor.clearHistory();
|
cmEditor.clearHistory();
|
||||||
|
try {
|
||||||
|
const line = JSON.parse(
|
||||||
|
vAPI.localStorage.getItem('myFiltersCursorPosition')
|
||||||
|
);
|
||||||
|
if ( typeof line === 'number' ) {
|
||||||
|
cmEditor.setCursor(line, 0);
|
||||||
|
}
|
||||||
|
} catch(ex) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
userFiltersChanged(false);
|
userFiltersChanged(false);
|
||||||
};
|
};
|
||||||
messaging.send('dashboard', { what: 'readUserFilters' }, onRead);
|
messaging.send('dashboard', { what: 'readUserFilters' }, onRead);
|
||||||
}
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
function allFiltersApplyHandler() {
|
const allFiltersApplyHandler = function() {
|
||||||
messaging.send('dashboard', { what: 'reloadAllFilters' });
|
messaging.send('dashboard', { what: 'reloadAllFilters' });
|
||||||
uDom('#userFiltersApply').prop('disabled', true );
|
uDom('#userFiltersApply').prop('disabled', true );
|
||||||
}
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var handleImportFilePicker = function() {
|
const handleImportFilePicker = function() {
|
||||||
// https://github.com/chrisaljoudi/uBlock/issues/1004
|
// https://github.com/chrisaljoudi/uBlock/issues/1004
|
||||||
// Support extraction of filters from ABP backup file
|
// Support extraction of filters from ABP backup file
|
||||||
var abpImporter = function(s) {
|
const abpImporter = function(s) {
|
||||||
var reAbpSubscriptionExtractor = /\n\[Subscription\]\n+url=~[^\n]+([\x08-\x7E]*?)(?:\[Subscription\]|$)/ig;
|
const reAbpSubscriptionExtractor = /\n\[Subscription\]\n+url=~[^\n]+([\x08-\x7E]*?)(?:\[Subscription\]|$)/ig;
|
||||||
var reAbpFilterExtractor = /\[Subscription filters\]([\x08-\x7E]*?)(?:\[Subscription\]|$)/i;
|
const reAbpFilterExtractor = /\[Subscription filters\]([\x08-\x7E]*?)(?:\[Subscription\]|$)/i;
|
||||||
var matches = reAbpSubscriptionExtractor.exec(s);
|
let matches = reAbpSubscriptionExtractor.exec(s);
|
||||||
// Not an ABP backup file
|
// Not an ABP backup file
|
||||||
if ( matches === null ) {
|
if ( matches === null ) { return s; }
|
||||||
return s;
|
|
||||||
}
|
|
||||||
//
|
//
|
||||||
var out = [];
|
const out = [];
|
||||||
var filterMatch;
|
|
||||||
while ( matches !== null ) {
|
while ( matches !== null ) {
|
||||||
if ( matches.length === 2 ) {
|
if ( matches.length === 2 ) {
|
||||||
filterMatch = reAbpFilterExtractor.exec(matches[1].trim());
|
let filterMatch = reAbpFilterExtractor.exec(matches[1].trim());
|
||||||
if ( filterMatch !== null && filterMatch.length === 2 ) {
|
if ( filterMatch !== null && filterMatch.length === 2 ) {
|
||||||
out.push(filterMatch[1].trim().replace(/\\\[/g, '['));
|
out.push(filterMatch[1].trim().replace(/\\\[/g, '['));
|
||||||
}
|
}
|
||||||
|
@ -110,26 +131,22 @@ var handleImportFilePicker = function() {
|
||||||
return out.join('\n');
|
return out.join('\n');
|
||||||
};
|
};
|
||||||
|
|
||||||
var fileReaderOnLoadHandler = function() {
|
const fileReaderOnLoadHandler = function() {
|
||||||
var sanitized = abpImporter(this.result);
|
const sanitized = abpImporter(this.result);
|
||||||
cmEditor.setValue(cmEditor.getValue().trim() + '\n' + sanitized);
|
cmEditor.setValue(cmEditor.getValue().trim() + '\n' + sanitized);
|
||||||
};
|
};
|
||||||
var file = this.files[0];
|
const file = this.files[0];
|
||||||
if ( file === undefined || file.name === '' ) {
|
if ( file === undefined || file.name === '' ) { return; }
|
||||||
return;
|
if ( file.type.indexOf('text') !== 0 ) { return; }
|
||||||
}
|
const fr = new FileReader();
|
||||||
if ( file.type.indexOf('text') !== 0 ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var fr = new FileReader();
|
|
||||||
fr.onload = fileReaderOnLoadHandler;
|
fr.onload = fileReaderOnLoadHandler;
|
||||||
fr.readAsText(file);
|
fr.readAsText(file);
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var startImportFilePicker = function() {
|
const startImportFilePicker = function() {
|
||||||
var input = document.getElementById('importFilePicker');
|
const input = document.getElementById('importFilePicker');
|
||||||
// Reset to empty string, this will ensure an change event is properly
|
// Reset to empty string, this will ensure an change event is properly
|
||||||
// triggered if the user pick a file, even if it is the same as the last
|
// triggered if the user pick a file, even if it is the same as the last
|
||||||
// one picked.
|
// one picked.
|
||||||
|
@ -139,10 +156,10 @@ var startImportFilePicker = function() {
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var exportUserFiltersToFile = function() {
|
const exportUserFiltersToFile = function() {
|
||||||
var val = cmEditor.getValue().trim();
|
const val = cmEditor.getValue().trim();
|
||||||
if ( val === '' ) { return; }
|
if ( val === '' ) { return; }
|
||||||
var filename = vAPI.i18n('1pExportFilename')
|
const filename = vAPI.i18n('1pExportFilename')
|
||||||
.replace('{{datetime}}', uBlockDashboard.dateNowToSensibleString())
|
.replace('{{datetime}}', uBlockDashboard.dateNowToSensibleString())
|
||||||
.replace(/ +/g, '_');
|
.replace(/ +/g, '_');
|
||||||
vAPI.download({
|
vAPI.download({
|
||||||
|
@ -153,24 +170,23 @@ var exportUserFiltersToFile = function() {
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var applyChanges = function() {
|
const applyChanges = function() {
|
||||||
var onWritten = function(details) {
|
|
||||||
if ( details.error ) { return; }
|
|
||||||
cachedUserFilters = details.content.trim();
|
|
||||||
allFiltersApplyHandler();
|
|
||||||
};
|
|
||||||
messaging.send(
|
messaging.send(
|
||||||
'dashboard',
|
'dashboard',
|
||||||
{
|
{
|
||||||
what: 'writeUserFilters',
|
what: 'writeUserFilters',
|
||||||
content: cmEditor.getValue()
|
content: cmEditor.getValue()
|
||||||
},
|
},
|
||||||
onWritten
|
details => {
|
||||||
|
if ( details.error ) { return; }
|
||||||
|
cachedUserFilters = details.content.trim();
|
||||||
|
allFiltersApplyHandler();
|
||||||
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
var revertChanges = function() {
|
const revertChanges = function() {
|
||||||
var content = cachedUserFilters;
|
let content = cachedUserFilters;
|
||||||
if ( content.length !== 0 ) {
|
if ( content.length !== 0 ) {
|
||||||
content += '\n';
|
content += '\n';
|
||||||
}
|
}
|
||||||
|
@ -179,11 +195,11 @@ var revertChanges = function() {
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var getCloudData = function() {
|
const getCloudData = function() {
|
||||||
return cmEditor.getValue();
|
return cmEditor.getValue();
|
||||||
};
|
};
|
||||||
|
|
||||||
var setCloudData = function(data, append) {
|
const setCloudData = function(data, append) {
|
||||||
if ( typeof data !== 'string' ) { return; }
|
if ( typeof data !== 'string' ) { return; }
|
||||||
if ( append ) {
|
if ( append ) {
|
||||||
data = uBlockDashboard.mergeNewLines(cmEditor.getValue(), data);
|
data = uBlockDashboard.mergeNewLines(cmEditor.getValue(), data);
|
||||||
|
@ -210,6 +226,4 @@ CodeMirror.commands.save = applyChanges;
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
// https://www.youtube.com/watch?v=UNilsLf6eW4
|
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in New Issue