2016-11-03 16:20:47 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-08-13 16:21:42 +02:00
|
|
|
Copyright (C) 2016-present Raymond Hill
|
2016-11-03 16:20:47 +01: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
|
|
|
|
*/
|
|
|
|
|
2018-08-13 16:21:42 +02:00
|
|
|
/* global CodeMirror, uDom, uBlockDashboard */
|
2016-11-03 16:20:47 +01:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-04-28 17:07:00 +02:00
|
|
|
{
|
2019-04-21 01:10:34 +02:00
|
|
|
// >>>> Start of private namespace
|
2016-11-03 16:20:47 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-04-28 17:07:00 +02:00
|
|
|
let defaultSettings = new Map();
|
2021-01-05 18:16:50 +01:00
|
|
|
let adminSettings = new Map();
|
2018-08-13 16:21:42 +02:00
|
|
|
let beforeHash = '';
|
|
|
|
|
2020-04-28 17:07:00 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
CodeMirror.defineMode('raw-settings', function() {
|
|
|
|
let lastSetting = '';
|
|
|
|
|
|
|
|
return {
|
|
|
|
token: function(stream) {
|
|
|
|
if ( stream.sol() ) {
|
2020-04-29 00:39:31 +02:00
|
|
|
stream.eatSpace();
|
|
|
|
const match = stream.match(/\S+/);
|
|
|
|
if ( match !== null && defaultSettings.has(match[0]) ) {
|
|
|
|
lastSetting = match[0];
|
2021-01-05 18:16:50 +01:00
|
|
|
return adminSettings.has(match[0])
|
|
|
|
? 'readonly keyword'
|
|
|
|
: 'keyword';
|
2020-04-28 17:07:00 +02:00
|
|
|
}
|
|
|
|
stream.skipToEnd();
|
2020-04-29 00:39:31 +02:00
|
|
|
return 'line-cm-error';
|
2020-04-28 17:07:00 +02:00
|
|
|
}
|
2020-04-29 00:39:31 +02:00
|
|
|
stream.eatSpace();
|
|
|
|
const match = stream.match(/.*$/);
|
2021-01-05 18:16:50 +01:00
|
|
|
if ( match !== null ) {
|
|
|
|
if ( match[0].trim() !== defaultSettings.get(lastSetting) ) {
|
|
|
|
return 'line-cm-strong';
|
|
|
|
}
|
|
|
|
if ( adminSettings.has(lastSetting) ) {
|
|
|
|
return 'readonly';
|
|
|
|
}
|
2020-04-28 17:07:00 +02:00
|
|
|
}
|
|
|
|
stream.skipToEnd();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2019-04-21 01:10:34 +02:00
|
|
|
const cmEditor = new CodeMirror(
|
2018-08-13 16:21:42 +02:00
|
|
|
document.getElementById('advancedSettings'),
|
|
|
|
{
|
|
|
|
autofocus: true,
|
|
|
|
lineNumbers: true,
|
2020-04-25 16:19:08 +02:00
|
|
|
lineWrapping: false,
|
2018-08-13 16:21:42 +02:00
|
|
|
styleActiveLine: true
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
uBlockDashboard.patchCodeMirrorEditor(cmEditor);
|
2016-11-03 16:20:47 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-04-21 01:10:34 +02:00
|
|
|
const hashFromAdvancedSettings = function(raw) {
|
2020-04-28 17:07:00 +02:00
|
|
|
const aa = typeof raw === 'string'
|
|
|
|
? arrayFromString(raw)
|
|
|
|
: arrayFromObject(raw);
|
|
|
|
aa.sort((a, b) => a[0].localeCompare(b[0]));
|
|
|
|
return JSON.stringify(aa);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
const arrayFromObject = function(o) {
|
|
|
|
const out = [];
|
|
|
|
for ( const k in o ) {
|
|
|
|
if ( o.hasOwnProperty(k) === false ) { continue; }
|
|
|
|
out.push([ k, `${o[k]}` ]);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
const arrayFromString = function(s) {
|
|
|
|
const out = [];
|
|
|
|
for ( let line of s.split(/[\n\r]+/) ) {
|
|
|
|
line = line.trim();
|
|
|
|
if ( line === '' ) { continue; }
|
|
|
|
const pos = line.indexOf(' ');
|
|
|
|
let k, v;
|
|
|
|
if ( pos !== -1 ) {
|
|
|
|
k = line.slice(0, pos);
|
|
|
|
v = line.slice(pos + 1);
|
|
|
|
} else {
|
|
|
|
k = line;
|
|
|
|
v = '';
|
|
|
|
}
|
|
|
|
out.push([ k.trim(), v.trim() ]);
|
|
|
|
}
|
|
|
|
return out;
|
2016-11-03 16:20:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// This is to give a visual hint that the content of user blacklist has changed.
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const advancedSettingsChanged = (( ) => {
|
|
|
|
let timer;
|
2016-11-03 16:20:47 +01:00
|
|
|
|
2019-04-21 01:10:34 +02:00
|
|
|
const handler = ( ) => {
|
2019-09-17 21:15:01 +02:00
|
|
|
timer = undefined;
|
|
|
|
const changed =
|
|
|
|
hashFromAdvancedSettings(cmEditor.getValue()) !== beforeHash;
|
2016-11-03 16:20:47 +01:00
|
|
|
uDom.nodeFromId('advancedSettingsApply').disabled = !changed;
|
2020-04-28 17:07:00 +02:00
|
|
|
CodeMirror.commands.save = changed ? applyChanges : function(){};
|
2016-11-03 16:20:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return function() {
|
2019-09-17 21:15:01 +02:00
|
|
|
if ( timer !== undefined ) { clearTimeout(timer); }
|
2016-11-03 16:20:47 +01:00
|
|
|
timer = vAPI.setTimeout(handler, 100);
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2018-08-13 16:21:42 +02:00
|
|
|
cmEditor.on('changes', advancedSettingsChanged);
|
|
|
|
|
2016-11-03 16:20:47 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const renderAdvancedSettings = async function(first) {
|
2020-04-28 17:07:00 +02:00
|
|
|
const details = await vAPI.messaging.send('dashboard', {
|
2019-09-17 21:15:01 +02:00
|
|
|
what: 'readHiddenSettings',
|
|
|
|
});
|
2020-04-28 17:07:00 +02:00
|
|
|
defaultSettings = new Map(arrayFromObject(details.default));
|
2021-01-05 18:16:50 +01:00
|
|
|
adminSettings = new Map(arrayFromObject(details.admin));
|
2020-04-28 17:07:00 +02:00
|
|
|
beforeHash = hashFromAdvancedSettings(details.current);
|
2020-01-23 18:06:40 +01:00
|
|
|
const pretty = [];
|
2021-01-05 18:16:50 +01:00
|
|
|
const roLines = [];
|
2020-04-28 17:07:00 +02:00
|
|
|
const entries = arrayFromObject(details.current);
|
2020-01-23 18:06:40 +01:00
|
|
|
let max = 0;
|
2020-04-28 17:07:00 +02:00
|
|
|
for ( const [ k ] of entries ) {
|
|
|
|
if ( k.length > max ) { max = k.length; }
|
2019-09-17 21:15:01 +02:00
|
|
|
}
|
2021-01-05 18:16:50 +01:00
|
|
|
for ( let i = 0; i < entries.length; i++ ) {
|
|
|
|
const [ k, v ] = entries[i];
|
2020-04-28 17:07:00 +02:00
|
|
|
pretty.push(' '.repeat(max - k.length) + `${k} ${v}`);
|
2021-01-05 18:16:50 +01:00
|
|
|
if ( adminSettings.has(k) ) {
|
|
|
|
roLines.push(i);
|
|
|
|
}
|
2019-09-17 21:15:01 +02:00
|
|
|
}
|
2020-01-23 18:06:40 +01:00
|
|
|
pretty.push('');
|
|
|
|
cmEditor.setValue(pretty.join('\n'));
|
2019-09-17 21:15:01 +02:00
|
|
|
if ( first ) {
|
|
|
|
cmEditor.clearHistory();
|
|
|
|
}
|
2021-01-05 18:16:50 +01:00
|
|
|
for ( const line of roLines ) {
|
|
|
|
cmEditor.markText(
|
|
|
|
{ line, ch: 0 },
|
|
|
|
{ line: line + 1, ch: 0 },
|
|
|
|
{ readOnly: true }
|
|
|
|
);
|
|
|
|
}
|
2019-09-17 21:15:01 +02:00
|
|
|
advancedSettingsChanged();
|
|
|
|
cmEditor.focus();
|
2018-08-13 16:21:42 +02:00
|
|
|
};
|
2016-11-03 16:20:47 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const applyChanges = async function() {
|
|
|
|
await vAPI.messaging.send('dashboard', {
|
|
|
|
what: 'writeHiddenSettings',
|
|
|
|
content: cmEditor.getValue(),
|
|
|
|
});
|
|
|
|
renderAdvancedSettings();
|
2016-11-03 16:20:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-08-13 16:21:42 +02:00
|
|
|
uDom.nodeFromId('advancedSettings').addEventListener(
|
|
|
|
'input',
|
|
|
|
advancedSettingsChanged
|
|
|
|
);
|
2019-09-17 21:15:01 +02:00
|
|
|
uDom.nodeFromId('advancedSettingsApply').addEventListener('click', ( ) => {
|
|
|
|
applyChanges();
|
|
|
|
});
|
2016-11-03 16:20:47 +01:00
|
|
|
|
2018-08-13 16:21:42 +02:00
|
|
|
renderAdvancedSettings(true);
|
2016-11-03 16:20:47 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-04-21 01:10:34 +02:00
|
|
|
// <<<< End of private namespace
|
2020-04-28 17:07:00 +02:00
|
|
|
}
|