1
0
Fork 0
mirror of https://github.com/gorhill/uMatrix.git synced 2024-07-01 20:40:36 +12:00
uMatrix/src/js/privacy.js

152 lines
5.4 KiB
JavaScript
Raw Normal View History

2014-10-18 08:01:09 +13:00
/*******************************************************************************
µMatrix - a Chromium browser extension to black/white list 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-04-12 09:15:57 +12:00
/* global vAPI, uDom */
2014-11-20 02:59:38 +13:00
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-04-12 09:15:57 +12:00
var messager = vAPI.messaging.channel('privacy.js');
2014-10-18 08:01:09 +13:00
2014-11-20 02:59:38 +13:00
var cachedPrivacySettings = {};
2014-10-18 08:01:09 +13:00
/******************************************************************************/
function changeUserSettings(name, value) {
2015-04-12 09:15:57 +12:00
messager.send({
2014-10-18 08:01:09 +13:00
what: 'userSettings',
name: name,
value: value
});
}
/******************************************************************************/
2014-11-20 02:59:38 +13:00
function changeMatrixSwitch(name, state) {
2015-04-12 09:15:57 +12:00
messager.send({
2014-11-20 02:59:38 +13:00
what: 'setMatrixSwitch',
switchName: name,
state: state
});
}
/******************************************************************************/
2014-10-25 10:34:30 +13:00
function onChangeValueHandler(uelem, setting, min, max) {
2014-11-20 02:59:38 +13:00
var oldVal = cachedPrivacySettings.userSettings[setting];
2014-10-25 10:34:30 +13:00
var newVal = Math.round(parseFloat(uelem.val()));
2014-10-18 08:01:09 +13:00
if ( typeof newVal !== 'number' ) {
newVal = oldVal;
} else {
newVal = Math.max(newVal, min);
newVal = Math.min(newVal, max);
}
2014-10-25 10:34:30 +13:00
uelem.val(newVal);
2014-10-18 08:01:09 +13:00
if ( newVal !== oldVal ) {
changeUserSettings(setting, newVal);
}
}
/******************************************************************************/
function prepareToDie() {
2014-10-25 10:34:30 +13:00
onChangeValueHandler(uDom('#delete-unused-session-cookies-after'), 'deleteUnusedSessionCookiesAfter', 15, 1440);
onChangeValueHandler(uDom('#clear-browser-cache-after'), 'clearBrowserCacheAfter', 15, 1440);
onChangeValueHandler(uDom('#spoof-user-agent-every'), 'spoofUserAgentEvery', 2, 999);
2014-10-18 08:01:09 +13:00
}
/******************************************************************************/
var installEventHandlers = function() {
2014-11-20 02:59:38 +13:00
uDom('[data-setting-bool]').on('change', function(){
var settingName = this.getAttribute('data-setting-bool');
if ( typeof settingName === 'string' && settingName !== '' ) {
changeUserSettings(settingName, this.checked);
}
2014-10-18 08:01:09 +13:00
});
2014-11-20 02:59:38 +13:00
uDom('[data-matrix-switch]').on('change', function(){
var switchName = this.getAttribute('data-matrix-switch');
if ( typeof switchName === 'string' && switchName !== '' ) {
changeMatrixSwitch(switchName, this.checked);
}
});
2014-10-25 10:34:30 +13:00
uDom('#delete-unused-session-cookies-after').on('change', function(){
onChangeValueHandler(uDom(this), 'deleteUnusedSessionCookiesAfter', 15, 1440);
2014-10-18 08:01:09 +13:00
});
2014-10-25 10:34:30 +13:00
uDom('#clear-browser-cache-after').on('change', function(){
onChangeValueHandler(uDom(this), 'clearBrowserCacheAfter', 15, 1440);
2014-10-18 08:01:09 +13:00
});
2014-10-25 10:34:30 +13:00
uDom('#spoof-user-agent-every').on('change', function(){
onChangeValueHandler(uDom(this), 'spoofUserAgentEvery', 2, 999);
2014-10-18 08:01:09 +13:00
});
2014-10-25 10:34:30 +13:00
uDom('#spoof-user-agent-with').on('change', function(){
changeUserSettings('spoofUserAgentWith', uDom(this).val());
2014-10-18 08:01:09 +13:00
});
// https://github.com/gorhill/httpswitchboard/issues/197
2014-10-25 10:34:30 +13:00
uDom(window).on('beforeunload', prepareToDie);
2014-10-18 08:01:09 +13:00
};
/******************************************************************************/
2014-10-25 10:34:30 +13:00
uDom.onLoad(function() {
2014-11-20 02:59:38 +13:00
var onSettingsReceived = function(privacySettings) {
2014-10-18 08:01:09 +13:00
// Cache copy
2014-11-20 02:59:38 +13:00
cachedPrivacySettings = privacySettings;
var userSettings = privacySettings.userSettings;
var matrixSwitches = privacySettings.matrixSwitches;
uDom('[data-setting-bool]').forEach(function(elem){
var settingName = elem.attr('data-setting-bool');
if ( typeof settingName === 'string' && settingName !== '' ) {
elem.prop('checked', userSettings[settingName] === true);
}
});
uDom('[data-matrix-switch]').forEach(function(elem){
var switchName = elem.attr('data-matrix-switch');
if ( typeof switchName === 'string' && switchName !== '' ) {
elem.prop('checked', matrixSwitches[switchName] === true);
}
});
2014-10-18 08:01:09 +13:00
2014-10-25 10:34:30 +13:00
uDom('#delete-unused-session-cookies-after').val(userSettings.deleteUnusedSessionCookiesAfter);
uDom('#clear-browser-cache-after').val(userSettings.clearBrowserCacheAfter);
uDom('#spoof-user-agent-every').val(userSettings.spoofUserAgentEvery);
uDom('#spoof-user-agent-with').val(userSettings.spoofUserAgentWith);
2014-10-18 08:01:09 +13:00
installEventHandlers();
};
2015-04-12 09:15:57 +12:00
messager.send({ what: 'getPrivacySettings' }, onSettingsReceived);
2014-10-18 08:01:09 +13:00
});
/******************************************************************************/
})();