2020-08-30 18:35:08 +02:00
|
|
|
/**
|
|
|
|
* Storage Manager
|
|
|
|
* Belongs to LocalCDN
|
|
|
|
*
|
|
|
|
* @author nobody
|
|
|
|
* @since 2020-08-28
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
|
|
|
|
const InvalidFile = 'Invalid file!';
|
|
|
|
|
|
|
|
|
2020-08-30 18:35:08 +02:00
|
|
|
/**
|
|
|
|
* Storage Manager
|
|
|
|
*/
|
|
|
|
|
|
|
|
var storageManager = {};
|
|
|
|
|
2021-02-17 07:01:08 +01:00
|
|
|
|
2020-08-30 18:35:08 +02:00
|
|
|
/**
|
|
|
|
* Public Methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
storageManager.checkStorageType = function () {
|
|
|
|
chrome.storage.local.get([Setting.STORAGE_TYPE], function (items) {
|
|
|
|
if (items.storageType === 'sync') {
|
|
|
|
storageManager.type = chrome.storage.sync;
|
|
|
|
} else {
|
|
|
|
storageManager.type = chrome.storage.local;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
storageManager.migrateData = function (target) {
|
2020-09-06 11:19:35 +02:00
|
|
|
let storageSource, storageDestination;
|
2020-08-30 18:35:08 +02:00
|
|
|
|
|
|
|
if (target === 'local') {
|
|
|
|
storageSource = chrome.storage.sync;
|
|
|
|
storageDestination = chrome.storage.local;
|
2020-09-06 11:19:35 +02:00
|
|
|
} else if (target === 'sync') {
|
2020-08-30 18:35:08 +02:00
|
|
|
storageSource = chrome.storage.local;
|
|
|
|
storageDestination = chrome.storage.sync;
|
2020-09-06 11:19:35 +02:00
|
|
|
} else {
|
|
|
|
return;
|
2020-08-30 18:35:08 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 11:19:35 +02:00
|
|
|
storageSource.get(null, function (data) {
|
|
|
|
chrome.storage.local.set({
|
|
|
|
[Setting.AMOUNT_INJECTED]: data.amountInjected,
|
|
|
|
[Setting.INTERNAL_STATISTICS]: data.internalStatistics,
|
|
|
|
[Setting.INTERNAL_STATISTICS_DATA]: data.internalStatisticsData,
|
|
|
|
[Setting.STORAGE_TYPE]: target
|
|
|
|
});
|
|
|
|
storageDestination.set({
|
|
|
|
[Setting.ALLOWED_DOMAINS_GOOGLE_FONTS]: data.allowedDomainsGoogleFonts,
|
|
|
|
[Setting.BLOCK_GOOGLE_FONTS]: data.blockGoogleFonts,
|
|
|
|
[Setting.BLOCK_MISSING]: data.blockMissing,
|
|
|
|
[Setting.DISABLE_PREFETCH]: data.disablePrefetch,
|
|
|
|
[Setting.DOMAINS_MANIPULATE_DOM]: data.domainsManipulateDOM,
|
|
|
|
[Setting.LOGGING]: data.logging,
|
|
|
|
[Setting.ENFORCE_STAGING]: data.enforceStaging,
|
2020-11-14 12:31:17 +01:00
|
|
|
[Setting.UPDATE_NOTIFICATION]: data.updateNotification,
|
2020-09-06 11:19:35 +02:00
|
|
|
[Setting.LAST_MAPPING_UPDATE]: data.lastMappingUpdate,
|
|
|
|
[Setting.NEGATE_HTML_FILTER_LIST]: data.negateHtmlFilterList,
|
|
|
|
[Setting.SELECTED_ICON]: data.selectedIcon,
|
|
|
|
[Setting.SHOW_ICON_BADGE]: data.showIconBadge,
|
|
|
|
[Setting.STRIP_METADATA]: data.stripMetadata,
|
2020-10-17 07:09:30 +02:00
|
|
|
[Setting.ALLOWLISTED_DOMAINS]: data.allowlistedDomains,
|
2020-09-25 16:15:50 +02:00
|
|
|
[Setting.XHR_TEST_DOMAIN]: data.xhrTestDomain,
|
2021-08-11 06:29:09 +02:00
|
|
|
[Setting.BADGE_DEFAULT_BACKGROUND_COLOR]: data.badgeDefaultBackgroundColor,
|
|
|
|
[Setting.BADGE_DEFAULT_TEXT_COLOR]: data.badgeDefaultTextColor,
|
|
|
|
[Setting.BADGE_HTML_FILTER_BACKGROUND_COLOR]: data.badgeHTMLFilterBackgroundColor,
|
|
|
|
[Setting.BADGE_HTML_FILTER_TEXT_COLOR]: data.badgeHTMLfilterTextColor
|
2020-09-06 11:19:35 +02:00
|
|
|
});
|
2020-08-30 18:35:08 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
storageManager.export = function () {
|
|
|
|
let filename = new Date().toISOString();
|
2021-02-17 07:01:08 +01:00
|
|
|
filename = `${filename.substring(0, 10)}_localcdn_backup.txt`;
|
2020-08-30 18:35:08 +02:00
|
|
|
|
|
|
|
storageManager.type.get(null, function (items) {
|
2020-11-12 06:57:00 +01:00
|
|
|
delete items['whitelistedDomains'];
|
2020-08-30 18:35:08 +02:00
|
|
|
let element = document.createElement('a');
|
2021-02-17 07:01:08 +01:00
|
|
|
element.setAttribute('href', `data:text/plain;charset=utf-8,${encodeURIComponent(JSON.stringify(items, null, ' '))}`);
|
2020-08-30 18:35:08 +02:00
|
|
|
element.setAttribute('download', filename);
|
|
|
|
element.style.display = 'none';
|
|
|
|
document.body.appendChild(element);
|
|
|
|
element.click();
|
|
|
|
document.body.removeChild(element);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
storageManager.startImportFilePicker = function () {
|
|
|
|
const input = document.getElementById('import-file-picker');
|
|
|
|
input.value = '';
|
|
|
|
input.click();
|
|
|
|
};
|
|
|
|
|
2021-02-17 07:02:14 +01:00
|
|
|
storageManager.handleImportFilePicker = function () {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
try {
|
|
|
|
let file = document.getElementById('import-file-picker').files[0];
|
|
|
|
storageManager._readFile(file)
|
|
|
|
.then(JSON.parse)
|
|
|
|
.then(storageManager._validation);
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`[ LocalCDN ] ${err}`);
|
|
|
|
alert(err);
|
|
|
|
}
|
|
|
|
});
|
2020-08-30 18:35:08 +02:00
|
|
|
};
|
|
|
|
|
2021-02-17 07:02:14 +01:00
|
|
|
|
2020-08-30 18:35:08 +02:00
|
|
|
/**
|
|
|
|
* Private Methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
storageManager._handleStorageChanged = function (type) {
|
|
|
|
if (Setting.STORAGE_TYPE in type) {
|
|
|
|
if (type.storageType.newValue === 'sync') {
|
|
|
|
storageManager.type = chrome.storage.sync;
|
|
|
|
} else {
|
|
|
|
storageManager.type = chrome.storage.local;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-17 07:02:14 +01:00
|
|
|
storageManager._readFile = function (file) {
|
2020-08-30 18:35:08 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let reader = new FileReader();
|
|
|
|
reader.onload = () => {
|
|
|
|
resolve(reader.result);
|
|
|
|
};
|
|
|
|
reader.onerror = reject;
|
|
|
|
reader.readAsText(file);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
storageManager._validation = function (content) {
|
|
|
|
let imported = {};
|
2020-11-12 06:57:00 +01:00
|
|
|
|
2020-11-15 12:30:24 +01:00
|
|
|
// Delete old keys
|
2021-02-07 09:54:18 +01:00
|
|
|
if (content.whitelistedDomains !== undefined) {
|
2020-11-15 12:30:24 +01:00
|
|
|
content.allowlistedDomains = content.whitelistedDomains;
|
|
|
|
delete content['whitelistedDomains'];
|
2020-11-12 06:57:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-15 12:30:24 +01:00
|
|
|
// Convert value of notifications
|
2021-02-07 09:54:18 +01:00
|
|
|
if (content.hideReleaseNotes !== undefined) {
|
2020-11-14 12:31:17 +01:00
|
|
|
content.updateNotification = content.hideReleaseNotes ? 0 : 2;
|
|
|
|
delete content['hideReleaseNotes'];
|
|
|
|
}
|
|
|
|
|
2020-12-06 07:54:26 +01:00
|
|
|
// Convert type of updateNotification
|
|
|
|
if (typeof content.updateNotification === 'string') {
|
|
|
|
content.updateNotification = parseInt(content.updateNotification);
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:29:09 +02:00
|
|
|
// Migrate badge colors
|
|
|
|
if (content.badgeDefaultBackgroundColor === undefined) {
|
|
|
|
content.badgeDefaultBackgroundColor = content.badgeColor;
|
|
|
|
}
|
|
|
|
if (content.badgeDefaultTextColor === undefined) {
|
|
|
|
content.badgeDefaultTextColor = content.badgeTextColor;
|
|
|
|
}
|
|
|
|
|
2020-08-30 18:35:08 +02:00
|
|
|
for (const [key, value] of Object.entries(SettingDefaults)) {
|
|
|
|
// If type the same as default settings
|
|
|
|
if (typeof value === typeof content[key]) {
|
2020-11-15 12:30:24 +01:00
|
|
|
if (typeof value === 'object') {
|
2020-08-30 18:35:08 +02:00
|
|
|
imported[key] = storageManager._validateDomainsAndStatistics(key, content[key]);
|
2020-11-15 12:30:24 +01:00
|
|
|
} else if (typeof value === 'string') {
|
2020-08-30 18:35:08 +02:00
|
|
|
imported[key] = storageManager._validateStrings(content[key]);
|
2020-11-15 12:30:24 +01:00
|
|
|
} else if (typeof value === 'number') {
|
2020-08-30 18:35:08 +02:00
|
|
|
imported[key] = storageManager._validateNumbers(content[key]);
|
2020-11-15 12:30:24 +01:00
|
|
|
} else if (typeof value === 'boolean') {
|
2020-09-08 20:47:55 +02:00
|
|
|
imported[key] = content[key];
|
2020-08-30 18:35:08 +02:00
|
|
|
}
|
2020-09-08 20:49:10 +02:00
|
|
|
} else if (content[key] === undefined) {
|
|
|
|
// Set default if not existing in file
|
|
|
|
imported[key] = value;
|
2020-08-30 18:35:08 +02:00
|
|
|
} else {
|
2021-02-17 07:01:08 +01:00
|
|
|
alert(`${chrome.i18n.getMessage('dialogImportFailed')}\n\n${key}: ${content[key]}`);
|
|
|
|
throw InvalidFile;
|
2020-08-30 18:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 20:50:54 +02:00
|
|
|
|
2020-08-30 18:35:08 +02:00
|
|
|
storageManager.type.set(imported);
|
2020-09-08 20:50:54 +02:00
|
|
|
|
2020-08-30 18:35:08 +02:00
|
|
|
alert(chrome.i18n.getMessage('dialogImportSuccessful'));
|
2021-08-12 06:37:45 +02:00
|
|
|
chrome.runtime.reload();
|
2020-08-30 18:35:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
storageManager._validateDomainsAndStatistics = function (type, obj) {
|
|
|
|
let valid = {};
|
2021-02-17 07:01:08 +01:00
|
|
|
|
2020-10-17 07:09:30 +02:00
|
|
|
if (type === 'allowedDomainsGoogleFonts' || type === 'domainsManipulateDOM' || type === 'allowlistedDomains') {
|
2020-08-30 18:35:08 +02:00
|
|
|
for (const [key, value] of Object.entries(obj)) {
|
2021-02-17 07:01:08 +01:00
|
|
|
// eslint-disable-next-line no-useless-escape
|
2022-07-28 06:11:44 +02:00
|
|
|
if (((/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,24}/).test(key) || key === '') && value === true) {
|
2020-08-30 18:35:08 +02:00
|
|
|
valid[key] = value;
|
|
|
|
} else {
|
2021-02-17 07:01:08 +01:00
|
|
|
alert(`${chrome.i18n.getMessage('dialogImportFailed')}: ${key}`);
|
|
|
|
throw InvalidFile;
|
2020-08-30 18:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type === 'internalStatisticsData') {
|
|
|
|
for (const [date, values] of Object.entries(obj)) {
|
2022-07-28 06:11:44 +02:00
|
|
|
if ((/((2\d)\d{2})-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])/).test(date)) {
|
2020-08-30 18:35:08 +02:00
|
|
|
for (const [types, category] of Object.entries(values)) {
|
|
|
|
if (types === 'frameworks') {
|
|
|
|
for (const [name, counter] of Object.entries(category)) {
|
2021-02-17 07:01:08 +01:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
if (!(/resources\/[0-9a-z.-]+\/((?:\d{1,2}\.){1,3}\d{1,2})?.*\.(css|jsm)/).test(name) && !storageManager._validateNumbers(counter)) {
|
|
|
|
alert(`${chrome.i18n.getMessage('dialogImportFailed')}: ${name}`);
|
|
|
|
throw InvalidFile;
|
2020-08-30 18:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (types === 'cdns') {
|
|
|
|
for (const [name, counter] of Object.entries(category)) {
|
2021-02-17 07:01:08 +01:00
|
|
|
// eslint-disable-next-line no-useless-escape, max-len
|
|
|
|
if (!(/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,24}/).test(name) && !storageManager._validateNumbers(counter)) {
|
|
|
|
alert(`${chrome.i18n.getMessage('dialogImportFailed')}: ${name}`);
|
|
|
|
throw InvalidFile;
|
2020-08-30 18:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-17 07:01:08 +01:00
|
|
|
alert(`${chrome.i18n.getMessage('dialogImportFailed')}: ${type}`);
|
|
|
|
throw InvalidFile;
|
2020-08-30 18:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-17 07:01:08 +01:00
|
|
|
alert(`${chrome.i18n.getMessage('dialogImportFailed')}: ${date}`);
|
|
|
|
throw InvalidFile;
|
2020-08-30 18:35:08 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 20:53:48 +02:00
|
|
|
valid = obj;
|
2020-08-30 18:35:08 +02:00
|
|
|
} else {
|
2021-02-17 07:01:08 +01:00
|
|
|
alert(`${chrome.i18n.getMessage('dialogImportFailed')}: ${type}`);
|
|
|
|
throw InvalidFile;
|
2020-08-30 18:35:08 +02:00
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
};
|
|
|
|
|
|
|
|
storageManager._validateStrings = function (value) {
|
2022-07-28 06:11:44 +02:00
|
|
|
if ((/((2\d)\d{2})-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])/).test(value)) {
|
2020-08-30 18:35:08 +02:00
|
|
|
return value;
|
2022-07-28 06:11:44 +02:00
|
|
|
} else if ((/#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/).test(value)) {
|
2020-09-25 16:15:50 +02:00
|
|
|
return value;
|
2020-08-30 18:35:08 +02:00
|
|
|
} else if (value === 'Default' || value === 'Light' || value === 'Grey') {
|
|
|
|
return value;
|
|
|
|
} else if (value === 'local' || value === 'sync') {
|
|
|
|
return value;
|
|
|
|
} else if (value === 'decentraleyes.org' || value === 'localcdn.org') {
|
|
|
|
return 'localcdn.org';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
storageManager._validateNumbers = function (value) {
|
2020-09-08 20:53:04 +02:00
|
|
|
return isNaN(value) ? 0 : value;
|
2020-08-30 18:35:08 +02:00
|
|
|
};
|
|
|
|
|
2021-02-17 07:01:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializations
|
|
|
|
*/
|
|
|
|
|
2020-08-30 18:35:08 +02:00
|
|
|
storageManager.data = {};
|
|
|
|
storageManager.type = chrome.storage.local;
|
2020-09-08 20:50:54 +02:00
|
|
|
storageManager.amountInjected = 0;
|
|
|
|
storageManager.statistics = {};
|
2020-08-30 18:35:08 +02:00
|
|
|
|
|
|
|
chrome.storage.onChanged.addListener(storageManager._handleStorageChanged);
|