LocalCDN-Firefox-Chrome-Brave/pages/options/options.js

333 lines
11 KiB
JavaScript
Raw Normal View History

2020-02-27 13:45:29 +01:00
/**
* Main Options Page
2020-06-30 18:41:58 +02:00
* Belongs to LocalCDN (since 2020-02-26)
* (Origin: Decentraleyes)
2020-02-27 13:45:29 +01:00
*
* @author Thomas Rientjes
* @since 2016-08-09
2020-06-30 18:41:58 +02:00
*
* @author nobody
* @since 2020-05-04
*
2020-02-27 13:45:29 +01:00
* @license MPL 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';
2021-02-17 07:01:08 +01:00
2020-02-27 13:45:29 +01:00
/**
* Options
*/
let options = {};
2020-02-27 13:45:29 +01:00
2021-02-17 07:01:08 +01:00
2020-02-27 13:45:29 +01:00
/**
* Private Methods
*/
options._renderContents = function () {
2020-04-07 12:19:32 +02:00
let translationComplete = true;
2020-02-27 13:45:29 +01:00
document.body.setAttribute('dir', options._scriptDirection);
2020-04-07 12:19:32 +02:00
translationComplete = helpers.insertI18nContentIntoDocument(document);
2020-02-27 13:45:29 +01:00
options._determineLocalOptionValues().then(options._determineOptionValues).then(options._renderOptionsPanel);
2020-05-29 08:20:53 +02:00
2020-04-07 12:19:32 +02:00
if (!translationComplete) {
options._renderLocaleNotice();
}
if (!BrowserType.FIREFOX) {
document.getElementById('chromium-banner').style.display = 'block';
document.getElementById('div-html-badge').style.display = 'none';
}
2021-01-16 07:56:50 +01:00
document.getElementById('label-version').textContent = chrome.runtime.getManifest().version;
2020-02-27 13:45:29 +01:00
};
options._renderOptionsPanel = function () {
2021-02-21 19:28:18 +01:00
let allowlistedDomains, domainAllowlist, htmlFilterDomains, domainHtmlFilter,
googleFontsDomains, domainAllowedGoogleFonts, basic, advanced, other;
2020-08-08 07:28:41 +02:00
2020-10-17 07:09:30 +02:00
allowlistedDomains = options._optionValues.allowlistedDomains;
domainAllowlist = options._serializeAllowlistedDomains(allowlistedDomains);
2020-02-27 13:45:29 +01:00
htmlFilterDomains = options._optionValues.domainsManipulateDOM;
2020-10-17 07:09:30 +02:00
domainHtmlFilter = options._serializeAllowlistedDomains(htmlFilterDomains);
googleFontsDomains = options._optionValues.allowedDomainsGoogleFonts;
2020-10-17 07:09:30 +02:00
domainAllowedGoogleFonts = options._serializeAllowlistedDomains(googleFontsDomains);
2021-02-21 19:28:18 +01:00
basic = {
'showIconBadge': options._optionValues.showIconBadge,
'updateNotification': options._optionValues.updateNotification,
'disablePrefetch': options._optionValues.disablePrefetch,
'stripMetadata': options._optionValues.stripMetadata,
'internalStatistics': options._internalStatistics,
2021-02-21 19:56:13 +01:00
'hideDonationButton': options._optionValues.hideDonationButton,
2021-02-21 19:28:18 +01:00
'allowlistedDomains': domainAllowlist
};
advanced = {
'blockMissing': options._optionValues.blockMissing,
'blockGoogleFonts': options._optionValues.blockGoogleFonts,
'allowedDomainsGoogleFonts': domainAllowedGoogleFonts,
'enableLogging': options._optionValues.enableLogging,
'domainsManipulateDOM': domainHtmlFilter,
2021-03-13 06:56:43 +01:00
'negateHtmlFilterList': options._optionValues.negateHtmlFilterList,
'changeBadgeColorMissingResources': options._optionValues.changeBadgeColorMissingResources
2021-02-21 19:28:18 +01:00
};
other = {
'selectedIcon': options._optionValues.selectedIcon,
'badgeDefaultBackgroundColor': options._optionValues.badgeDefaultBackgroundColor,
'badgeDefaultTextColor': options._optionValues.badgeDefaultTextColor,
'badgeHTMLFilterBackgroundColor': options._optionValues.badgeHTMLFilterBackgroundColor,
'badgeHTMLfilterTextColor': options._optionValues.badgeHTMLfilterTextColor,
'badgeMissingResourceBackgroundColor': options._optionValues.badgeMissingResourceBackgroundColor,
'badgeMissingResourceTextColor': options._optionValues.badgeMissingResourceTextColor,
2021-02-21 19:28:18 +01:00
'storageType': options._optionValues.storageType
};
2020-02-27 13:45:29 +01:00
options._registerMiscellaneousEventListeners();
if (options._optionValues.blockMissing === true) {
options._renderBlockMissingNotice();
}
if (options._languageSupported === false) {
options._renderLocaleNotice();
}
2021-02-21 19:28:18 +01:00
optionsBasic.init(basic);
optionsAdvanced.init(advanced);
optionsOther.init(other);
optionsInfo.init();
2020-08-30 19:07:14 +02:00
document.getElementById('btn-general-tab').addEventListener('click', options._changeTab);
document.getElementById('btn-advanced-tab').addEventListener('click', options._changeTab);
document.getElementById('btn-export-import-tab').addEventListener('click', options._changeTab);
2020-02-27 13:45:29 +01:00
};
options._renderBlockMissingNotice = function () {
let blockMissingNoticeElement = document.getElementById('notice-block-missing');
blockMissingNoticeElement.setAttribute('class', 'notice notice-warning');
};
options._hideBlockMissingNotice = function () {
let blockMissingNoticeElement = document.getElementById('notice-block-missing');
blockMissingNoticeElement.setAttribute('class', 'notice notice-warning hidden');
};
options._renderLocaleNotice = function () {
let localeNoticeElement = document.getElementById('notice-locale');
localeNoticeElement.setAttribute('class', 'notice notice-default');
};
options._registerMiscellaneousEventListeners = function () {
2021-02-21 19:28:18 +01:00
let blockMissingButtonElement, changeEvent, optionBlockMissing;
2020-02-27 13:45:29 +01:00
2021-02-21 19:28:18 +01:00
blockMissingButtonElement = document.getElementById('button-block-missing');
changeEvent = new Event('change');
optionBlockMissing = options.getOptionElement('blockMissing');
2020-02-27 13:45:29 +01:00
2021-02-21 19:28:18 +01:00
blockMissingButtonElement.addEventListener('click', function () {
optionBlockMissing.checked = false;
optionBlockMissing.dispatchEvent(changeEvent);
2020-02-27 13:45:29 +01:00
});
};
options._determineOptionValues = function () {
return new Promise((resolve) => {
2021-02-21 19:28:18 +01:00
let nodeList, optionList, optionKeys;
nodeList = document.querySelectorAll('[data-option]');
optionList = {};
for (let element of nodeList) {
optionList[element.getAttribute('data-option')] = true;
}
delete optionList.internalStatistics;
2021-02-21 19:28:18 +01:00
optionKeys = Object.keys(optionList);
2020-02-27 13:45:29 +01:00
2022-12-03 06:22:57 +01:00
if (options._storageType === 'sync') {
chrome.storage.sync.get(optionKeys, function (items) {
options._optionValues = items;
resolve();
});
} else {
chrome.storage.local.get(optionKeys, function (items) {
options._optionValues = items;
resolve();
});
}
2020-02-27 13:45:29 +01:00
});
};
2020-08-08 07:28:41 +02:00
options._determineLocalOptionValues = function () {
return new Promise((resolve) => {
2020-08-30 18:56:36 +02:00
chrome.storage.local.get([Setting.INTERNAL_STATISTICS, Setting.STORAGE_TYPE], function (items) {
2020-08-08 07:28:41 +02:00
options._internalStatistics = items.internalStatistics;
2020-08-30 18:56:36 +02:00
options._storageType = items.storageType;
2020-08-08 07:28:41 +02:00
resolve();
});
});
};
2021-02-21 19:28:18 +01:00
options.getOptionElement = function (optionKey) {
2020-02-27 13:45:29 +01:00
return document.querySelector(`[data-option=${optionKey}]`);
};
options._configureLinkPrefetching = function (value) {
if (value === false) {
// Restore default values of related preference values.
chrome.privacy.network.networkPredictionEnabled.clear({});
} else {
chrome.privacy.network.networkPredictionEnabled.set({
2021-02-17 07:01:08 +01:00
'value': false,
2020-02-27 13:45:29 +01:00
});
}
};
2020-10-17 07:09:30 +02:00
options._serializeAllowlistedDomains = function (allowlistedDomains) {
if (allowlistedDomains === undefined || allowlistedDomains === null) {
2021-02-17 07:01:08 +01:00
return '';
}
2020-10-17 07:09:30 +02:00
let domainAllowlist, allowlistedDomainKeys;
2020-02-27 13:45:29 +01:00
2022-04-22 06:12:23 +02:00
allowlistedDomainKeys = Object.keys(allowlistedDomains).sort();
2020-10-17 07:09:30 +02:00
domainAllowlist = '';
2020-02-27 13:45:29 +01:00
2020-10-17 07:09:30 +02:00
allowlistedDomainKeys.forEach(function (domain) {
domainAllowlist = `${domainAllowlist}${domain}\n`;
2020-02-27 13:45:29 +01:00
});
2020-10-17 07:09:30 +02:00
domainAllowlist = domainAllowlist.slice(0, -1);
domainAllowlist = domainAllowlist.replace(Allowlist.TRIM_EXPRESSION, '');
2020-02-27 13:45:29 +01:00
2020-10-17 07:09:30 +02:00
return domainAllowlist;
2020-02-27 13:45:29 +01:00
};
2020-10-17 07:09:30 +02:00
options._parseDomainAllowlist = function (domainAllowlist) {
let allowlistedDomains = {};
2020-02-27 13:45:29 +01:00
2020-10-17 07:09:30 +02:00
domainAllowlist.split(Allowlist.VALUE_SEPARATOR).forEach(function (domain) {
allowlistedDomains[helpers.normalizeDomain(domain)] = true;
2020-02-27 13:45:29 +01:00
});
2020-10-17 07:09:30 +02:00
return allowlistedDomains;
2020-02-27 13:45:29 +01:00
};
2021-02-17 07:01:08 +01:00
2020-02-27 13:45:29 +01:00
/**
* Event Handlers
*/
options._onDocumentLoaded = function () {
let language = navigator.language;
options._languageSupported = helpers.languageIsFullySupported(language);
options._scriptDirection = helpers.determineScriptDirection(language);
options._renderContents();
};
2021-02-21 19:28:18 +01:00
options.onOptionChanged = function ({target}) {
2020-08-30 18:56:36 +02:00
let optionKey, optionType, optionValue;
2020-02-27 13:45:29 +01:00
optionKey = target.getAttribute('data-option');
optionType = target.getAttribute('type');
2020-06-21 08:18:18 +02:00
if (optionType === 'checkbox') {
2020-02-27 13:45:29 +01:00
optionValue = target.checked;
2020-06-21 08:18:18 +02:00
} else {
2020-02-27 13:45:29 +01:00
optionValue = target.value;
}
2021-02-21 19:28:18 +01:00
switch (optionKey) {
case Setting.UPDATE_NOTIFICATION:
optionValue = parseInt(optionValue);
break;
case Setting.BLOCK_MISSING:
if (optionValue === true) {
options._renderBlockMissingNotice();
} else {
options._hideBlockMissingNotice();
}
break;
case Setting.DISABLE_PREFETCH:
options._configureLinkPrefetching(optionValue);
break;
case Setting.ALLOWLISTED_DOMAINS:
case Setting.DOMAINS_MANIPULATE_DOM:
case Setting.ALLOWED_DOMAINS_GOOGLE_FONTS:
optionValue = options._parseDomainAllowlist(optionValue);
break;
case Setting.BLOCK_GOOGLE_FONTS:
optionsAdvanced._renderAdvancedSection(optionValue);
break;
case Setting.NEGATE_HTML_FILTER_LIST:
optionsAdvanced._renderHtmlFilterSection(optionValue);
break;
case Setting.SELECTED_ICON:
optionsOther._setIcon(optionValue);
break;
case Setting.LOGGING:
if (optionValue === false) {
chrome.runtime.sendMessage({'topic': 'logs:delete', 'value': ''});
}
break;
}
if (options._storageType === 'sync' && optionKey !== 'internalStatistics') {
2022-12-03 06:22:57 +01:00
chrome.storage.sync.set({
[optionKey]: optionValue,
});
} else {
chrome.storage.local.set({
[optionKey]: optionValue,
});
}
2020-02-27 13:45:29 +01:00
};
2020-08-30 19:07:14 +02:00
options._onLinkClick = function (url) {
chrome.tabs.create({
2021-02-17 07:01:08 +01:00
'url': url,
'active': true
});
2020-06-30 18:44:50 +02:00
};
2021-02-17 07:01:08 +01:00
options._changeTab = function ({target}) {
2020-08-30 19:07:14 +02:00
let tabContent, tabButton, optionKey;
2020-06-28 14:59:07 +02:00
2021-02-21 19:28:18 +01:00
optionKey = target.getAttribute('data-option-tab');
2020-08-30 19:07:14 +02:00
tabContent = document.getElementsByClassName('tab-content');
tabButton = document.getElementsByClassName('option-buttons');
2020-08-30 19:07:14 +02:00
for (let i = 0; i < tabContent.length; i++) {
if (tabContent[i].id === optionKey) {
tabContent[i].style.display = 'block';
tabButton[i].classList.add('option-buttons-active');
} else {
tabContent[i].style.display = 'none';
tabButton[i].classList.remove('option-buttons-active');
}
}
2020-08-08 07:28:41 +02:00
};
2020-02-27 13:45:29 +01:00
/**
* Initializations
*/
2021-02-21 19:28:18 +01:00
2020-08-30 18:56:36 +02:00
options._storageType = 'local';
2020-02-27 13:45:29 +01:00
document.addEventListener('DOMContentLoaded', options._onDocumentLoaded);