2020-02-24 23:16:45 +00:00
|
|
|
/* global prefs debounce iconUtil FIREFOX CHROME VIVALDI tabManager navigatorUtil API_METHODS */
|
2020-02-23 15:43:26 +00:00
|
|
|
/* exported iconManager */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const iconManager = (() => {
|
2020-05-31 05:43:56 +00:00
|
|
|
const ICON_SIZES = FIREFOX || CHROME >= 55 && !VIVALDI ? [16, 32] : [19, 38];
|
2020-02-24 23:16:45 +00:00
|
|
|
const staleBadges = new Set();
|
2020-02-23 15:43:26 +00:00
|
|
|
|
|
|
|
prefs.subscribe([
|
|
|
|
'disableAll',
|
|
|
|
'badgeDisabled',
|
|
|
|
'badgeNormal',
|
|
|
|
], () => debounce(refreshIconBadgeColor));
|
|
|
|
|
|
|
|
prefs.subscribe([
|
2020-11-18 11:17:15 +00:00
|
|
|
'show-badge',
|
2020-02-23 15:43:26 +00:00
|
|
|
], () => debounce(refreshAllIconsBadgeText));
|
|
|
|
|
|
|
|
prefs.subscribe([
|
|
|
|
'disableAll',
|
|
|
|
'iconset',
|
|
|
|
], () => debounce(refreshAllIcons));
|
|
|
|
|
|
|
|
prefs.initializing.then(() => {
|
|
|
|
refreshIconBadgeColor();
|
|
|
|
refreshAllIconsBadgeText();
|
|
|
|
refreshAllIcons();
|
|
|
|
});
|
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
Object.assign(API_METHODS, {
|
|
|
|
/** @param {(number|string)[]} styleIds
|
|
|
|
* @param {boolean} [lazyBadge=false] preventing flicker during page load */
|
|
|
|
updateIconBadge(styleIds, {lazyBadge} = {}) {
|
|
|
|
// FIXME: in some cases, we only have to redraw the badge. is it worth a optimization?
|
|
|
|
const {frameId, tab: {id: tabId}} = this.sender;
|
|
|
|
const value = styleIds.length ? styleIds.map(Number) : undefined;
|
|
|
|
tabManager.set(tabId, 'styleIds', frameId, value);
|
|
|
|
debounce(refreshStaleBadges, frameId && lazyBadge ? 250 : 0);
|
|
|
|
staleBadges.add(tabId);
|
|
|
|
if (!frameId) refreshIcon(tabId, true);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
navigatorUtil.onCommitted(({tabId, frameId}) => {
|
|
|
|
if (!frameId) tabManager.set(tabId, 'styleIds', undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
chrome.runtime.onConnect.addListener(port => {
|
|
|
|
if (port.name === 'iframe') {
|
|
|
|
port.onDisconnect.addListener(onPortDisconnected);
|
|
|
|
}
|
|
|
|
});
|
2020-02-23 15:43:26 +00:00
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
function onPortDisconnected({sender}) {
|
|
|
|
if (tabManager.get(sender.tab.id, 'styleIds')) {
|
|
|
|
API_METHODS.updateIconBadge.call({sender}, [], {lazyBadge: true});
|
|
|
|
}
|
2020-02-23 15:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function refreshIconBadgeText(tabId) {
|
2020-02-24 23:16:45 +00:00
|
|
|
const text = prefs.get('show-badge') ? `${getStyleCount(tabId)}` : '';
|
|
|
|
iconUtil.setBadgeText({tabId, text});
|
2020-02-23 15:43:26 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
function getIconName(hasStyles = false) {
|
2020-02-23 15:43:26 +00:00
|
|
|
const iconset = prefs.get('iconset') === 1 ? 'light/' : '';
|
2020-02-24 23:16:45 +00:00
|
|
|
const postfix = prefs.get('disableAll') ? 'x' : !hasStyles ? 'w' : '';
|
2020-02-23 15:43:26 +00:00
|
|
|
return `${iconset}$SIZE$${postfix}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshIcon(tabId, force = false) {
|
|
|
|
const oldIcon = tabManager.get(tabId, 'icon');
|
2020-02-24 23:16:45 +00:00
|
|
|
const newIcon = getIconName(tabManager.get(tabId, 'styleIds', 0));
|
|
|
|
// (changing the icon only for the main page, frameId = 0)
|
2020-02-23 15:43:26 +00:00
|
|
|
|
|
|
|
if (!force && oldIcon === newIcon) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tabManager.set(tabId, 'icon', newIcon);
|
|
|
|
iconUtil.setIcon({
|
|
|
|
path: getIconPath(newIcon),
|
2020-11-18 11:17:15 +00:00
|
|
|
tabId,
|
2020-02-23 15:43:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIconPath(icon) {
|
|
|
|
return ICON_SIZES.reduce(
|
|
|
|
(obj, size) => {
|
|
|
|
obj[size] = `/images/icon/${icon.replace('$SIZE$', size)}.png`;
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
/** @return {number | ''} */
|
|
|
|
function getStyleCount(tabId) {
|
|
|
|
const allIds = new Set();
|
|
|
|
const data = tabManager.get(tabId, 'styleIds') || {};
|
|
|
|
Object.values(data).forEach(frameIds => frameIds.forEach(id => allIds.add(id)));
|
|
|
|
return allIds.size || '';
|
|
|
|
}
|
|
|
|
|
2020-02-23 15:43:26 +00:00
|
|
|
function refreshGlobalIcon() {
|
|
|
|
iconUtil.setIcon({
|
2020-11-18 11:17:15 +00:00
|
|
|
path: getIconPath(getIconName()),
|
2020-02-23 15:43:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshIconBadgeColor() {
|
|
|
|
const color = prefs.get(prefs.get('disableAll') ? 'badgeDisabled' : 'badgeNormal');
|
|
|
|
iconUtil.setBadgeBackgroundColor({
|
2020-11-18 11:17:15 +00:00
|
|
|
color,
|
2020-02-23 15:43:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshAllIcons() {
|
|
|
|
for (const tabId of tabManager.list()) {
|
|
|
|
refreshIcon(tabId);
|
|
|
|
}
|
|
|
|
refreshGlobalIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshAllIconsBadgeText() {
|
|
|
|
for (const tabId of tabManager.list()) {
|
|
|
|
refreshIconBadgeText(tabId);
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 23:16:45 +00:00
|
|
|
|
|
|
|
function refreshStaleBadges() {
|
|
|
|
for (const tabId of staleBadges) {
|
|
|
|
refreshIconBadgeText(tabId);
|
|
|
|
}
|
|
|
|
staleBadges.clear();
|
|
|
|
}
|
2020-02-23 15:43:26 +00:00
|
|
|
})();
|