1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

this fixes #39

This commit is contained in:
gorhill 2014-07-17 10:52:43 -04:00
parent b3d9b3ba73
commit 999573aa86
9 changed files with 315 additions and 3 deletions

View File

@ -12,9 +12,6 @@ div > p:first-child {
div > p:last-child {
margin-bottom: 0;
}
.dim {
color: #444;
}
.userFilters {
font-size: smaller;
width: 48em;

View File

@ -19,6 +19,10 @@
"message":"Deine Filter",
"description":"appears as tab name in dashboard."
},
"whitelistPageName":{
"message":"Whitelist",
"description":"appears as tab name in dashboard."
},
"statsPageName":{
"message":"Statistiken",
"description":"appears as tab name in dashboard."
@ -139,6 +143,22 @@
"message":"Änderungen anwenden",
"description":"English: Apply changes"
},
"whitelistPrompt":{
"message":"Your list of host names for which µBlock will be disabled. One entry per line. Invalid host names will be silently ignored.",
"description":"English: Your list of host names for which µBlock will be disabled. One host name per line. Invalid host names will be silently ignored."
},
"whitelistImport":{
"message":"Import and append",
"description":"English: Import and append"
},
"whitelistExport":{
"message":"Export",
"description":"English: Export"
},
"whitelistApply":{
"message":"Apply changes",
"description":"English: Apply changes"
},
"logBlockedRequestsPrompt":{
"message":"Protokollieren von blockierten Elementen aktivieren",
"description":"English: Enable the logging of blocked requests"

View File

@ -19,6 +19,10 @@
"message":"Your filters",
"description":"appears as tab name in dashboard."
},
"whitelistPageName":{
"message":"Whitelist",
"description":"appears as tab name in dashboard."
},
"statsPageName":{
"message":"Statistics",
"description":"appears as tab name in dashboard."
@ -139,6 +143,22 @@
"message":"Apply changes",
"description":"English: Apply changes"
},
"whitelistPrompt":{
"message":"Your list of host names for which µBlock will be disabled. One entry per line. Invalid host names will be silently ignored.",
"description":"English: Your list of host names for which µBlock will be disabled. One host name per line. Invalid host names will be silently ignored."
},
"whitelistImport":{
"message":"Import and append",
"description":"English: Import and append"
},
"whitelistExport":{
"message":"Export",
"description":"English: Export"
},
"whitelistApply":{
"message":"Apply changes",
"description":"English: Apply changes"
},
"logBlockedRequestsPrompt":{
"message":"Enable the logging of blocked requests",
"description":"English: Enable the logging of blocked requests"

View File

@ -19,6 +19,10 @@
"message":"Vos règles",
"description":"appears as tab name in dashboard."
},
"whitelistPageName":{
"message":"Whitelist",
"description":"appears as tab name in dashboard."
},
"statsPageName":{
"message":"Statistiques",
"description":"appears as tab name in dashboard."
@ -139,6 +143,22 @@
"message":"Appliquer",
"description":"English: Apply changes"
},
"whitelistPrompt":{
"message":"Your list of host names for which µBlock will be disabled. One entry per line. Invalid host names will be silently ignored.",
"description":"English: Your list of host names for which µBlock will be disabled. One host name per line. Invalid host names will be silently ignored."
},
"whitelistImport":{
"message":"Import and append",
"description":"English: Import and append"
},
"whitelistExport":{
"message":"Export",
"description":"English: Export"
},
"whitelistApply":{
"message":"Apply changes",
"description":"English: Apply changes"
},
"logBlockedRequestsPrompt":{
"message":"Activer la journalisation des requêtes bloquées",
"description":"English: Enable the logging of blocked requests"

View File

@ -83,6 +83,7 @@ iframe {
<span>µBlock</span>
<a class="tabButton" href="#" data-dashboard-panel-url="3p-filters.html" data-i18n="3pPageName"></a>
<a class="tabButton" href="#" data-dashboard-panel-url="1p-filters.html" data-i18n="1pPageName"></a>
<a class="tabButton" href="#" data-dashboard-panel-url="whitelist.html" data-i18n="whitelistPageName"></a>
<a class="tabButton" href="#" data-dashboard-panel-url="settings.html" data-i18n="settingsPageName"></a>
<a class="tabButton" href="#" data-dashboard-panel-url="stats.html" data-i18n="statsPageName"></a>
<a class="tabButton" href="#" data-dashboard-panel-url="about.html" data-i18n="aboutPageName"></a>

View File

@ -301,6 +301,45 @@ var onMessage = function(request, sender, callback) {
/******************************************************************************/
// whitelist.js
(function() {
var onMessage = function(request, sender, callback) {
var µb = µBlock;
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
switch ( request.what ) {
case 'getWhitelist':
response = µb.userSettings.netExceptionList;
break;
case 'setWhitelist':
µb.userSettings.netExceptionList = request.whitelist;
µb.saveWhitelist();
break;
default:
return µb.messaging.defaultHandler(request, sender, callback);
}
callback(response);
};
µBlock.messaging.listen('whitelist.js', onMessage);
})();
/******************************************************************************/
// stats.js
(function() {

View File

@ -80,6 +80,15 @@
/******************************************************************************/
µBlock.saveWhitelist = function() {
var bin = { 'netExceptionList': this.userSettings.netExceptionList };
chrome.storage.local.set(bin, function() {
µBlock.getBytesInUse();
});
};
/******************************************************************************/
µBlock.saveUserFilters = function(content, callback) {
return this.assets.put(this.userFiltersPath, content, callback);
};

162
js/whitelist.js Normal file
View File

@ -0,0 +1,162 @@
/*******************************************************************************
µBlock - 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/uBlock
*/
/* global chrome, messaging, uDom */
/******************************************************************************/
(function() {
/******************************************************************************/
messaging.start('whitelist.js');
/******************************************************************************/
var cachedWhitelist = '';
// Could make it more fancy if needed. But speed... It's a compromise.
var reBadHostname = /[\x00-\x08\x0b\x0c\x0e-\x1f\x21-\x2c\x2f\x3a-\x40\x5b-\x60\x7b-\x7e]/;
/******************************************************************************/
var validateHostname = function(s) {
var hn = punycode.toASCII(s).toLowerCase();
if ( reBadHostname.test(hn) ) {
return '';
}
return hn;
};
var whitelistFromString = function(s) {
var whitelist = {};
var items = s.split(/\s+/);
var item;
for ( var i = 0; i < items.length; i++ ) {
item = validateHostname(items[i]);
if ( item !== '' ) {
whitelist[item] = true;
}
}
return whitelist;
};
var stringFromWhitelist = function(whitelist) {
var s = [];
var items = Object.keys(whitelist);
for ( var i = 0; i < items.length; i++ ) {
s.push(punycode.toUnicode(items[i]));
}
return s.sort(function(a, b) { return a.localeCompare(b); }).join('\n');
};
/******************************************************************************/
var badWhitelist = function(s) {
return reBadHostname.test(s);
};
/******************************************************************************/
var whitelistChanged = function() {
var s = uDom('#whitelist').val().trim();
uDom('#whitelistApply').prop(
'disabled',
s === cachedWhitelist
);
uDom('#whitelist').toggleClass('bad', badWhitelist(s));
};
/******************************************************************************/
function renderWhitelist() {
var onRead = function(whitelist) {
cachedWhitelist = stringFromWhitelist(whitelist);
uDom('#whitelist').val(cachedWhitelist);
};
messaging.ask({ what: 'getWhitelist' }, onRead);
};
/******************************************************************************/
var importWhitelistFromFile = function() {
var input = uDom('<input />').attr({
type: 'file',
accept: 'text/plain'
});
var fileReaderOnLoadHandler = function() {
var textarea = uDom('#whitelist');
textarea.val([textarea.val(), this.result].join('\n').trim());
whitelistChanged();
};
var filePickerOnChangeHandler = function() {
input.off('change', filePickerOnChangeHandler);
var file = this.files[0];
if ( !file ) {
return;
}
if ( file.type.indexOf('text') !== 0 ) {
return;
}
var fr = new FileReader();
fr.onload = fileReaderOnLoadHandler;
fr.readAsText(file);
};
input.on('change', filePickerOnChangeHandler);
input.trigger('click');
};
/******************************************************************************/
var exportWhitelistToFile = function() {
chrome.downloads.download({
'url': 'data:text/plain,' + encodeURIComponent(uDom('#whitelist').val()),
'filename': 'my-ublock-whitelist.txt',
'saveAs': true
});
};
/******************************************************************************/
var whitelistApplyHandler = function() {
cachedWhitelist = uDom('#whitelist').val().trim();
var request = {
what: 'setWhitelist',
whitelist: whitelistFromString(cachedWhitelist)
};
messaging.tell(request);
whitelistChanged();
};
/******************************************************************************/
uDom.onLoad(function() {
uDom('#importWhitelistFromFile').on('click', importWhitelistFromFile);
uDom('#exportWhitelistToFile').on('click', exportWhitelistToFile);
uDom('#whitelist').on('input', whitelistChanged);
uDom('#whitelistApply').on('click', whitelistApplyHandler);
renderWhitelist();
});
/******************************************************************************/
})();

44
whitelist.html Normal file
View File

@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>µBlock — Whitelist</title>
<link rel="stylesheet" type="text/css" href="css/common.css">
<link rel="stylesheet" type="text/css" href="css/dashboard-common.css">
<style>
div > p:first-child {
margin-top: 0;
}
div > p:last-child {
margin-bottom: 0;
}
#whitelist {
font-size: smaller;
width: 48em;
height: 40em;
white-space: nowrap;
}
#whitelist.bad {
background-color: #fee;
}
</style>
</head>
<body>
<div>
<p data-i18n="whitelistPrompt"></p>
<p><button id="importWhitelistFromFile" data-i18n="whitelistImport"></button> &emsp; <button id="exportWhitelistToFile" data-i18n="whitelistExport"></button></p>
<textarea id="whitelist"></textarea>
<p><button id="whitelistApply" type="button" disabled="true" data-i18n="whitelistApply"></button></p>
</div>
<script src="lib/punycode.min.js"></script>
<script src="js/udom.js"></script>
<script src="js/i18n.js"></script>
<script src="js/dashboard-common.js"></script>
<script src="js/messaging-client.js"></script>
<script src="js/whitelist.js"></script>
</body>
</html>