Show badge if no injections (#301)

This commit is contained in:
nobody 2021-03-13 06:55:20 +01:00
parent 3779e1642a
commit 294171707c
No known key found for this signature in database
GPG Key ID: 8F6DE3D614FCFD7A
3 changed files with 61 additions and 31 deletions

View File

@ -63,7 +63,7 @@ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
}
if (!targetDetails) {
return interceptor._handleMissingCandidate(requestDetails.url);
return interceptor._handleMissingCandidate(requestDetails.url, tabIdentifier);
}
stateManager.requests[requestDetails.requestId] = {
@ -80,8 +80,21 @@ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
* Private Methods
*/
interceptor._handleMissingCandidate = function (requestUrl, preserveUrl) {
let requestUrlSegments;
interceptor._handleMissingCandidate = function (requestUrl, tabIdentifier) {
let requestUrlSegments, injectionCount, missingCount;
if (stateManager.showIconBadge === true) {
if (stateManager.changeBadgeColorMissingResources === true) {
missingCount = stateManager.tabs[tabIdentifier].missing || 0;
injectionCount = Object.keys(stateManager.tabs[tabIdentifier].injections).length || 0;
if (missingCount > 0 && injectionCount === 0) {
wrappers.setBadgeMissing(tabIdentifier, injectionCount);
}
} else {
wrappers.defaultBadge(tabIdentifier);
}
}
if (interceptor.blockMissing === true) {
return {
@ -89,12 +102,6 @@ interceptor._handleMissingCandidate = function (requestUrl, preserveUrl) {
};
}
if (preserveUrl === true) {
return {
'cancel': false
};
}
requestUrlSegments = new URL(requestUrl);
if (requestUrlSegments.protocol === Address.HTTP) {

View File

@ -54,27 +54,13 @@ stateManager.registerInjection = function (tabIdentifier, injection) {
});
}
}
if (missingCount > 0) {
chrome.browserAction.setBadgeTextColor({
'tabId': tabIdentifier,
'color': 'black'
});
chrome.browserAction.setBadgeBackgroundColor({
'tabId': tabIdentifier,
'color': 'yellow'
});
} else {
chrome.browserAction.setBadgeTextColor({
'tabId': tabIdentifier,
'color': wrappers.textColor
});
chrome.browserAction.setBadgeBackgroundColor({
'tabId': tabIdentifier,
'color': wrappers.backgroundColor
});
if (stateManager.showIconBadge === true) {
if (missingCount > 0 && stateManager.changeBadgeColorMissingResources) {
wrappers.setBadgeMissing(tabIdentifier, injectionCount);
} else {
wrappers.defaultBadge(tabIdentifier, injectionCount);
}
}
if (isNaN(storageManager.amountInjected)) {
storageManager.type.get(Setting.AMOUNT_INJECTED, function (items) {
storageManager.amountInjected = items.amountInjected;
@ -171,7 +157,6 @@ stateManager._removeTab = function (tabIdentifier) {
stateManager._updateTab = function (details) {
let tabDomain, domainIsAllowlisted, frameIdentifier, tabIdentifier;
tabDomain = helpers.extractDomainFromUrl(details.url, true);
domainIsAllowlisted = stateManager._domainIsListed(tabDomain);
frameIdentifier = details.frameId;
@ -227,6 +212,8 @@ stateManager._handleStorageChanged = function (changes) {
stats.data = changes.internalStatisticsData.newValue;
} else if (Setting.HIDE_DONATION_BUTTON in changes) {
stateManager.hideDonationButton = changes.hideDonationButton.newValue;
} else if (Setting.CHANGE_BADGE_COLOR_MISSING_RESOURCES in changes) {
stateManager.changeBadgeColorMissingResources = changes.changeBadgeColorMissingResources.newValue;
}
};
@ -276,6 +263,7 @@ stateManager.validHosts = [];
stateManager.selectedIcon = 'Default';
stateManager.internalStatistics = false;
stateManager.hideDonationButton = false;
stateManager.changeBadgeColorMissingResources = false;
for (let mapping in mappings.cdn) {
let supportedHost = Address.ANY_PROTOCOL + mapping + Address.ANY_PATH;
@ -286,7 +274,11 @@ chrome.tabs.query({}, function (tabs) {
tabs.forEach(stateManager._createTab);
});
storageManager.type.get([Setting.SHOW_ICON_BADGE, Setting.SELECTED_ICON], function (items) {
storageManager.type.get([
Setting.SHOW_ICON_BADGE,
Setting.SELECTED_ICON,
Setting.CHANGE_BADGE_COLOR_MISSING_RESOURCES
], function (items) {
if (items.showIconBadge === undefined) {
items.showIconBadge = true;
}
@ -295,6 +287,7 @@ storageManager.type.get([Setting.SHOW_ICON_BADGE, Setting.SELECTED_ICON], functi
}
stateManager.showIconBadge = items.showIconBadge;
stateManager.selectedIcon = items.selectedIcon;
stateManager.changeBadgeColorMissingResources = items.changeBadgeColorMissingResources;
});
chrome.storage.local.get([Setting.INTERNAL_STATISTICS], function (items) {

View File

@ -62,6 +62,36 @@ wrappers.setIcon = function (details, type) {
}
};
wrappers.setBadgeMissing = function (tabIdentifier, counter) {
chrome.browserAction.setBadgeText({
'tabId': tabIdentifier,
'text': `${counter}`,
});
chrome.browserAction.setBadgeTextColor({
'tabId': tabIdentifier,
'color': 'black',
});
chrome.browserAction.setBadgeBackgroundColor({
'tabId': tabIdentifier,
'color': 'yellow',
});
};
wrappers.defaultBadge = function (tabIdentifier, counter) {
chrome.browserAction.setBadgeText({
'tabId': tabIdentifier,
'text': `${counter}`,
});
chrome.browserAction.setBadgeTextColor({
'tabId': tabIdentifier,
'color': wrappers.textColor
});
chrome.browserAction.setBadgeBackgroundColor({
'tabId': tabIdentifier,
'color': wrappers.backgroundColor
});
};
storageManager.type.get([Setting.BADGE_COLOR, Setting.BADGE_TEXT_COLOR], function (items) {
wrappers.textColor = items.badgeTextColor || '#FFFFFF';
wrappers.backgroundColor = items.badgeColor || '#4A826C';