2014-12-31 23:26:17 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
µMatrix - a Chromium browser extension to block requests.
|
|
|
|
Copyright (C) 2014 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/uMatrix
|
|
|
|
*/
|
|
|
|
|
2015-01-06 18:14:37 +01:00
|
|
|
/* global vAPI, uDom */
|
2014-12-31 23:26:17 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var messager = vAPI.messaging.channel('dyna-rules.js');
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 17:34:51 +01:00
|
|
|
var renderRules = function(details) {
|
|
|
|
var rules, rule, i;
|
|
|
|
var permanentList = [];
|
|
|
|
var sessionList = [];
|
|
|
|
var allRules = {};
|
|
|
|
var permanentRules = {};
|
|
|
|
var sessionRules = {};
|
|
|
|
var onLeft, onRight;
|
|
|
|
|
|
|
|
rules = details.sessionRules.split(/\n+/);
|
|
|
|
i = rules.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
rule = rules[i].trim();
|
2015-02-11 17:49:08 +01:00
|
|
|
if ( rule === '' ) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-11 17:34:51 +01:00
|
|
|
sessionRules[rule] = allRules[rule] = true;
|
2014-12-31 23:26:17 +01:00
|
|
|
}
|
2015-02-11 17:34:51 +01:00
|
|
|
details.sessionRules = rules.sort().join('\n');
|
2014-12-31 23:26:17 +01:00
|
|
|
|
2015-02-11 17:34:51 +01:00
|
|
|
rules = details.permanentRules.split(/\n+/);
|
|
|
|
i = rules.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
rule = rules[i].trim();
|
2015-02-11 17:49:08 +01:00
|
|
|
if ( rule === '' ) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-11 17:34:51 +01:00
|
|
|
permanentRules[rule] = allRules[rule] = true;
|
|
|
|
}
|
|
|
|
details.permanentRules = rules.sort().join('\n');
|
|
|
|
|
|
|
|
rules = Object.keys(allRules).sort();
|
|
|
|
for ( i = 0; i < rules.length; i++ ) {
|
|
|
|
rule = rules[i];
|
|
|
|
onLeft = permanentRules.hasOwnProperty(rule);
|
|
|
|
onRight = sessionRules.hasOwnProperty(rule);
|
|
|
|
if ( onLeft && onRight ) {
|
|
|
|
permanentList.push('<li>', rule);
|
|
|
|
sessionList.push('<li>', rule);
|
|
|
|
} else if ( onLeft ) {
|
|
|
|
permanentList.push('<li>', rule);
|
|
|
|
sessionList.push('<li class="notRight toRemove">', rule);
|
|
|
|
} else {
|
|
|
|
permanentList.push('<li> ');
|
|
|
|
sessionList.push('<li class="notLeft">', rule);
|
|
|
|
}
|
|
|
|
}
|
2014-12-31 23:26:17 +01:00
|
|
|
|
2015-02-11 17:34:51 +01:00
|
|
|
uDom('#diff > .left > ul > li').remove();
|
|
|
|
uDom('#diff > .left > ul').html(permanentList.join(''));
|
|
|
|
uDom('#diff > .right > ul > li').remove();
|
|
|
|
uDom('#diff > .right > ul').html(sessionList.join(''));
|
|
|
|
uDom('#diff').toggleClass('dirty', details.sessionRules !== details.permanentRules);
|
2014-12-31 23:26:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function handleImportFilePicker() {
|
|
|
|
var fileReaderOnLoadHandler = function() {
|
|
|
|
if ( typeof this.result !== 'string' || this.result === '' ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-10 02:12:37 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/757
|
|
|
|
// Support RequestPolicy rule syntax
|
|
|
|
var result = this.result;
|
|
|
|
var matches = /\[origins-to-destinations\]([^\[]+)/.exec(result);
|
|
|
|
if ( matches && matches.length === 2 ) {
|
|
|
|
result = matches[1].trim()
|
|
|
|
.replace(/\|/g, ' ')
|
|
|
|
.replace(/\n/g, ' * noop\n');
|
|
|
|
}
|
2015-02-11 17:34:51 +01:00
|
|
|
var request = {
|
|
|
|
'what': 'setSessionFirewallRules',
|
|
|
|
'rules': rulesFromHTML('#diff .right li') + '\n' + result
|
|
|
|
};
|
|
|
|
messager.send(request, renderRules);
|
2014-12-31 23:26:17 +01:00
|
|
|
};
|
|
|
|
var file = this.files[0];
|
|
|
|
if ( file === undefined || file.name === '' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( file.type.indexOf('text') !== 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var fr = new FileReader();
|
|
|
|
fr.onload = fileReaderOnLoadHandler;
|
|
|
|
fr.readAsText(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var startImportFilePicker = function() {
|
|
|
|
var input = document.getElementById('importFilePicker');
|
|
|
|
// 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
|
|
|
|
// one picked.
|
|
|
|
input.value = '';
|
|
|
|
input.click();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function exportUserRulesToFile() {
|
2015-01-06 18:14:37 +01:00
|
|
|
var now = new Date();
|
|
|
|
var filename = vAPI.i18n('rulesDefaultFileName')
|
|
|
|
.replace('{{datetime}}', now.toLocaleString())
|
|
|
|
.replace(/ +/g, '_');
|
|
|
|
vAPI.download({
|
2015-02-11 17:34:51 +01:00
|
|
|
'url': 'data:text/plain,' + encodeURIComponent(rulesFromHTML('#diff .left li')),
|
2015-01-06 18:14:37 +01:00
|
|
|
'filename': filename,
|
2014-12-31 23:26:17 +01:00
|
|
|
'saveAs': true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 17:34:51 +01:00
|
|
|
var rulesFromHTML = function(selector) {
|
|
|
|
var rules = [];
|
|
|
|
var lis = uDom(selector);
|
|
|
|
var li;
|
|
|
|
for ( var i = 0; i < lis.length; i++ ) {
|
|
|
|
li = lis.at(i);
|
|
|
|
if ( li.hasClassName('toRemove') ) {
|
|
|
|
rules.push('');
|
|
|
|
} else {
|
|
|
|
rules.push(li.text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rules.join('\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var revertHandler = function() {
|
|
|
|
var request = {
|
|
|
|
'what': 'setSessionFirewallRules',
|
|
|
|
'rules': rulesFromHTML('#diff .left li')
|
|
|
|
};
|
|
|
|
messager.send(request, renderRules);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var commitHandler = function() {
|
|
|
|
var request = {
|
|
|
|
'what': 'setPermanentFirewallRules',
|
|
|
|
'rules': rulesFromHTML('#diff .right li')
|
|
|
|
};
|
|
|
|
messager.send(request, renderRules);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 17:54:05 +01:00
|
|
|
var editStartHandler = function() {
|
2015-02-11 17:34:51 +01:00
|
|
|
uDom('#diff .right textarea').val(rulesFromHTML('#diff .right li'));
|
|
|
|
var parent = uDom(this).ancestors('#diff');
|
|
|
|
parent.toggleClass('edit', true);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 17:54:05 +01:00
|
|
|
var editStopHandler = function() {
|
2015-02-11 17:34:51 +01:00
|
|
|
var parent = uDom(this).ancestors('#diff');
|
|
|
|
parent.toggleClass('edit', false);
|
|
|
|
var request = {
|
|
|
|
'what': 'setSessionFirewallRules',
|
|
|
|
'rules': uDom('#diff .right textarea').val()
|
|
|
|
};
|
|
|
|
messager.send(request, renderRules);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-11 17:54:05 +01:00
|
|
|
var editCancelHandler = function() {
|
2015-02-11 17:34:51 +01:00
|
|
|
var parent = uDom(this).ancestors('#diff');
|
|
|
|
parent.toggleClass('edit', false);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-31 23:26:17 +01:00
|
|
|
uDom.onLoad(function() {
|
|
|
|
// Handle user interaction
|
|
|
|
uDom('#importButton').on('click', startImportFilePicker);
|
|
|
|
uDom('#importFilePicker').on('change', handleImportFilePicker);
|
|
|
|
uDom('#exportButton').on('click', exportUserRulesToFile);
|
|
|
|
|
2015-02-11 17:54:05 +01:00
|
|
|
uDom('#revertButton').on('click', revertHandler);
|
|
|
|
uDom('#commitButton').on('click', commitHandler);
|
|
|
|
uDom('#editEnterButton').on('click', editStartHandler);
|
|
|
|
uDom('#editStopButton').on('click', editStopHandler);
|
|
|
|
uDom('#editCancelButton').on('click', editCancelHandler);
|
2015-02-11 17:34:51 +01:00
|
|
|
|
|
|
|
messager.send({ what: 'getFirewallRules' }, renderRules);
|
2014-12-31 23:26:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|