2021-01-01 14:27:58 +00:00
|
|
|
/* global API */// msg.js
|
|
|
|
/* global addAPI bgReady */// common.js
|
|
|
|
/* global prefs */
|
|
|
|
/* global tabMan */
|
|
|
|
/* global CHROME FIREFOX VIVALDI debounce ignoreChromeError */// toolbox.js
|
2020-02-23 15:43:26 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-02-09 06:58:30 +00:00
|
|
|
/* exported iconMan */
|
|
|
|
const iconMan = (() => {
|
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();
|
2021-01-01 14:27:58 +00:00
|
|
|
const imageDataCache = new Map();
|
2021-02-09 06:58:30 +00:00
|
|
|
const badgeOvr = {color: '', text: ''};
|
2021-01-01 14:27:58 +00:00
|
|
|
// https://github.com/openstyles/stylus/issues/335
|
|
|
|
let hasCanvas = loadImage(`/images/icon/${ICON_SIZES[0]}.png`)
|
|
|
|
.then(({data}) => (hasCanvas = data.some(b => b !== 255)));
|
|
|
|
|
|
|
|
addAPI(/** @namespace API */ {
|
|
|
|
/**
|
|
|
|
* @param {(number|string)[]} styleIds
|
|
|
|
* @param {boolean} [lazyBadge=false] preventing flicker during page load
|
|
|
|
*/
|
2020-02-24 23:16:45 +00:00
|
|
|
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;
|
2021-01-01 14:27:58 +00:00
|
|
|
tabMan.set(tabId, 'styleIds', frameId, value);
|
2020-02-24 23:16:45 +00:00
|
|
|
debounce(refreshStaleBadges, frameId && lazyBadge ? 250 : 0);
|
|
|
|
staleBadges.add(tabId);
|
|
|
|
if (!frameId) refreshIcon(tabId, true);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
chrome.webNavigation.onCommitted.addListener(({tabId, frameId}) => {
|
|
|
|
if (!frameId) tabMan.set(tabId, 'styleIds', undefined);
|
2020-02-24 23:16:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
chrome.runtime.onConnect.addListener(port => {
|
|
|
|
if (port.name === 'iframe') {
|
|
|
|
port.onDisconnect.addListener(onPortDisconnected);
|
|
|
|
}
|
|
|
|
});
|
2020-02-23 15:43:26 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
bgReady.all.then(() => {
|
|
|
|
prefs.subscribe([
|
|
|
|
'disableAll',
|
|
|
|
'badgeDisabled',
|
|
|
|
'badgeNormal',
|
|
|
|
], () => debounce(refreshIconBadgeColor), {runNow: true});
|
|
|
|
prefs.subscribe([
|
|
|
|
'show-badge',
|
|
|
|
], () => debounce(refreshAllIconsBadgeText), {runNow: true});
|
|
|
|
prefs.subscribe([
|
|
|
|
'disableAll',
|
|
|
|
'iconset',
|
|
|
|
], () => debounce(refreshAllIcons), {runNow: true});
|
|
|
|
});
|
|
|
|
|
2021-02-09 06:58:30 +00:00
|
|
|
return {
|
|
|
|
/** Calling with no params clears the override */
|
|
|
|
overrideBadge({text = '', color = '', title = ''} = {}) {
|
|
|
|
if (badgeOvr.text === text) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
badgeOvr.text = text;
|
|
|
|
badgeOvr.color = color;
|
|
|
|
refreshIconBadgeColor();
|
|
|
|
setBadgeText({text});
|
|
|
|
for (const tabId of tabMan.list()) {
|
2021-02-09 09:37:36 +00:00
|
|
|
if (text) {
|
2021-02-09 06:58:30 +00:00
|
|
|
setBadgeText({tabId, text});
|
|
|
|
} else {
|
|
|
|
refreshIconBadgeText(tabId);
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 09:37:36 +00:00
|
|
|
chrome.browserAction.setTitle({
|
|
|
|
title: title && chrome.i18n.getMessage(title) || title || '',
|
|
|
|
});
|
2021-02-09 06:58:30 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
function onPortDisconnected({sender}) {
|
2021-01-01 14:27:58 +00:00
|
|
|
if (tabMan.get(sender.tab.id, 'styleIds')) {
|
|
|
|
API.updateIconBadge.call({sender}, [], {lazyBadge: true});
|
2020-02-24 23:16:45 +00:00
|
|
|
}
|
2020-02-23 15:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function refreshIconBadgeText(tabId) {
|
2021-02-09 06:58:30 +00:00
|
|
|
if (badgeOvr.text) return;
|
2020-02-24 23:16:45 +00:00
|
|
|
const text = prefs.get('show-badge') ? `${getStyleCount(tabId)}` : '';
|
2021-01-01 14:27:58 +00:00
|
|
|
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) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const oldIcon = tabMan.get(tabId, 'icon');
|
|
|
|
const newIcon = getIconName(tabMan.get(tabId, 'styleIds', 0));
|
2020-02-24 23:16:45 +00:00
|
|
|
// (changing the icon only for the main page, frameId = 0)
|
2020-02-23 15:43:26 +00:00
|
|
|
|
|
|
|
if (!force && oldIcon === newIcon) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
tabMan.set(tabId, 'icon', newIcon);
|
|
|
|
setIcon({
|
2020-02-23 15:43:26 +00:00
|
|
|
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();
|
2021-01-01 14:27:58 +00:00
|
|
|
const data = tabMan.get(tabId, 'styleIds') || {};
|
2020-02-24 23:16:45 +00:00
|
|
|
Object.values(data).forEach(frameIds => frameIds.forEach(id => allIds.add(id)));
|
|
|
|
return allIds.size || '';
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
// Caches imageData for icon paths
|
|
|
|
async function loadImage(url) {
|
2021-05-31 14:21:17 +00:00
|
|
|
const {OffscreenCanvas} = !FIREFOX && self.createImageBitmap && self || {};
|
2021-01-01 14:27:58 +00:00
|
|
|
const img = OffscreenCanvas
|
|
|
|
? await createImageBitmap(await (await fetch(url)).blob())
|
|
|
|
: await new Promise((resolve, reject) =>
|
|
|
|
Object.assign(new Image(), {
|
|
|
|
src: url,
|
|
|
|
onload: e => resolve(e.target),
|
|
|
|
onerror: reject,
|
|
|
|
}));
|
|
|
|
const {width: w, height: h} = img;
|
|
|
|
const canvas = OffscreenCanvas
|
|
|
|
? new OffscreenCanvas(w, h)
|
|
|
|
: Object.assign(document.createElement('canvas'), {width: w, height: h});
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
ctx.drawImage(img, 0, 0, w, h);
|
|
|
|
const result = ctx.getImageData(0, 0, w, h);
|
|
|
|
imageDataCache.set(url, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-02-23 15:43:26 +00:00
|
|
|
function refreshGlobalIcon() {
|
2021-01-01 14:27:58 +00:00
|
|
|
setIcon({
|
2020-11-18 11:17:15 +00:00
|
|
|
path: getIconPath(getIconName()),
|
2020-02-23 15:43:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshIconBadgeColor() {
|
2021-01-01 14:27:58 +00:00
|
|
|
setBadgeBackgroundColor({
|
2021-02-09 06:58:30 +00:00
|
|
|
color: badgeOvr.color ||
|
|
|
|
prefs.get(prefs.get('disableAll') ? 'badgeDisabled' : 'badgeNormal'),
|
2020-02-23 15:43:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshAllIcons() {
|
2021-01-01 14:27:58 +00:00
|
|
|
for (const tabId of tabMan.list()) {
|
2020-02-23 15:43:26 +00:00
|
|
|
refreshIcon(tabId);
|
|
|
|
}
|
|
|
|
refreshGlobalIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshAllIconsBadgeText() {
|
2021-01-01 14:27:58 +00:00
|
|
|
for (const tabId of tabMan.list()) {
|
2020-02-23 15:43:26 +00:00
|
|
|
refreshIconBadgeText(tabId);
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 23:16:45 +00:00
|
|
|
|
|
|
|
function refreshStaleBadges() {
|
|
|
|
for (const tabId of staleBadges) {
|
|
|
|
refreshIconBadgeText(tabId);
|
|
|
|
}
|
|
|
|
staleBadges.clear();
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
|
|
|
|
function safeCall(method, data) {
|
|
|
|
const {browserAction = {}} = chrome;
|
|
|
|
const fn = browserAction[method];
|
|
|
|
if (fn) {
|
|
|
|
try {
|
|
|
|
// Chrome supports the callback since 67.0.3381.0, see https://crbug.com/451320
|
|
|
|
fn.call(browserAction, data, ignoreChromeError);
|
|
|
|
} catch (e) {
|
|
|
|
// FIXME: skip pre-rendered tabs?
|
|
|
|
fn.call(browserAction, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {chrome.browserAction.TabIconDetails} data */
|
|
|
|
async function setIcon(data) {
|
|
|
|
if (hasCanvas === true || await hasCanvas) {
|
|
|
|
data.imageData = {};
|
|
|
|
for (const [key, url] of Object.entries(data.path)) {
|
|
|
|
data.imageData[key] = imageDataCache.get(url) || await loadImage(url);
|
|
|
|
}
|
|
|
|
delete data.path;
|
|
|
|
}
|
|
|
|
safeCall('setIcon', data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {chrome.browserAction.BadgeTextDetails} data */
|
|
|
|
function setBadgeText(data) {
|
|
|
|
safeCall('setBadgeText', data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {chrome.browserAction.BadgeBackgroundColorDetails} data */
|
|
|
|
function setBadgeBackgroundColor(data) {
|
|
|
|
safeCall('setBadgeBackgroundColor', data);
|
|
|
|
}
|
2020-02-23 15:43:26 +00:00
|
|
|
})();
|