stylus/background/style-via-api.js

221 lines
5.9 KiB
JavaScript
Raw Normal View History

2017-11-13 12:28:50 +00:00
/* global getStyles */
'use strict';
// eslint-disable-next-line no-var
2017-12-09 01:27:09 +00:00
var styleViaAPI = !CHROME &&
(() => {
2017-11-13 12:28:50 +00:00
const ACTIONS = {
styleApply,
styleDeleted,
styleUpdated,
styleAdded,
2017-12-09 06:14:18 +00:00
styleReplaceAll: styleApply,
2017-11-13 12:28:50 +00:00
prefChanged,
ping,
2017-11-13 12:28:50 +00:00
};
const NOP = Promise.resolve(new Error('NOP'));
const PONG = Promise.resolve(true);
2017-12-09 01:27:09 +00:00
const onError = () => NOP;
2017-11-13 12:28:50 +00:00
const cache = new Map();
const allFrameUrls = new Map();
2017-11-13 12:28:50 +00:00
2017-12-09 06:14:18 +00:00
chrome.tabs.onRemoved.addListener(onTabRemoved);
chrome.tabs.onReplaced.addListener(onTabReplaced);
2017-11-13 12:28:50 +00:00
return {
process,
getFrameUrl,
setFrameUrl,
allFrameUrls,
cache,
2017-11-13 12:28:50 +00:00
};
2017-12-09 06:14:18 +00:00
//region public methods
2017-12-09 01:27:09 +00:00
function process(request, sender) {
2017-12-09 06:14:18 +00:00
console.log(request.action || request.method, request.prefs || request.styles || request.style, sender.tab, sender.frameId);
const action = ACTIONS[request.action || request.method];
2017-12-09 06:14:18 +00:00
if (!action) {
return NOP;
}
const {tab} = sender;
if (!isNaN(sender.frameId)) {
const result = action(request, sender);
return result ? result.catch(onError) : NOP;
}
return browser.webNavigation.getAllFrames({tabId: tab.id}).then(frames =>
Promise.all((frames || []).map(({frameId}) =>
(action(request, {tab, frameId}) || NOP).catch(onError)))
).catch(onError);
2017-12-09 01:27:09 +00:00
}
function getFrameUrl(tabId, frameId = 0) {
const frameUrls = allFrameUrls.get(tabId);
return frameUrls && frameUrls[frameId] || '';
}
function setFrameUrl(tabId, frameId, url) {
const frameUrls = allFrameUrls.get(tabId);
if (frameUrls) {
frameUrls[frameId] = url;
} else {
allFrameUrls.set(tabId, {[frameId]: url});
}
}
2017-12-09 06:14:18 +00:00
//endregion
//region actions
2017-12-09 01:27:09 +00:00
2017-12-09 06:14:18 +00:00
function styleApply({styles, disableAll}, sender) {
if (disableAll) {
2017-12-09 01:27:09 +00:00
return;
2017-11-13 12:28:50 +00:00
}
2017-12-09 06:14:18 +00:00
const {tab: {id: tabId}, frameId, url} = sender;
if (!styles || styles === 'DIY') {
return requestStyles({matchUrl: url || getFrameUrl(tabId, frameId)}, sender);
}
const {tabFrames, frameStyles} = getCachedData(tabId, frameId);
const newSorted = getSortedById(styles);
if (!sameArrays(frameStyles, newSorted, sameArrays)) {
tabFrames[frameId] = newSorted;
cache.set(tabId, tabFrames);
return replaceCSS(tabId, frameId, frameStyles, newSorted);
2017-11-13 12:28:50 +00:00
}
}
function styleDeleted({id}, {tab, frameId}) {
2017-12-09 06:14:18 +00:00
const {frameStyles} = getCachedData(tab.id, frameId);
const index = frameStyles.findIndex(item => item.id === id);
if (index >= 0) {
const oldStyles = frameStyles.slice();
frameStyles.splice(index, 1);
return replaceCSS(tab.id, frameId, oldStyles, frameStyles);
2017-11-13 12:28:50 +00:00
}
}
function styleUpdated({style}, sender) {
2017-12-09 01:27:09 +00:00
return (style.enabled ? styleApply : styleDeleted)(style, sender);
2017-11-13 12:28:50 +00:00
}
2017-12-09 06:14:18 +00:00
function styleAdded({style: {enabled}}, sender) {
return enabled && styleApply({}, sender);
2017-11-13 12:28:50 +00:00
}
function prefChanged({prefs}, sender) {
if ('disableAll' in prefs) {
2017-12-09 01:27:09 +00:00
disableAll(prefs.disableAll, sender);
2017-11-13 12:28:50 +00:00
}
}
function ping() {
return PONG;
}
2017-12-09 06:14:18 +00:00
//endregion
//region action helpers
2017-12-09 01:27:09 +00:00
function disableAll(state, sender) {
if (state) {
const {tab, frameId} = sender;
const {tabFrames, frameStyles} = getCachedData(tab.id, frameId);
delete tabFrames[frameId];
return removeCSS(tab.id, frameId, frameStyles);
} else {
2017-12-09 06:14:18 +00:00
return styleApply({}, sender);
2017-11-13 12:28:50 +00:00
}
}
2017-12-09 06:14:18 +00:00
//endregion
//region observer
2017-11-13 12:28:50 +00:00
function onTabRemoved(tabId) {
cache.delete(tabId);
}
function onTabReplaced(addedTabId, removedTabId) {
2017-12-09 06:14:18 +00:00
cache.delete(removedTabId);
2017-11-13 12:28:50 +00:00
}
2017-12-09 06:14:18 +00:00
//endregion
//region browser API
2017-12-09 01:27:09 +00:00
function replaceCSS(tabId, frameId, oldStyles, newStyles) {
2017-12-09 06:14:18 +00:00
console.log.apply(null, arguments);
2017-12-09 01:27:09 +00:00
return insertCSS(tabId, frameId, newStyles).then(() =>
removeCSS(tabId, frameId, oldStyles));
2017-11-13 12:28:50 +00:00
}
2017-12-09 01:27:09 +00:00
function insertCSS(tabId, frameId, frameStyles) {
const code = getFrameCode(frameStyles);
2017-12-09 06:14:18 +00:00
return !code ? NOP :
browser.tabs.insertCSS(tabId, {
code,
frameId,
runAt: 'document_start',
matchAboutBlank: true,
}).catch(onError);
}
2017-12-09 01:27:09 +00:00
function removeCSS(tabId, frameId, frameStyles) {
const code = getFrameCode(frameStyles);
2017-12-09 06:14:18 +00:00
return !code ? NOP :
browser.tabs.removeCSS(tabId, {
code,
frameId,
matchAboutBlank: true
}).catch(onError);
2017-12-09 01:27:09 +00:00
}
2017-12-09 06:14:18 +00:00
//endregion
//region utilities
2017-12-09 06:14:18 +00:00
function requestStyles(options, sender) {
options.matchUrl = options.matchUrl || sender.url;
options.enabled = true;
options.asHash = true;
return getStyles(options).then(styles =>
styleApply({styles}, sender));
2017-11-13 12:28:50 +00:00
}
2017-12-09 01:27:09 +00:00
function getSortedById(styleHash) {
const styles = [];
let needsSorting = false;
let prevKey = -1;
for (let k in styleHash) {
k = parseInt(k);
if (!isNaN(k)) {
2017-12-09 06:14:18 +00:00
const sections = styleHash[k].map(({code}) => code);
2017-12-09 01:27:09 +00:00
styles.push(sections);
2017-12-09 06:14:18 +00:00
defineProperty(sections, 'id', k);
2017-12-09 01:27:09 +00:00
needsSorting |= k < prevKey;
prevKey = k;
2017-11-13 12:28:50 +00:00
}
}
2017-12-09 01:27:09 +00:00
return needsSorting ? styles.sort((a, b) => a.id - b.id) : styles;
}
function getCachedData(tabId, frameId, styleId) {
const tabFrames = cache.get(tabId) || {};
2017-12-09 06:14:18 +00:00
const frameStyles = tabFrames[frameId] || [];
const styleSections = styleId && frameStyles.find(s => s.id === styleId) || [];
2017-12-09 01:27:09 +00:00
return {tabFrames, frameStyles, styleSections};
}
function getFrameCode(frameStyles) {
2017-12-09 06:14:18 +00:00
// we cache a shallow copy of code from the sections array in order to reuse references
// in other places whereas the combined string gets garbage-collected
return typeof frameStyles === 'string' ? frameStyles : [].concat(...frameStyles).join('\n');
2017-11-13 12:28:50 +00:00
}
2017-12-09 01:27:09 +00:00
function defineProperty(obj, name, value) {
return Object.defineProperty(obj, name, {value, configurable: true});
2017-11-13 12:28:50 +00:00
}
2017-12-09 06:14:18 +00:00
function sameArrays(a, b, fn) {
return a.length === b.length && a.every((el, i) => fn ? fn(el, b[i]) : el === b[i]);
2017-11-13 12:28:50 +00:00
}
2017-12-09 06:14:18 +00:00
//endregion
2017-11-13 12:28:50 +00:00
})();