2021-01-01 14:27:58 +00:00
|
|
|
/* global API msg */// msg.js
|
|
|
|
/* global StyleInjector */
|
|
|
|
/* global prefs */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2017-03-15 13:11:33 +00:00
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
(() => {
|
|
|
|
if (window.INJECTED === 1) return;
|
2020-02-12 14:39:00 +00:00
|
|
|
|
2021-02-21 21:32:22 +00:00
|
|
|
/** true -> when the page styles are received,
|
|
|
|
* false -> when disableAll mode is on at start, the styles won't be sent
|
|
|
|
* so while disableAll lasts we can ignore messages about style updates because
|
|
|
|
* the tab will explicitly ask for all styles in bulk when disableAll mode ends */
|
2021-01-01 14:27:58 +00:00
|
|
|
let hasStyles = false;
|
2021-02-21 21:32:22 +00:00
|
|
|
let isDisabled = false;
|
2021-01-01 14:27:58 +00:00
|
|
|
let isTab = !chrome.tabs || location.pathname !== '/popup.html';
|
2022-01-28 23:54:56 +00:00
|
|
|
const order = {main: [], prio: []};
|
|
|
|
const calcOrder = ({id}) =>
|
|
|
|
(order.prio[id] || 0) * 1e6 ||
|
|
|
|
order.main[id] ||
|
|
|
|
id + .5e6; // no order = at the end of `main`
|
2021-01-01 14:27:58 +00:00
|
|
|
const isFrame = window !== parent;
|
|
|
|
const isFrameAboutBlank = isFrame && location.href === 'about:blank';
|
|
|
|
const isUnstylable = !chrome.app && document instanceof XMLDocument;
|
|
|
|
const styleInjector = StyleInjector({
|
2022-01-28 23:54:56 +00:00
|
|
|
compare: (a, b) => calcOrder(a) - calcOrder(b),
|
2020-02-12 14:39:00 +00:00
|
|
|
onUpdate: onInjectorUpdate,
|
2019-03-10 02:58:17 +00:00
|
|
|
});
|
2021-02-28 19:32:25 +00:00
|
|
|
// dynamic iframes don't have a URL yet so we'll use their parent's URL (hash isn't inherited)
|
|
|
|
let matchUrl = isFrameAboutBlank && tryCatch(() => parent.location.href.split('#')[0]) ||
|
|
|
|
location.href;
|
2021-01-01 14:27:58 +00:00
|
|
|
|
|
|
|
// save it now because chrome.runtime will be unavailable in the orphaned script
|
|
|
|
const orphanEventId = chrome.runtime.id;
|
|
|
|
let isOrphaned;
|
|
|
|
// firefox doesn't orphanize content scripts so the old elements stay
|
|
|
|
if (!chrome.app) styleInjector.clearOrphans();
|
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
/** @type chrome.runtime.Port */
|
|
|
|
let port;
|
2021-01-01 14:27:58 +00:00
|
|
|
let lazyBadge = isFrame;
|
2020-10-22 19:16:55 +00:00
|
|
|
let parentDomain;
|
2020-02-24 23:16:45 +00:00
|
|
|
|
2021-02-28 19:32:25 +00:00
|
|
|
/* about:blank iframes are often used by sites for file upload or background tasks
|
|
|
|
* and they may break if unexpected DOM stuff is present at `load` event
|
|
|
|
* so we'll add the styles only if the iframe becomes visible */
|
2021-09-24 06:43:10 +00:00
|
|
|
const xoEventId = `${Math.random()}`;
|
2021-02-28 19:32:25 +00:00
|
|
|
/** @type IntersectionObserver */
|
|
|
|
let xo;
|
2022-01-29 15:19:21 +00:00
|
|
|
window[Symbol.for('xo')] = (el, cb) => {
|
|
|
|
if (!xo) xo = new IntersectionObserver(onIntersect, {rootMargin: '100%'});
|
|
|
|
el.addEventListener(xoEventId, cb, {once: true});
|
|
|
|
xo.observe(el);
|
|
|
|
};
|
2021-02-28 19:32:25 +00:00
|
|
|
|
2022-02-17 00:10:59 +00:00
|
|
|
// FIXME: move this to background page when following bugs are fixed:
|
|
|
|
// https://bugzil.la/1587723, https://crbug.com/968651
|
|
|
|
const mqDark = matchMedia('(prefers-color-scheme: dark)');
|
|
|
|
mqDark.onchange = e => API.colorScheme.updateSystemPreferDark(e.matches);
|
|
|
|
mqDark.onchange(mqDark);
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
// Declare all vars before init() or it'll throw due to "temporal dead zone" of const/let
|
|
|
|
const ready = init();
|
|
|
|
|
2020-02-24 23:16:45 +00:00
|
|
|
// the popup needs a check as it's not a tab but can be opened in a tab manually for whatever reason
|
2021-01-01 14:27:58 +00:00
|
|
|
if (!isTab) {
|
2020-02-24 23:16:45 +00:00
|
|
|
chrome.tabs.getCurrent(tab => {
|
2021-01-01 14:27:58 +00:00
|
|
|
isTab = Boolean(tab);
|
2020-02-24 23:16:45 +00:00
|
|
|
if (tab && styleInjector.list.length) updateCount();
|
|
|
|
});
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
msg.onTab(applyOnMessage);
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2020-02-12 14:39:00 +00:00
|
|
|
if (!chrome.tabs) {
|
|
|
|
window.dispatchEvent(new CustomEvent(orphanEventId));
|
|
|
|
window.addEventListener(orphanEventId, orphanCheck, true);
|
2017-04-12 12:31:05 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2019-03-10 02:58:17 +00:00
|
|
|
function onInjectorUpdate() {
|
2020-02-12 14:39:00 +00:00
|
|
|
if (!isOrphaned) {
|
|
|
|
updateCount();
|
2020-10-22 19:16:55 +00:00
|
|
|
const onOff = prefs[styleInjector.list.length ? 'subscribe' : 'unsubscribe'];
|
2021-01-01 14:27:58 +00:00
|
|
|
onOff('disableAll', updateDisableAll);
|
|
|
|
if (isFrame) {
|
2020-10-22 19:16:55 +00:00
|
|
|
updateExposeIframes();
|
2021-01-01 14:27:58 +00:00
|
|
|
onOff('exposeIframes', updateExposeIframes);
|
2020-10-22 19:16:55 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:16:55 +00:00
|
|
|
async function init() {
|
2021-01-01 14:27:58 +00:00
|
|
|
if (isUnstylable) {
|
2020-10-22 19:16:55 +00:00
|
|
|
await API.styleViaAPI({method: 'styleApply'});
|
|
|
|
} else {
|
2021-01-01 14:27:58 +00:00
|
|
|
const SYM_ID = 'styles';
|
|
|
|
const SYM = Symbol.for(SYM_ID);
|
2021-02-05 07:35:02 +00:00
|
|
|
const parentStyles = isFrameAboutBlank &&
|
|
|
|
tryCatch(() => parent[parent.Symbol.for(SYM_ID)]);
|
2021-01-01 14:27:58 +00:00
|
|
|
const styles =
|
|
|
|
window[SYM] ||
|
2021-02-28 19:32:25 +00:00
|
|
|
parentStyles && await new Promise(onFrameElementInView) && parentStyles ||
|
2021-02-05 07:51:39 +00:00
|
|
|
!isFrameAboutBlank && chrome.app && !chrome.tabs && tryCatch(getStylesViaXhr) ||
|
2021-01-01 14:27:58 +00:00
|
|
|
await API.styles.getSectionsByUrl(matchUrl, null, true);
|
2022-01-14 12:44:48 +00:00
|
|
|
if (styles.cfg) {
|
|
|
|
isDisabled = styles.cfg.disableAll;
|
2022-01-28 23:54:56 +00:00
|
|
|
Object.assign(order, styles.cfg.order);
|
2022-01-14 12:44:48 +00:00
|
|
|
}
|
2021-02-21 21:32:22 +00:00
|
|
|
hasStyles = !isDisabled;
|
2021-01-01 14:27:58 +00:00
|
|
|
if (hasStyles) {
|
|
|
|
window[SYM] = styles;
|
|
|
|
await styleInjector.apply(styles);
|
|
|
|
} else {
|
|
|
|
delete window[SYM];
|
|
|
|
prefs.subscribe('disableAll', updateDisableAll);
|
2020-10-22 19:58:11 +00:00
|
|
|
}
|
2021-02-21 21:29:25 +00:00
|
|
|
styleInjector.toggle(hasStyles);
|
2020-10-22 19:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/** Must be executed inside try/catch */
|
2020-11-18 11:17:15 +00:00
|
|
|
function getStylesViaXhr() {
|
2021-01-01 14:27:58 +00:00
|
|
|
const blobId = document.cookie.split(chrome.runtime.id + '=')[1].split(';')[0];
|
|
|
|
const url = 'blob:' + chrome.runtime.getURL(blobId);
|
|
|
|
document.cookie = `${chrome.runtime.id}=1; max-age=0`; // remove our cookie
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', url, false); // synchronous
|
|
|
|
xhr.send();
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
return JSON.parse(xhr.response);
|
2018-08-09 17:33:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function applyOnMessage(request) {
|
2021-01-01 14:27:58 +00:00
|
|
|
const {method} = request;
|
|
|
|
if (isUnstylable) {
|
|
|
|
if (method === 'urlChanged') {
|
2018-11-07 06:09:29 +00:00
|
|
|
request.method = 'styleReplaceAll';
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
if (/^(style|updateCount)/.test(method)) {
|
2020-02-02 04:36:54 +00:00
|
|
|
API.styleViaAPI(request);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-13 12:28:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
const {style} = request;
|
|
|
|
switch (method) {
|
2020-02-02 04:36:54 +00:00
|
|
|
case 'ping':
|
|
|
|
return true;
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
case 'styleDeleted':
|
2021-01-01 14:27:58 +00:00
|
|
|
styleInjector.remove(style.id);
|
2017-11-25 21:56:57 +00:00
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
case 'styleUpdated':
|
2021-02-21 21:32:22 +00:00
|
|
|
if (!hasStyles && isDisabled) break;
|
2021-01-01 14:27:58 +00:00
|
|
|
if (style.enabled) {
|
|
|
|
API.styles.getSectionsByUrl(matchUrl, style.id).then(sections =>
|
|
|
|
sections[style.id]
|
|
|
|
? styleInjector.apply(sections)
|
|
|
|
: styleInjector.remove(style.id));
|
2017-11-25 21:56:57 +00:00
|
|
|
} else {
|
2021-01-01 14:27:58 +00:00
|
|
|
styleInjector.remove(style.id);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
case 'styleAdded':
|
2021-02-21 21:32:22 +00:00
|
|
|
if (!hasStyles && isDisabled) break;
|
2021-01-01 14:27:58 +00:00
|
|
|
if (style.enabled) {
|
|
|
|
API.styles.getSectionsByUrl(matchUrl, style.id)
|
2020-02-12 14:39:00 +00:00
|
|
|
.then(styleInjector.apply);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2022-01-14 12:44:48 +00:00
|
|
|
case 'styleSort':
|
2022-01-28 23:54:56 +00:00
|
|
|
Object.assign(order, request.order);
|
2022-01-14 12:44:48 +00:00
|
|
|
styleInjector.sort();
|
|
|
|
break;
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
case 'urlChanged':
|
2021-02-28 19:32:25 +00:00
|
|
|
if (!hasStyles && isDisabled || matchUrl === request.url) break;
|
|
|
|
matchUrl = request.url;
|
2021-01-01 14:27:58 +00:00
|
|
|
API.styles.getSectionsByUrl(matchUrl).then(sections => {
|
|
|
|
hasStyles = true;
|
|
|
|
styleInjector.replace(sections);
|
|
|
|
});
|
2017-11-25 21:56:57 +00:00
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
case 'backgroundReady':
|
2021-01-01 14:27:58 +00:00
|
|
|
ready.catch(err =>
|
2020-10-26 14:39:07 +00:00
|
|
|
msg.isIgnorableError(err)
|
|
|
|
? init()
|
|
|
|
: console.error(err));
|
2017-11-25 21:56:57 +00:00
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
case 'updateCount':
|
|
|
|
updateCount();
|
2017-11-25 21:56:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
|
2020-10-22 19:16:55 +00:00
|
|
|
function updateDisableAll(key, disableAll) {
|
2021-02-21 21:32:22 +00:00
|
|
|
isDisabled = disableAll;
|
2021-01-01 14:27:58 +00:00
|
|
|
if (isUnstylable) {
|
2018-11-07 06:09:29 +00:00
|
|
|
API.styleViaAPI({method: 'prefChanged', prefs: {disableAll}});
|
2021-01-01 14:27:58 +00:00
|
|
|
} else if (!hasStyles && !disableAll) {
|
|
|
|
init();
|
2018-11-07 06:09:29 +00:00
|
|
|
} else {
|
2019-03-10 02:58:17 +00:00
|
|
|
styleInjector.toggle(!disableAll);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2017-04-28 23:36:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 19:16:55 +00:00
|
|
|
async function updateExposeIframes(key, value = prefs.get('exposeIframes')) {
|
|
|
|
const attr = 'stylus-iframe';
|
|
|
|
const el = document.documentElement;
|
|
|
|
if (!el) return; // got no styles so styleInjector didn't wait for <html>
|
|
|
|
if (!value || !styleInjector.list.length) {
|
|
|
|
el.removeAttribute(attr);
|
2018-11-07 06:09:29 +00:00
|
|
|
} else {
|
2020-10-22 19:16:55 +00:00
|
|
|
if (!parentDomain) parentDomain = await API.getTabUrlPrefix();
|
|
|
|
// Check first to avoid triggering DOM mutation
|
|
|
|
if (el.getAttribute(attr) !== parentDomain) {
|
|
|
|
el.setAttribute(attr, parentDomain);
|
|
|
|
}
|
2017-04-12 06:15:57 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function updateCount() {
|
2021-01-01 14:27:58 +00:00
|
|
|
if (!isTab) return;
|
|
|
|
if (isFrame) {
|
2020-02-24 23:16:45 +00:00
|
|
|
if (!port && styleInjector.list.length) {
|
|
|
|
port = chrome.runtime.connect({name: 'iframe'});
|
|
|
|
} else if (port && !styleInjector.list.length) {
|
|
|
|
port.disconnect();
|
|
|
|
}
|
|
|
|
if (lazyBadge && performance.now() > 1000) lazyBadge = false;
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
(isUnstylable ?
|
2020-02-24 23:16:45 +00:00
|
|
|
API.styleViaAPI({method: 'updateCount'}) :
|
|
|
|
API.updateIconBadge(styleInjector.list.map(style => style.id), {lazyBadge})
|
|
|
|
).catch(msg.ignoreError);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2021-02-28 19:32:25 +00:00
|
|
|
function onFrameElementInView(cb) {
|
2022-01-29 15:19:21 +00:00
|
|
|
parent[parent.Symbol.for('xo')](frameElement, cb);
|
2021-02-28 19:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {IntersectionObserverEntry[]} entries */
|
|
|
|
function onIntersect(entries) {
|
|
|
|
for (const e of entries) {
|
|
|
|
if (e.isIntersecting) {
|
|
|
|
xo.unobserve(e.target);
|
2021-09-24 06:43:10 +00:00
|
|
|
e.target.dispatchEvent(new Event(xoEventId));
|
2021-02-28 19:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function tryCatch(func, ...args) {
|
2020-02-12 14:39:00 +00:00
|
|
|
try {
|
2021-01-01 14:27:58 +00:00
|
|
|
return func(...args);
|
2020-02-12 14:39:00 +00:00
|
|
|
} catch (e) {}
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function orphanCheck() {
|
|
|
|
if (tryCatch(() => chrome.i18n.getUILanguage())) return;
|
2017-11-25 21:56:57 +00:00
|
|
|
// In Chrome content script is orphaned on an extension update/reload
|
|
|
|
// so we need to detach event listeners
|
2020-02-12 14:39:00 +00:00
|
|
|
window.removeEventListener(orphanEventId, orphanCheck, true);
|
2022-02-17 00:10:59 +00:00
|
|
|
mqDark.onchange = null;
|
2020-02-12 14:39:00 +00:00
|
|
|
isOrphaned = true;
|
2021-03-19 16:00:37 +00:00
|
|
|
setTimeout(styleInjector.clear, 1000); // avoiding FOUC
|
2021-01-01 14:27:58 +00:00
|
|
|
tryCatch(msg.off, applyOnMessage);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
})();
|