1
0
Fork 0
mirror of https://github.com/gorhill/uMatrix.git synced 2024-06-02 02:14:52 +12:00
uMatrix/src/js/hosts-files.js

451 lines
14 KiB
JavaScript
Raw Normal View History

2014-10-18 08:01:09 +13:00
/*******************************************************************************
µMatrix - a Chromium browser extension to black/white list requests.
2015-05-02 11:27:43 +12:00
Copyright (C) 2014-2015 Raymond Hill
2014-10-18 08:01:09 +13: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/uMatrix
*/
2015-04-12 09:15:57 +12:00
/* global vAPI, uDom */
2014-10-18 08:01:09 +13:00
/******************************************************************************/
(function() {
2015-04-12 09:15:57 +12:00
'use strict';
2014-10-18 08:01:09 +13:00
/******************************************************************************/
2015-05-02 11:27:43 +12:00
var listDetails = {};
var externalHostsFiles = '';
var cacheWasPurged = false;
var needUpdate = false;
var hasCachedContent = false;
/******************************************************************************/
2014-10-18 08:01:09 +13:00
var onMessage = function(msg) {
switch ( msg.what ) {
2015-05-02 11:27:43 +12:00
case 'loadHostsFilesCompleted':
renderHostsFiles();
break;
case 'forceUpdateAssetsProgress':
renderBusyOverlay(true, msg.progress);
if ( msg.done ) {
messager.send({ what: 'reloadHostsFiles' });
}
break;
2014-10-18 08:01:09 +13:00
2015-05-02 11:27:43 +12:00
default:
break;
2014-10-18 08:01:09 +13:00
}
};
2015-04-12 09:15:57 +12:00
var messager = vAPI.messaging.channel('hosts-files.js', onMessage);
/******************************************************************************/
2015-05-02 11:27:43 +12:00
var renderNumber = function(value) {
return value.toLocaleString();
};
2014-10-18 08:01:09 +13:00
/******************************************************************************/
// TODO: get rid of background page dependencies
2015-05-02 11:27:43 +12:00
var renderHostsFiles = function() {
var listEntryTemplate = uDom('#templates .listEntry');
var listStatsTemplate = vAPI.i18n('hostsFilesPerFileStats');
var lastUpdateString = vAPI.i18n('hostsFilesLastUpdate');
var renderElapsedTimeToString = vAPI.i18n.renderElapsedTimeToString;
2015-05-09 01:02:12 +12:00
var reExternalHostFile = /^https?:/;
2014-10-18 08:01:09 +13:00
// Assemble a pretty blacklist name if possible
2014-10-24 12:40:48 +13:00
var listNameFromListKey = function(listKey) {
var list = listDetails.current[listKey] || listDetails.available[listKey];
var listTitle = list ? list.title : '';
if ( listTitle === '' ) {
return listKey;
2014-10-18 08:01:09 +13:00
}
2014-10-24 12:40:48 +13:00
return listTitle;
};
2015-05-02 11:27:43 +12:00
var liFromListEntry = function(listKey) {
var elem, text;
var entry = listDetails.available[listKey];
var li = listEntryTemplate.clone();
if ( entry.off !== true ) {
li.descendants('input').attr('checked', '');
2014-10-18 08:01:09 +13:00
}
2015-05-02 11:27:43 +12:00
elem = li.descendants('a:nth-of-type(1)');
elem.attr('href', encodeURI(listKey));
elem.text(listNameFromListKey(listKey) + '\u200E');
elem = li.descendants('a:nth-of-type(2)');
if ( entry.homeDomain ) {
elem.attr('href', 'http://' + encodeURI(entry.homeHostname));
elem.text('(' + entry.homeDomain + ')');
elem.css('display', '');
2014-10-18 08:01:09 +13:00
}
2015-05-02 11:27:43 +12:00
elem = li.descendants('span:nth-of-type(1)');
text = listStatsTemplate
.replace('{{used}}', renderNumber(!entry.off && !isNaN(+entry.entryUsedCount) ? entry.entryUsedCount : 0))
.replace('{{total}}', !isNaN(+entry.entryCount) ? renderNumber(entry.entryCount) : '?');
elem.text(text);
// https://github.com/gorhill/uBlock/issues/78
// Badge for non-secure connection
var remoteURL = listKey;
if ( remoteURL.lastIndexOf('http:', 0) !== 0 ) {
remoteURL = entry.homeURL || '';
2014-10-24 12:40:48 +13:00
}
2015-05-02 11:27:43 +12:00
if ( remoteURL.lastIndexOf('http:', 0) === 0 ) {
li.descendants('span.status.unsecure').css('display', '');
}
// https://github.com/chrisaljoudi/uBlock/issues/104
var asset = listDetails.cache[listKey] || {};
// Badge for update status
if ( entry.off !== true ) {
if ( asset.repoObsolete ) {
li.descendants('span.status.new').css('display', '');
needUpdate = true;
} else if ( asset.cacheObsolete ) {
li.descendants('span.status.obsolete').css('display', '');
needUpdate = true;
} else if ( entry.external && !asset.cached ) {
li.descendants('span.status.obsolete').css('display', '');
2014-10-24 12:40:48 +13:00
needUpdate = true;
}
}
2015-05-02 11:27:43 +12:00
2014-10-24 12:40:48 +13:00
// In cache
if ( asset.cached ) {
2015-05-02 11:27:43 +12:00
elem = li.descendants('span.status.purge');
elem.css('display', '');
elem.attr('title', lastUpdateString.replace('{{ago}}', renderElapsedTimeToString(asset.lastModified)));
2014-10-24 12:40:48 +13:00
hasCachedContent = true;
}
2015-05-02 11:27:43 +12:00
return li;
2014-10-24 12:40:48 +13:00
};
var onListsReceived = function(details) {
// Before all, set context vars
listDetails = details;
needUpdate = false;
hasCachedContent = false;
2015-05-02 11:27:43 +12:00
var availableLists = details.available;
var listKeys = Object.keys(details.available);
2015-05-09 01:02:12 +12:00
// Sort works this way:
// - Send /^https?:/ items at the end (custom hosts file URL)
2015-05-02 11:27:43 +12:00
listKeys.sort(function(a, b) {
2015-05-09 01:02:12 +12:00
var ta = availableLists[a].title || a;
var tb = availableLists[b].title || b;
if ( reExternalHostFile.test(ta) === reExternalHostFile.test(tb) ) {
2015-05-02 11:27:43 +12:00
return ta.localeCompare(tb);
2014-10-24 12:40:48 +13:00
}
2015-05-09 01:02:12 +12:00
if ( reExternalHostFile.test(tb) ) {
2015-05-02 11:27:43 +12:00
return -1;
}
return 1;
});
var ulList = uDom('#lists').empty();
for ( var i = 0; i < listKeys.length; i++ ) {
ulList.append(liFromListEntry(listKeys[i]));
2014-10-24 12:40:48 +13:00
}
uDom('#listsOfBlockedHostsPrompt').text(
2015-05-02 11:27:43 +12:00
vAPI.i18n('hostsFilesStats').replace(
'{{blockedHostnameCount}}',
renderNumber(details.blockedHostnameCount)
)
2014-10-24 12:40:48 +13:00
);
uDom('#autoUpdate').prop('checked', listDetails.autoUpdate === true);
2015-05-02 11:27:43 +12:00
renderWidgets();
renderBusyOverlay(details.manualUpdate, details.manualUpdateProgress);
2014-10-24 12:40:48 +13:00
};
2015-04-12 09:15:57 +12:00
messager.send({ what: 'getLists' }, onListsReceived);
2014-10-24 12:40:48 +13:00
};
2014-10-18 08:01:09 +13:00
/******************************************************************************/
2015-05-02 11:27:43 +12:00
// Progress must be normalized to [0, 1], or can be undefined.
var renderBusyOverlay = function(state, progress) {
progress = progress || {};
var showProgress = typeof progress.value === 'number';
if ( showProgress ) {
uDom('#busyOverlay > div:nth-of-type(2) > div:first-child').css(
'width',
(progress.value * 100).toFixed(1) + '%'
);
var text = progress.text || '';
if ( text !== '' ) {
uDom('#busyOverlay > div:nth-of-type(2) > div:last-child').text(text);
}
}
uDom('#busyOverlay > div:nth-of-type(2)').css('display', showProgress ? '' : 'none');
uDom('body').toggleClass('busy', !!state);
};
/******************************************************************************/
// This is to give a visual hint that the selection of blacklists has changed.
var renderWidgets = function() {
uDom('#buttonApply').toggleClass('disabled', !listsSelectionChanged());
uDom('#buttonUpdate').toggleClass('disabled', !listsContentChanged());
uDom('#buttonPurgeAll').toggleClass('disabled', !hasCachedContent);
};
/******************************************************************************/
2014-10-24 12:40:48 +13:00
// Return whether selection of lists changed.
2014-10-18 08:01:09 +13:00
2014-10-24 12:40:48 +13:00
var listsSelectionChanged = function() {
if ( cacheWasPurged ) {
return true;
}
var availableLists = listDetails.available;
var currentLists = listDetails.current;
var location, availableOff, currentOff;
// This check existing entries
for ( location in availableLists ) {
if ( availableLists.hasOwnProperty(location) === false ) {
continue;
}
availableOff = availableLists[location].off === true;
currentOff = currentLists[location] === undefined || currentLists[location].off === true;
if ( availableOff !== currentOff ) {
return true;
}
2014-10-18 08:01:09 +13:00
}
2014-10-24 12:40:48 +13:00
// This check removed entries
for ( location in currentLists ) {
if ( currentLists.hasOwnProperty(location) === false ) {
continue;
}
currentOff = currentLists[location].off === true;
availableOff = availableLists[location] === undefined || availableLists[location].off === true;
if ( availableOff !== currentOff ) {
return true;
}
}
return false;
};
2014-10-18 08:01:09 +13:00
2014-10-24 12:40:48 +13:00
/******************************************************************************/
// Return whether content need update.
var listsContentChanged = function() {
return needUpdate;
};
2014-10-18 08:01:09 +13:00
/******************************************************************************/
// This is to give a visual hint that the selection of blacklists has changed.
2014-10-24 12:40:48 +13:00
var updateWidgets = function() {
uDom('#buttonApply').toggleClass('disabled', !listsSelectionChanged());
uDom('#buttonUpdate').toggleClass('disabled', !listsContentChanged());
uDom('#buttonPurgeAll').toggleClass('disabled', !hasCachedContent);
uDom('body').toggleClass('busy', false);
};
2014-10-18 08:01:09 +13:00
/******************************************************************************/
2014-10-24 12:40:48 +13:00
var onListCheckboxChanged = function() {
var href = uDom(this).parent().descendants('a').first().attr('href');
if ( typeof href !== 'string' ) {
2014-10-18 08:01:09 +13:00
return;
}
2014-10-24 12:40:48 +13:00
if ( listDetails.available[href] === undefined ) {
return;
}
listDetails.available[href].off = !this.checked;
updateWidgets();
};
/******************************************************************************/
var onListLinkClicked = function(ev) {
2015-04-12 09:15:57 +12:00
messager.send({
2014-10-24 12:40:48 +13:00
what: 'gotoExtensionURL',
url: 'asset-viewer.html?url=' + uDom(this).attr('href')
});
ev.preventDefault();
};
/******************************************************************************/
var onPurgeClicked = function() {
var button = uDom(this);
var li = button.parent();
var href = li.descendants('a').first().attr('href');
if ( !href ) {
return;
}
2015-04-12 09:15:57 +12:00
messager.send({ what: 'purgeCache', path: href });
2014-10-24 12:40:48 +13:00
button.remove();
if ( li.descendants('input').first().prop('checked') ) {
cacheWasPurged = true;
updateWidgets();
}
};
/******************************************************************************/
2015-05-02 11:27:43 +12:00
var selectHostsFiles = function(callback) {
2014-10-18 08:01:09 +13:00
var switches = [];
2015-05-02 11:27:43 +12:00
var lis = uDom('#lists .listEntry'), li;
2014-10-18 08:01:09 +13:00
var i = lis.length;
while ( i-- ) {
2015-05-02 11:27:43 +12:00
li = lis.at(i);
2014-10-18 08:01:09 +13:00
switches.push({
2015-05-02 11:27:43 +12:00
location: li.descendants('a').attr('href'),
off: li.descendants('input').prop('checked') === false
2014-10-18 08:01:09 +13:00
});
}
2015-05-02 11:27:43 +12:00
2015-04-12 09:15:57 +12:00
messager.send({
2015-05-02 11:27:43 +12:00
what: 'selectHostsFiles',
switches: switches
}, callback);
2014-10-24 12:40:48 +13:00
};
2014-10-18 08:01:09 +13:00
/******************************************************************************/
2014-10-24 12:40:48 +13:00
var buttonApplyHandler = function() {
2015-05-02 11:27:43 +12:00
uDom('#buttonApply').removeClass('enabled');
renderBusyOverlay(true);
var onSelectionDone = function() {
messager.send({ what: 'reloadHostsFiles' });
};
selectHostsFiles(onSelectionDone);
cacheWasPurged = false;
2014-10-24 12:40:48 +13:00
};
/******************************************************************************/
var buttonUpdateHandler = function() {
2015-05-02 11:27:43 +12:00
uDom('#buttonUpdate').removeClass('enabled');
2014-10-24 12:40:48 +13:00
if ( needUpdate ) {
2015-05-02 11:27:43 +12:00
renderBusyOverlay(true);
var onSelectionDone = function() {
messager.send({ what: 'forceUpdateAssets' });
};
selectHostsFiles(onSelectionDone);
cacheWasPurged = false;
2014-10-24 12:40:48 +13:00
}
};
/******************************************************************************/
var buttonPurgeAllHandler = function() {
2015-05-02 11:27:43 +12:00
uDom('#buttonPurgeAll').removeClass('enabled');
renderBusyOverlay(true);
2014-10-24 12:40:48 +13:00
var onCompleted = function() {
2015-05-02 11:27:43 +12:00
cacheWasPurged = true;
renderHostsFiles();
2014-10-24 12:40:48 +13:00
};
2015-05-02 11:27:43 +12:00
2015-04-12 09:15:57 +12:00
messager.send({ what: 'purgeAllCaches' }, onCompleted);
2014-10-24 12:40:48 +13:00
};
/******************************************************************************/
var autoUpdateCheckboxChanged = function() {
2015-04-12 09:15:57 +12:00
messager.send({
2014-10-24 12:40:48 +13:00
what: 'userSettings',
name: 'autoUpdate',
value: this.checked
});
};
/******************************************************************************/
var renderExternalLists = function() {
var onReceived = function(details) {
uDom('#externalHostsFiles').val(details);
externalHostsFiles = details;
};
2015-04-12 09:15:57 +12:00
messager.send({ what: 'userSettings', name: 'externalHostsFiles' }, onReceived);
2014-10-24 12:40:48 +13:00
};
/******************************************************************************/
var externalListsChangeHandler = function() {
uDom('#externalListsParse').prop(
'disabled',
this.value.trim() === externalHostsFiles
);
};
/******************************************************************************/
var externalListsApplyHandler = function() {
externalHostsFiles = uDom('#externalHostsFiles').val();
2015-04-12 09:15:57 +12:00
messager.send({
2014-10-24 12:40:48 +13:00
what: 'userSettings',
name: 'externalHostsFiles',
value: externalHostsFiles
});
2015-05-02 11:27:43 +12:00
renderHostsFiles();
2014-10-24 12:40:48 +13:00
uDom('#externalListsParse').prop('disabled', true);
};
/******************************************************************************/
uDom.onLoad(function() {
uDom('#autoUpdate').on('change', autoUpdateCheckboxChanged);
uDom('#buttonApply').on('click', buttonApplyHandler);
uDom('#buttonUpdate').on('click', buttonUpdateHandler);
uDom('#buttonPurgeAll').on('click', buttonPurgeAllHandler);
2015-05-02 11:27:43 +12:00
uDom('#lists').on('change', '.listEntry > input', onListCheckboxChanged);
uDom('#lists').on('click', '.listEntry > a:nth-of-type(1)', onListLinkClicked);
2014-10-24 12:40:48 +13:00
uDom('#lists').on('click', 'span.purge', onPurgeClicked);
uDom('#externalHostsFiles').on('input', externalListsChangeHandler);
uDom('#externalListsParse').on('click', externalListsApplyHandler);
2015-05-02 11:27:43 +12:00
renderHostsFiles();
2014-10-24 12:40:48 +13:00
renderExternalLists();
2014-10-18 08:01:09 +13:00
});
/******************************************************************************/
})();