1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-18 00:13:30 +01:00
uBlock/src/js/dyna-rules.js

257 lines
8.1 KiB
JavaScript
Raw Normal View History

/*******************************************************************************
2015-03-07 19:20:18 +01:00
µMatrix - a 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 */
/******************************************************************************/
(function() {
'use strict';
/******************************************************************************/
var messager = vAPI.messaging.channel('dyna-rules.js');
/******************************************************************************/
2015-02-11 17:34:51 +01:00
var renderRules = function(details) {
var liTemplate = uDom('#templates > ul > li');
var ulLeft = uDom('#diff > .left ul').empty().remove();
var ulRight = uDom('#diff > .right ul').empty().remove();
var liLeft, liRight;
2015-02-11 17:34:51 +01:00
var rules, rule, i;
// Switches always displayed first -- just like in uMatrix
2015-05-21 20:15:17 +02:00
// Merge url rules and switches: they just look the same
rules = details.hnSwitches.split(/\n+/).sort();
2015-05-21 20:15:17 +02:00
for ( i = 0; i < rules.length; i++ ) {
rule = rules[i];
liLeft = liTemplate.clone().text(rule);
liRight = liTemplate.clone().text(rule);
ulLeft.append(liLeft);
ulRight.append(liRight);
}
// Firewall rules follow
2015-02-11 17:34:51 +01:00
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;
}
2015-02-11 17:34:51 +01:00
details.sessionRules = rules.sort().join('\n');
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);
2015-03-09 05:21:08 +01:00
liLeft = liTemplate.clone();
liRight = liTemplate.clone();
2015-02-11 17:34:51 +01:00
if ( onLeft && onRight ) {
2015-03-09 05:21:08 +01:00
liLeft.text(rule);
liRight.text(rule);
2015-02-11 17:34:51 +01:00
} else if ( onLeft ) {
2015-03-09 05:21:08 +01:00
liLeft.text(rule);
liRight.text(rule).addClass('notRight toRemove');
2015-02-11 17:34:51 +01:00
} else {
2015-03-09 05:21:08 +01:00
liRight.text(rule).addClass('notLeft');
2015-02-11 17:34:51 +01:00
}
2015-03-09 05:21:08 +01:00
ulLeft.append(liLeft);
ulRight.append(liRight);
2015-02-11 17:34:51 +01:00
}
uDom('#diff > .left > .rulesContainer').append(ulLeft);
uDom('#diff > .right > .rulesContainer').append(ulRight);
2015-02-11 17:34:51 +01:00
uDom('#diff').toggleClass('dirty', details.sessionRules !== details.permanentRules);
};
/******************************************************************************/
function handleImportFilePicker() {
var fileReaderOnLoadHandler = function() {
if ( typeof this.result !== 'string' || this.result === '' ) {
return;
}
2015-04-07 03:26:05 +02:00
// https://github.com/chrisaljoudi/uBlock/issues/757
2015-02-10 02:12:37 +01:00
// 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 = {
2015-05-21 20:15:17 +02:00
'what': 'setSessionRules',
2015-02-11 17:34:51 +01:00
'rules': rulesFromHTML('#diff .right li') + '\n' + result
};
messager.send(request, renderRules);
};
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,
'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());
}
}
2015-05-16 22:15:30 +02:00
return rules.join('\n').trim();
2015-02-11 17:34:51 +01:00
};
/******************************************************************************/
var revertHandler = function() {
var request = {
2015-05-21 20:15:17 +02:00
'what': 'setSessionRules',
2015-02-11 17:34:51 +01:00
'rules': rulesFromHTML('#diff .left li')
};
messager.send(request, renderRules);
};
/******************************************************************************/
var commitHandler = function() {
var request = {
2015-05-21 20:15:17 +02:00
'what': 'setPermanentRules',
2015-02-11 17:34:51 +01:00
'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
var parent = uDom(this).ancestors('#diff');
2015-03-12 06:52:27 +01:00
// If we're already editing, don't reset
if ( parent.hasClassName('edit') ) {
return;
}
uDom('#diff .right textarea').val(rulesFromHTML('#diff .right li'));
2015-02-11 17:34:51 +01:00
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 = {
2015-05-21 20:15:17 +02:00
'what': 'setSessionRules',
2015-02-11 17:34:51 +01:00
'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);
};
/******************************************************************************/
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);
2015-02-17 07:31:01 +01:00
uDom('#diff > .pane.right > .rulesContainer').on('dblclick', editStartHandler);
2015-02-11 17:54:05 +01:00
uDom('#editStopButton').on('click', editStopHandler);
uDom('#editCancelButton').on('click', editCancelHandler);
2015-02-11 17:34:51 +01:00
2015-05-21 20:15:17 +02:00
messager.send({ what: 'getRules' }, renderRules);
});
/******************************************************************************/
})();