stylus/background/style-via-api.js

233 lines
6.6 KiB
JavaScript
Raw Normal View History

2018-10-13 16:03:34 +00:00
/* global API_METHODS styleManager CHROME prefs updateIconBadge */
2017-11-13 12:28:50 +00:00
'use strict';
2018-01-01 17:02:49 +00:00
API_METHODS.styleViaAPI = !CHROME && (() => {
2017-11-13 12:28:50 +00:00
const ACTIONS = {
styleApply,
styleDeleted,
styleUpdated,
styleAdded,
styleReplaceAll,
prefChanged,
2018-10-13 16:03:34 +00:00
updateCount,
2017-11-13 12:28:50 +00:00
};
const NOP = Promise.resolve(new Error('NOP'));
const onError = () => {};
/* <tabId>: Object
<frameId>: Object
url: String, non-enumerable
<styleId>: Array of strings
section code */
const cache = new Map();
let observingTabs = false;
2018-10-04 04:46:19 +00:00
return function (request) {
2018-10-13 13:29:06 +00:00
const action = ACTIONS[request.method];
2017-11-13 12:28:50 +00:00
return !action ? NOP :
2018-10-04 04:46:19 +00:00
action(request, this.sender)
2017-11-13 12:28:50 +00:00
.catch(onError)
.then(maybeToggleObserver);
};
2018-10-13 16:03:34 +00:00
function updateCount(request, {tab, frameId}) {
if (frameId) {
throw new Error('we do not count styles for frames');
}
const {frameStyles} = getCachedData(tab.id, frameId);
updateIconBadge(tab.id, Object.keys(frameStyles).length);
}
2018-10-13 13:29:06 +00:00
function styleApply({id = null, ignoreUrlCheck = false}, {tab, frameId, url}) {
2017-11-13 12:28:50 +00:00
if (prefs.get('disableAll')) {
return NOP;
}
const {tabFrames, frameStyles} = getCachedData(tab.id, frameId);
if (id === null && !ignoreUrlCheck && frameStyles.url === url) {
return NOP;
}
2018-10-13 08:39:54 +00:00
return styleManager.getSectionsByUrl(url, id).then(sections => {
2017-11-13 12:28:50 +00:00
const tasks = [];
2018-10-07 13:20:39 +00:00
for (const section of Object.values(sections)) {
2018-10-07 13:28:51 +00:00
const styleId = section.id;
2018-10-13 13:29:06 +00:00
const code = section.code.join('\n');
2017-11-13 12:28:50 +00:00
if (code === (frameStyles[styleId] || []).join('\n')) {
continue;
}
2018-10-13 13:29:06 +00:00
frameStyles[styleId] = section.code;
2017-11-13 12:28:50 +00:00
tasks.push(
browser.tabs.insertCSS(tab.id, {
code,
frameId,
runAt: 'document_start',
matchAboutBlank: true,
}).catch(onError));
}
if (!removeFrameIfEmpty(tab.id, frameId, tabFrames, frameStyles)) {
Object.defineProperty(frameStyles, 'url', {value: url, configurable: true});
tabFrames[frameId] = frameStyles;
cache.set(tab.id, tabFrames);
}
return Promise.all(tasks);
2018-10-13 16:39:17 +00:00
})
.then(() => updateCount(null, {tab, frameId}));
2017-11-13 12:28:50 +00:00
}
2018-10-09 16:41:07 +00:00
function styleDeleted({style: {id}}, {tab, frameId}) {
2017-11-13 12:28:50 +00:00
const {tabFrames, frameStyles, styleSections} = getCachedData(tab.id, frameId, id);
const code = styleSections.join('\n');
if (code && !duplicateCodeExists({frameStyles, id, code})) {
delete frameStyles[id];
removeFrameIfEmpty(tab.id, frameId, tabFrames, frameStyles);
2018-10-13 16:39:17 +00:00
return removeCSS(tab.id, frameId, code)
.then(() => updateCount(null, {tab, frameId}));
2017-11-13 12:28:50 +00:00
} else {
return NOP;
}
}
function styleUpdated({style}, sender) {
if (!style.enabled) {
2018-10-09 16:41:07 +00:00
return styleDeleted({style}, sender);
2017-11-13 12:28:50 +00:00
}
const {tab, frameId} = sender;
const {frameStyles, styleSections} = getCachedData(tab.id, frameId, style.id);
const code = styleSections.join('\n');
return styleApply(style, sender).then(code && (() => {
if (!duplicateCodeExists({frameStyles, code, id: null})) {
return removeCSS(tab.id, frameId, code);
}
}));
}
function styleAdded({style}, sender) {
return style.enabled ? styleApply(style, sender) : NOP;
}
function styleReplaceAll(request, sender) {
const {tab, frameId} = sender;
const oldStylesCode = getFrameStylesJoined(sender);
return styleApply({ignoreUrlCheck: true}, sender).then(() => {
const newStylesCode = getFrameStylesJoined(sender);
const tasks = oldStylesCode
.filter(code => !newStylesCode.includes(code))
.map(code => removeCSS(tab.id, frameId, code));
return Promise.all(tasks);
});
}
function prefChanged({prefs}, sender) {
if ('disableAll' in prefs) {
if (!prefs.disableAll) {
return styleApply({}, sender);
}
const {tab, frameId} = sender;
const {tabFrames, frameStyles} = getCachedData(tab.id, frameId);
if (isEmpty(frameStyles)) {
return NOP;
}
removeFrameIfEmpty(tab.id, frameId, tabFrames, {});
const tasks = Object.keys(frameStyles)
.map(id => removeCSS(tab.id, frameId, frameStyles[id].join('\n')));
return Promise.all(tasks);
} else {
return NOP;
2017-11-13 12:28:50 +00:00
}
}
/* utilities */
function maybeToggleObserver() {
let method;
if (!observingTabs && cache.size) {
method = 'addListener';
} else if (observingTabs && !cache.size) {
method = 'removeListener';
} else {
return;
}
observingTabs = !observingTabs;
chrome.webNavigation.onCommitted[method](onNavigationCommitted);
chrome.tabs.onRemoved[method](onTabRemoved);
chrome.tabs.onReplaced[method](onTabReplaced);
}
function onNavigationCommitted({tabId, frameId}) {
if (frameId === 0) {
onTabRemoved(tabId);
return;
}
const tabFrames = cache.get(tabId);
if (frameId in tabFrames) {
delete tabFrames[frameId];
if (isEmpty(tabFrames)) {
onTabRemoved(tabId);
}
}
}
function onTabRemoved(tabId) {
cache.delete(tabId);
maybeToggleObserver();
}
function onTabReplaced(addedTabId, removedTabId) {
onTabRemoved(removedTabId);
}
function removeFrameIfEmpty(tabId, frameId, tabFrames, frameStyles) {
if (isEmpty(frameStyles)) {
delete tabFrames[frameId];
if (isEmpty(tabFrames)) {
cache.delete(tabId);
}
return true;
}
}
function getCachedData(tabId, frameId, styleId) {
const tabFrames = cache.get(tabId) || {};
const frameStyles = tabFrames[frameId] || {};
const styleSections = styleId && frameStyles[styleId] || [];
return {tabFrames, frameStyles, styleSections};
}
function getFrameStylesJoined({
tab,
frameId,
frameStyles = getCachedData(tab.id, frameId).frameStyles,
}) {
return Object.keys(frameStyles).map(id => frameStyles[id].join('\n'));
}
function duplicateCodeExists({
tab,
frameId,
frameStyles = getCachedData(tab.id, frameId).frameStyles,
frameStylesCode = {},
id,
code = frameStylesCode[id] || frameStyles[id].join('\n'),
}) {
id = String(id);
for (const styleId in frameStyles) {
if (id !== styleId &&
code === (frameStylesCode[styleId] || frameStyles[styleId].join('\n'))) {
return true;
}
}
}
function removeCSS(tabId, frameId, code) {
return browser.tabs.removeCSS(tabId, {frameId, code, matchAboutBlank: true})
.catch(onError);
}
function isEmpty(obj) {
for (const k in obj) {
return false;
}
return true;
}
})();