2020-02-27 13:45:29 +01:00
|
|
|
|
/**
|
|
|
|
|
* State Manager
|
|
|
|
|
* Belongs to Decentraleyes.
|
|
|
|
|
*
|
|
|
|
|
* @author Thomas Rientjes
|
|
|
|
|
* @since 2017-03-10
|
2020-04-30 07:31:41 +02:00
|
|
|
|
*
|
|
|
|
|
* @author nobody42
|
|
|
|
|
* @since 2020-02-26
|
|
|
|
|
*
|
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';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* State Manager
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
var stateManager = {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Public Methods
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
stateManager.registerInjection = function (tabIdentifier, injection) {
|
|
|
|
|
|
|
|
|
|
let injectionIdentifier, registeredTab, injectionCount;
|
|
|
|
|
|
|
|
|
|
injectionIdentifier = injection.source + injection.path + injection.version;
|
|
|
|
|
registeredTab = stateManager.tabs[tabIdentifier];
|
|
|
|
|
|
|
|
|
|
registeredTab.injections[injectionIdentifier] = injection;
|
|
|
|
|
injectionCount = Object.keys(registeredTab.injections).length || 0;
|
|
|
|
|
|
|
|
|
|
if (injectionCount > 0) {
|
|
|
|
|
|
|
|
|
|
chrome.browserAction.setTitle({
|
|
|
|
|
'tabId': tabIdentifier,
|
|
|
|
|
'title': `LocalCDN (${injectionCount})`
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (stateManager.showIconBadge === true) {
|
|
|
|
|
|
|
|
|
|
wrappers.setBadgeText({
|
|
|
|
|
'tabId': tabIdentifier,
|
|
|
|
|
'text': injectionCount.toString()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isNaN(interceptor.amountInjected)) {
|
|
|
|
|
|
2020-06-08 11:38:52 +02:00
|
|
|
|
chrome.storage.sync.get(Setting.AMOUNT_INJECTED, function (items) {
|
2020-02-27 13:45:29 +01:00
|
|
|
|
|
|
|
|
|
interceptor.amountInjected = items.amountInjected;
|
|
|
|
|
|
2020-06-08 11:38:52 +02:00
|
|
|
|
chrome.storage.sync.set({
|
2020-02-27 13:45:29 +01:00
|
|
|
|
[Setting.AMOUNT_INJECTED]: ++interceptor.amountInjected
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
2020-06-08 11:38:52 +02:00
|
|
|
|
chrome.storage.sync.set({
|
2020-02-27 13:45:29 +01:00
|
|
|
|
[Setting.AMOUNT_INJECTED]: ++interceptor.amountInjected
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stateManager.addDomainToWhitelist = function (domain) {
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
|
|
|
|
|
let whitelistedDomains = requestAnalyzer.whitelistedDomains;
|
|
|
|
|
whitelistedDomains[domain] = true;
|
|
|
|
|
|
2020-06-08 11:38:52 +02:00
|
|
|
|
chrome.storage.sync.set({whitelistedDomains}, resolve);
|
2020-02-27 13:45:29 +01:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stateManager.removeDomainFromWhitelist = function (domain) {
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
|
|
|
|
|
let whitelistedDomains = requestAnalyzer.whitelistedDomains;
|
|
|
|
|
delete whitelistedDomains[domain];
|
|
|
|
|
|
2020-06-08 11:38:52 +02:00
|
|
|
|
chrome.storage.sync.set({whitelistedDomains}, resolve);
|
2020-02-27 13:45:29 +01:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-25 22:30:44 +02:00
|
|
|
|
stateManager.addDomainToManipulateDOMlist = function (domain) {
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
|
2020-06-05 07:28:26 +02:00
|
|
|
|
let domainsManipulateDOM = requestAnalyzer.domainsManipulateDOM;
|
2020-05-25 22:30:44 +02:00
|
|
|
|
domainsManipulateDOM[domain] = true;
|
|
|
|
|
|
2020-06-08 11:38:52 +02:00
|
|
|
|
chrome.storage.sync.set({domainsManipulateDOM}, resolve);
|
2020-05-25 22:30:44 +02:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stateManager.removeDomainFromManipulateDOMlist = function (domain) {
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
|
2020-06-05 07:28:26 +02:00
|
|
|
|
let domainsManipulateDOM = requestAnalyzer.domainsManipulateDOM;
|
2020-05-25 22:30:44 +02:00
|
|
|
|
delete domainsManipulateDOM[domain];
|
|
|
|
|
|
2020-06-08 11:38:52 +02:00
|
|
|
|
chrome.storage.sync.set({domainsManipulateDOM}, resolve);
|
2020-05-25 22:30:44 +02:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
|
/**
|
|
|
|
|
* Private Methods
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
stateManager._createTab = function (tab) {
|
|
|
|
|
|
|
|
|
|
let tabIdentifier, requestFilters;
|
|
|
|
|
|
|
|
|
|
tabIdentifier = tab.id;
|
|
|
|
|
|
|
|
|
|
stateManager.tabs[tabIdentifier] = {
|
|
|
|
|
'injections': {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
requestFilters = {
|
|
|
|
|
'tabId': tabIdentifier,
|
|
|
|
|
'urls': stateManager.validHosts
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
chrome.webRequest.onBeforeRequest.addListener(function (requestDetails) {
|
|
|
|
|
|
2020-06-21 08:18:18 +02:00
|
|
|
|
tab = stateManager.tabs[tabIdentifier].details || {};
|
2020-02-27 13:45:29 +01:00
|
|
|
|
return interceptor.handleRequest(requestDetails, tabIdentifier, tab);
|
|
|
|
|
|
|
|
|
|
}, requestFilters, [WebRequest.BLOCKING]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stateManager._removeTab = function (tabIdentifier) {
|
|
|
|
|
delete stateManager.tabs[tabIdentifier];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stateManager._updateTab = function (details) {
|
|
|
|
|
|
|
|
|
|
let tabDomain, domainIsWhitelisted, frameIdentifier, tabIdentifier;
|
|
|
|
|
|
|
|
|
|
tabDomain = helpers.extractDomainFromUrl(details.url, true);
|
2020-05-26 20:26:15 +02:00
|
|
|
|
domainIsWhitelisted = stateManager._domainIsListed(tabDomain);
|
2020-02-27 13:45:29 +01:00
|
|
|
|
|
|
|
|
|
frameIdentifier = details.frameId;
|
|
|
|
|
tabIdentifier = details.tabId;
|
|
|
|
|
|
|
|
|
|
if (frameIdentifier !== 0 || tabIdentifier === -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chrome.browserAction.setTitle({
|
|
|
|
|
'tabId': tabIdentifier,
|
|
|
|
|
'title': 'LocalCDN (0)'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (domainIsWhitelisted) {
|
|
|
|
|
|
|
|
|
|
stateManager._setIconDisabled(tabIdentifier);
|
|
|
|
|
|
|
|
|
|
chrome.browserAction.setTitle({
|
|
|
|
|
'tabId': tabIdentifier,
|
|
|
|
|
'title': 'LocalCDN (–)'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stateManager.showIconBadge === true) {
|
|
|
|
|
stateManager._clearBadgeText(tabIdentifier);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stateManager.tabs[tabIdentifier]) {
|
|
|
|
|
stateManager.tabs[tabIdentifier].injections = {};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stateManager._handleStorageChanged = function (changes) {
|
|
|
|
|
|
|
|
|
|
if (Setting.SHOW_ICON_BADGE in changes) {
|
|
|
|
|
|
|
|
|
|
stateManager.showIconBadge = changes.showIconBadge.newValue;
|
|
|
|
|
|
|
|
|
|
if (changes.showIconBadge.newValue !== true) {
|
|
|
|
|
|
|
|
|
|
chrome.tabs.query({}, function (tabs) {
|
|
|
|
|
tabs.forEach(stateManager._removeIconBadgeFromTab);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Setting.STRIP_METADATA in changes) {
|
|
|
|
|
|
|
|
|
|
requestSanitizer.disable();
|
|
|
|
|
|
|
|
|
|
if (changes.stripMetadata.newValue !== false) {
|
|
|
|
|
requestSanitizer.enable();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-25 07:54:17 +02:00
|
|
|
|
if (Setting.NEGATE_HTML_FILTER_LIST in changes) {
|
|
|
|
|
stateManager.getInvertOption = changes.negateHtmlFilterList.newValue;
|
|
|
|
|
}
|
2020-02-27 13:45:29 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stateManager._clearBadgeText = function (tabIdentifier) {
|
|
|
|
|
|
|
|
|
|
wrappers.setBadgeText({
|
|
|
|
|
'tabId': tabIdentifier,
|
|
|
|
|
'text': ''
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stateManager._removeIconBadgeFromTab = function (tab) {
|
|
|
|
|
stateManager._clearBadgeText(tab.id);
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-27 18:33:29 +02:00
|
|
|
|
stateManager._domainIsListed = function (domain, listname) {
|
2020-02-27 13:45:29 +01:00
|
|
|
|
|
|
|
|
|
if (domain !== null) {
|
|
|
|
|
|
|
|
|
|
let whitelistRecord, isWhitelisted;
|
|
|
|
|
|
2020-05-25 22:53:29 +02:00
|
|
|
|
if (listname === "manipulate-dom") {
|
2020-06-05 07:28:26 +02:00
|
|
|
|
whitelistRecord = requestAnalyzer.domainsManipulateDOM[domain];
|
2020-05-25 22:53:29 +02:00
|
|
|
|
isWhitelisted = Boolean(whitelistRecord);
|
|
|
|
|
} else {
|
|
|
|
|
whitelistRecord = requestAnalyzer.whitelistedDomains[domain];
|
|
|
|
|
isWhitelisted = Boolean(whitelistRecord);
|
|
|
|
|
}
|
2020-02-27 13:45:29 +01:00
|
|
|
|
|
|
|
|
|
return isWhitelisted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stateManager._setIconDisabled = function (tabIdentifier) {
|
|
|
|
|
|
|
|
|
|
wrappers.setIcon({
|
|
|
|
|
'path': stateManager.disabledIconPath,
|
|
|
|
|
'tabId': tabIdentifier
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-30 19:13:27 +02:00
|
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
|
/**
|
|
|
|
|
* Initializations
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
stateManager.requests = {};
|
|
|
|
|
stateManager.tabs = {};
|
|
|
|
|
|
2020-06-25 07:54:17 +02:00
|
|
|
|
stateManager.getInvertOption;
|
2020-02-27 13:45:29 +01:00
|
|
|
|
stateManager.disabledIconPath = {
|
|
|
|
|
'16': chrome.runtime.getURL('icons/action/icon16-disabled.png'),
|
|
|
|
|
'18': chrome.runtime.getURL('icons/action/icon18-disabled.png'),
|
|
|
|
|
'19': chrome.runtime.getURL('icons/action/icon19-disabled.png'),
|
|
|
|
|
'32': chrome.runtime.getURL('icons/action/icon32-disabled.png'),
|
|
|
|
|
'36': chrome.runtime.getURL('icons/action/icon36-disabled.png'),
|
|
|
|
|
'38': chrome.runtime.getURL('icons/action/icon38-disabled.png'),
|
|
|
|
|
'64': chrome.runtime.getURL('icons/action/icon64-disabled.png')
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
stateManager.validHosts = [];
|
|
|
|
|
|
|
|
|
|
for (let mapping in mappings) {
|
|
|
|
|
|
|
|
|
|
let supportedHost = Address.ANY_PROTOCOL + mapping + Address.ANY_PATH;
|
|
|
|
|
stateManager.validHosts.push(supportedHost);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chrome.tabs.query({}, function (tabs) {
|
|
|
|
|
tabs.forEach(stateManager._createTab);
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-08 11:38:52 +02:00
|
|
|
|
chrome.storage.sync.get(Setting.SHOW_ICON_BADGE, function (items) {
|
2020-02-27 13:45:29 +01:00
|
|
|
|
|
|
|
|
|
if (items.showIconBadge === undefined) {
|
|
|
|
|
items.showIconBadge = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stateManager.showIconBadge = items.showIconBadge;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Event Handlers
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
chrome.tabs.onCreated.addListener(stateManager._createTab);
|
|
|
|
|
chrome.tabs.onRemoved.addListener(stateManager._removeTab);
|
|
|
|
|
|
|
|
|
|
chrome.webRequest.onBeforeRequest.addListener(function (requestDetails) {
|
|
|
|
|
|
|
|
|
|
if (requestDetails.tabId !== -1 && stateManager.tabs[requestDetails.tabId]) {
|
|
|
|
|
|
|
|
|
|
stateManager.tabs[requestDetails.tabId].details = {
|
|
|
|
|
'url': requestDetails.url
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 07:31:41 +02:00
|
|
|
|
}, {'types': [WebRequestType.MAIN_FRAME], 'urls': [Address.ANY]}, [WebRequest.BLOCKING]);
|
2020-02-27 13:45:29 +01:00
|
|
|
|
|
|
|
|
|
chrome.webNavigation.onCommitted.addListener(stateManager._updateTab, {
|
|
|
|
|
'url': [{'urlContains': ':'}]
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
chrome.webRequest.onErrorOccurred.addListener(function (requestDetails) {
|
|
|
|
|
|
|
|
|
|
if (stateManager.requests[requestDetails.requestId]) {
|
|
|
|
|
delete stateManager.requests[requestDetails.requestId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, {'urls': [Address.ANY]});
|
|
|
|
|
|
|
|
|
|
chrome.webRequest.onBeforeRedirect.addListener(function (requestDetails) {
|
|
|
|
|
|
|
|
|
|
let knownRequest = stateManager.requests[requestDetails.requestId];
|
|
|
|
|
|
|
|
|
|
if (knownRequest) {
|
|
|
|
|
|
|
|
|
|
stateManager.registerInjection(knownRequest.tabIdentifier, knownRequest.targetDetails);
|
|
|
|
|
delete stateManager.requests[requestDetails.requestId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, {'urls': [Address.ANY]});
|
|
|
|
|
|
|
|
|
|
chrome.storage.onChanged.addListener(stateManager._handleStorageChanged);
|