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-01-01 14:27:58 +00:00
|
|
|
let hasStyles = false;
|
|
|
|
let isTab = !chrome.tabs || location.pathname !== '/popup.html';
|
|
|
|
const isFrame = window !== parent;
|
|
|
|
const isFrameAboutBlank = isFrame && location.href === 'about:blank';
|
|
|
|
const isUnstylable = !chrome.app && document instanceof XMLDocument;
|
|
|
|
const styleInjector = StyleInjector({
|
2019-03-10 02:58:17 +00:00
|
|
|
compare: (a, b) => a.id - b.id,
|
2020-02-12 14:39:00 +00:00
|
|
|
onUpdate: onInjectorUpdate,
|
2019-03-10 02:58:17 +00:00
|
|
|
});
|
2021-01-01 14:27:58 +00:00
|
|
|
// dynamic about: and javascript: iframes don't have a URL yet so we'll use their parent
|
|
|
|
const matchUrl = isFrameAboutBlank && tryCatch(() => parent.location.href) || location.href;
|
|
|
|
|
|
|
|
// 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-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-05 07:35:02 +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 in the next tick */
|
|
|
|
parentStyles && await new Promise(requestAnimationFrame) && parentStyles ||
|
|
|
|
chrome.app && !chrome.tabs && tryCatch(getStylesViaXhr) ||
|
2021-01-01 14:27:58 +00:00
|
|
|
await API.styles.getSectionsByUrl(matchUrl, null, true);
|
|
|
|
hasStyles = !styles.disableAll;
|
|
|
|
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
|
|
|
}
|
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-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-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
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
case 'urlChanged':
|
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-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-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);
|
|
|
|
isOrphaned = true;
|
2019-03-10 02:58:17 +00:00
|
|
|
styleInjector.clear();
|
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
|
|
|
})();
|