2017-03-26 02:30:59 +00:00
|
|
|
/* eslint no-var: 0 */
|
2018-10-10 16:54:38 +00:00
|
|
|
/* global msg API prefs */
|
2018-10-11 09:32:27 +00:00
|
|
|
/* exported APPLY */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2017-03-15 13:11:33 +00:00
|
|
|
|
2018-10-11 07:42:23 +00:00
|
|
|
// some weird bug in new Chrome: the content script gets injected multiple times
|
|
|
|
// define a constant so it throws when redefined
|
|
|
|
const APPLY = (() => {
|
2018-01-15 16:42:30 +00:00
|
|
|
const CHROME = chrome.app ? parseInt(navigator.userAgent.match(/Chrom\w+\/(?:\d+\.){2}(\d+)|$/)[1]) : NaN;
|
2018-10-13 13:29:06 +00:00
|
|
|
const STYLE_VIA_API = !chrome.app && document instanceof XMLDocument;
|
2017-11-25 21:56:57 +00:00
|
|
|
var ID_PREFIX = 'stylus-';
|
2018-10-13 09:50:28 +00:00
|
|
|
var ROOT;
|
2017-11-25 21:56:57 +00:00
|
|
|
var isOwnPage = location.protocol.endsWith('-extension:');
|
|
|
|
var disableAll = false;
|
|
|
|
var styleElements = new Map();
|
|
|
|
var disabledElements = new Map();
|
|
|
|
var docRewriteObserver;
|
|
|
|
var docRootObserver;
|
2018-10-12 07:03:00 +00:00
|
|
|
const setStyleContent = createSetStyleContent();
|
2018-10-11 18:02:14 +00:00
|
|
|
const initializing = init();
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2018-10-06 05:02:45 +00:00
|
|
|
msg.onTab(applyOnMessage);
|
2017-11-25 21:56:57 +00:00
|
|
|
|
|
|
|
if (!isOwnPage) {
|
2018-10-11 09:32:27 +00:00
|
|
|
window.dispatchEvent(new CustomEvent(chrome.runtime.id, {
|
2018-10-11 15:39:11 +00:00
|
|
|
detail: pageObject({method: 'orphan'})
|
2018-10-11 09:32:27 +00:00
|
|
|
}));
|
2017-11-25 21:56:57 +00:00
|
|
|
window.addEventListener(chrome.runtime.id, orphanCheck, true);
|
2017-04-12 12:31:05 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2018-10-10 17:39:10 +00:00
|
|
|
let parentDomain;
|
|
|
|
|
2018-10-04 10:05:41 +00:00
|
|
|
prefs.subscribe(['disableAll'], (key, value) => doDisableAll(value));
|
2018-10-10 17:39:10 +00:00
|
|
|
if (window !== parent) {
|
|
|
|
prefs.subscribe(['exposeIframes'], updateExposeIframes);
|
|
|
|
}
|
2018-10-04 10:05:41 +00:00
|
|
|
|
2018-10-11 18:02:14 +00:00
|
|
|
function init() {
|
|
|
|
// FIXME: getStylesFallback?
|
2018-10-13 13:29:06 +00:00
|
|
|
if (STYLE_VIA_API) {
|
|
|
|
return API.styleViaAPI({method: 'styleApply'});
|
2018-10-11 18:02:14 +00:00
|
|
|
}
|
2018-10-13 08:39:54 +00:00
|
|
|
return API.getSectionsByUrl(getMatchUrl())
|
2018-10-11 18:02:14 +00:00
|
|
|
.then(result => {
|
2018-10-13 09:50:28 +00:00
|
|
|
ROOT = document.documentElement;
|
2018-10-13 13:29:06 +00:00
|
|
|
applyStyles(result, () => {
|
|
|
|
// CSS transition bug workaround: since we insert styles asynchronously,
|
|
|
|
// the browsers, especially Firefox, may apply all transitions on page load
|
|
|
|
if ([...styleElements.values()].some(n => n.textContent.includes('transition'))) {
|
2018-10-11 18:02:14 +00:00
|
|
|
applyTransitionPatch();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:39:11 +00:00
|
|
|
function pageObject(target) {
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
|
|
|
|
const obj = new window.Object();
|
|
|
|
Object.assign(obj, target);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2018-10-11 09:32:27 +00:00
|
|
|
function createSetStyleContent() {
|
|
|
|
// FF59+ bug workaround
|
|
|
|
// See https://github.com/openstyles/stylus/issues/461
|
|
|
|
// Since it's easy to spoof the browser version in pre-Quantum FF we're checking
|
|
|
|
// for getPreventDefault which got removed in FF59 https://bugzil.la/691151
|
|
|
|
const EVENT_NAME = chrome.runtime.id;
|
2018-10-13 18:16:48 +00:00
|
|
|
const usePageScript = CHROME || isOwnPage || Event.prototype.getPreventDefault ?
|
|
|
|
Promise.resolve(false) : injectPageScript();
|
2018-10-11 09:32:27 +00:00
|
|
|
return (el, content) => {
|
2018-10-13 18:16:48 +00:00
|
|
|
usePageScript.then(ok => {
|
|
|
|
if (!ok) {
|
|
|
|
// FIXME: do we have to keep el.sheet.disabled?
|
|
|
|
el.textContent = content;
|
|
|
|
} else {
|
|
|
|
const detail = pageObject({
|
|
|
|
method: 'setStyleContent',
|
|
|
|
id: el.id,
|
|
|
|
content
|
|
|
|
});
|
|
|
|
window.dispatchEvent(new CustomEvent(EVENT_NAME, {detail}));
|
|
|
|
}
|
2018-10-11 15:39:11 +00:00
|
|
|
});
|
2018-10-11 09:32:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function injectPageScript() {
|
|
|
|
const scriptContent = EVENT_NAME => {
|
2018-10-13 18:16:48 +00:00
|
|
|
document.currentScript.remove();
|
2018-10-11 09:32:27 +00:00
|
|
|
window.addEventListener(EVENT_NAME, function handler(e) {
|
|
|
|
const {method, id, content} = e.detail;
|
|
|
|
if (method === 'setStyleContent') {
|
|
|
|
const el = document.getElementById(id);
|
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const disabled = el.sheet.disabled;
|
|
|
|
el.textContent = content;
|
|
|
|
el.sheet.disabled = disabled;
|
|
|
|
} else if (method === 'orphan') {
|
|
|
|
window.removeEventListener(EVENT_NAME, handler);
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
};
|
2018-10-13 18:16:48 +00:00
|
|
|
const code = `(${scriptContent})(${JSON.stringify(EVENT_NAME)})`;
|
|
|
|
const src = `data:application/javascript;base64,${btoa(code)}`;
|
|
|
|
const script = document.createElement('script');
|
|
|
|
const {resolve, promise} = deferred();
|
|
|
|
script.src = src;
|
|
|
|
script.onload = () => resolve(true);
|
|
|
|
script.onerror = () => resolve(false);
|
|
|
|
document.documentElement.appendChild(script);
|
|
|
|
return promise;
|
2018-10-11 09:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 18:16:48 +00:00
|
|
|
function deferred() {
|
|
|
|
const o = {};
|
|
|
|
o.promise = new Promise((resolve, reject) => {
|
|
|
|
o.resolve = resolve;
|
|
|
|
o.reject = reject;
|
|
|
|
});
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2018-10-05 10:47:52 +00:00
|
|
|
function getMatchUrl() {
|
2017-11-25 21:56:57 +00:00
|
|
|
var matchUrl = location.href;
|
|
|
|
if (!matchUrl.match(/^(http|file|chrome|ftp)/)) {
|
|
|
|
// dynamic about: and javascript: iframes don't have an URL yet
|
|
|
|
// so we'll try the parent frame which is guaranteed to have a real URL
|
|
|
|
try {
|
|
|
|
if (window !== parent) {
|
|
|
|
matchUrl = parent.location.href;
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
2018-10-05 10:47:52 +00:00
|
|
|
return matchUrl;
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:33:04 +00:00
|
|
|
/**
|
|
|
|
* TODO: remove when FF fixes the bug.
|
|
|
|
* Firefox borks sendMessage in same-origin iframes that have 'src' with a real path on the site.
|
|
|
|
* We implement a workaround for the initial styleApply case only.
|
|
|
|
* Everything else (like toggling of styles) is still buggy.
|
|
|
|
* @param {Object} msg
|
|
|
|
* @param {Function} callback
|
|
|
|
* @returns {Boolean|undefined}
|
|
|
|
*/
|
2018-10-10 16:54:38 +00:00
|
|
|
// function getStylesFallback(msg) {
|
|
|
|
// if (window !== parent &&
|
|
|
|
// location.href !== 'about:blank') {
|
|
|
|
// try {
|
|
|
|
// if (parent.location.origin === location.origin &&
|
|
|
|
// parent.location.href !== location.href) {
|
|
|
|
// chrome.runtime.connect({name: 'getStyles:' + JSON.stringify(msg)});
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// } catch (e) {}
|
|
|
|
// }
|
|
|
|
// }
|
2018-08-09 17:33:04 +00:00
|
|
|
|
2018-10-06 05:02:45 +00:00
|
|
|
function applyOnMessage(request) {
|
2018-10-13 13:29:06 +00:00
|
|
|
if (request.method === 'ping') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (STYLE_VIA_API) {
|
|
|
|
if (request.method === 'urlChanged') {
|
|
|
|
request.method = 'styleReplaceAll';
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2018-10-04 04:46:19 +00:00
|
|
|
API.styleViaAPI(request);
|
2017-11-25 21:56:57 +00:00
|
|
|
return;
|
2017-11-13 12:28:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
switch (request.method) {
|
|
|
|
case 'styleDeleted':
|
2018-10-09 16:41:07 +00:00
|
|
|
removeStyle(request.style);
|
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':
|
2018-10-06 17:42:43 +00:00
|
|
|
if (request.codeIsUpdated === false) {
|
2017-11-25 21:56:57 +00:00
|
|
|
applyStyleState(request.style);
|
2018-10-06 17:42:43 +00:00
|
|
|
} else if (request.style.enabled) {
|
2018-10-13 08:39:54 +00:00
|
|
|
API.getSectionsByUrl(getMatchUrl(), request.style.id)
|
2018-10-06 17:42:43 +00:00
|
|
|
.then(sections => {
|
|
|
|
if (!sections[request.style.id]) {
|
|
|
|
removeStyle(request.style);
|
|
|
|
} else {
|
2018-10-13 13:29:06 +00:00
|
|
|
applyStyles(sections);
|
2018-10-06 17:42:43 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-25 21:56:57 +00:00
|
|
|
} else {
|
|
|
|
removeStyle(request.style);
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
case 'styleAdded':
|
|
|
|
if (request.style.enabled) {
|
2018-10-13 08:39:54 +00:00
|
|
|
API.getSectionsByUrl(getMatchUrl(), request.style.id)
|
2018-10-05 10:47:52 +00:00
|
|
|
.then(applyStyles);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2018-10-07 13:20:39 +00:00
|
|
|
case 'urlChanged':
|
2018-10-13 08:39:54 +00:00
|
|
|
API.getSectionsByUrl(getMatchUrl())
|
2018-10-10 15:05:20 +00:00
|
|
|
.then(replaceAll);
|
2018-10-07 13:20:39 +00:00
|
|
|
break;
|
|
|
|
|
2018-10-11 18:02:14 +00:00
|
|
|
case 'backgroundReady':
|
2018-10-13 16:40:23 +00:00
|
|
|
initializing
|
|
|
|
.catch(err => {
|
|
|
|
if (msg.RX_NO_RECEIVER.test(err.message)) {
|
|
|
|
return init();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.error);
|
2018-10-11 18:02:14 +00:00
|
|
|
break;
|
2018-10-13 06:50:31 +00:00
|
|
|
|
|
|
|
case 'updateCount':
|
|
|
|
updateCount();
|
|
|
|
break;
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function doDisableAll(disable = disableAll) {
|
|
|
|
if (!disable === !disableAll) {
|
|
|
|
return;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
disableAll = disable;
|
2018-10-13 13:29:06 +00:00
|
|
|
if (STYLE_VIA_API) {
|
|
|
|
API.styleViaAPI({method: 'prefChanged', prefs: {disableAll}});
|
|
|
|
} else {
|
|
|
|
Array.prototype.forEach.call(document.styleSheets, stylesheet => {
|
|
|
|
if (stylesheet.ownerNode.matches(`style.stylus[id^="${ID_PREFIX}"]`)
|
|
|
|
&& stylesheet.disabled !== disable) {
|
|
|
|
stylesheet.disabled = disable;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-04-28 23:36:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 17:39:10 +00:00
|
|
|
function fetchParentDomain() {
|
|
|
|
if (parentDomain) {
|
|
|
|
return Promise.resolve();
|
2017-04-21 08:59:01 +00:00
|
|
|
}
|
2018-10-10 19:21:38 +00:00
|
|
|
return API.getTabUrlPrefix()
|
2018-10-10 17:39:10 +00:00
|
|
|
.then(newDomain => {
|
|
|
|
parentDomain = newDomain;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateExposeIframes() {
|
2018-10-10 19:21:38 +00:00
|
|
|
if (!prefs.get('exposeIframes') || window === parent || !styleElements.size) {
|
2017-11-25 21:56:57 +00:00
|
|
|
document.documentElement.removeAttribute('stylus-iframe');
|
2018-10-10 17:39:10 +00:00
|
|
|
} else {
|
|
|
|
fetchParentDomain().then(() => {
|
|
|
|
document.documentElement.setAttribute('stylus-iframe', 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-10-11 07:42:23 +00:00
|
|
|
function updateCount() {
|
|
|
|
if (window !== parent) {
|
|
|
|
// we don't care about iframes
|
|
|
|
return;
|
|
|
|
}
|
2018-10-13 16:40:23 +00:00
|
|
|
if (/^\w+?-extension:\/\/.+(popup|options)\.html$/.test(location.href)) {
|
|
|
|
// popup and the option page are not tabs
|
|
|
|
return;
|
|
|
|
}
|
2018-10-13 15:57:45 +00:00
|
|
|
if (STYLE_VIA_API) {
|
2018-10-13 16:40:23 +00:00
|
|
|
API.styleViaAPI({method: 'updateCount'}).catch(msg.ignoreError);
|
2018-10-13 15:57:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-11 07:42:23 +00:00
|
|
|
let count = 0;
|
|
|
|
for (const id of styleElements.keys()) {
|
|
|
|
if (!disabledElements.has(id)) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we have to send the tabId so we can't use `sendBg` that is used by `API`
|
|
|
|
msg.send({
|
|
|
|
method: 'invokeAPI',
|
|
|
|
name: 'updateIconBadge',
|
|
|
|
args: [count]
|
2018-10-13 15:57:45 +00:00
|
|
|
}).catch(msg.ignoreError);
|
2018-10-11 07:42:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function applyStyleState({id, enabled}) {
|
|
|
|
const inCache = disabledElements.get(id) || styleElements.get(id);
|
|
|
|
const inDoc = document.getElementById(ID_PREFIX + id);
|
|
|
|
if (enabled) {
|
|
|
|
if (inDoc) {
|
|
|
|
return;
|
|
|
|
} else if (inCache) {
|
|
|
|
addStyleElement(inCache);
|
|
|
|
disabledElements.delete(id);
|
|
|
|
} else {
|
2018-10-13 08:39:54 +00:00
|
|
|
return API.getSectionsByUrl(getMatchUrl(), id)
|
2018-10-05 10:47:52 +00:00
|
|
|
.then(applyStyles);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2017-04-12 09:54:55 +00:00
|
|
|
} else {
|
2017-11-25 21:56:57 +00:00
|
|
|
if (inDoc) {
|
|
|
|
disabledElements.set(id, inDoc);
|
2017-12-09 15:41:05 +00:00
|
|
|
docRootObserver.evade(() => inDoc.remove());
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2017-04-12 09:54:55 +00:00
|
|
|
}
|
2018-10-11 07:42:23 +00:00
|
|
|
updateCount();
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-07-12 20:02:17 +00:00
|
|
|
|
2018-10-11 09:32:27 +00:00
|
|
|
function removeStyle({id}) {
|
2017-11-25 21:56:57 +00:00
|
|
|
const el = document.getElementById(ID_PREFIX + id);
|
|
|
|
if (el) {
|
2018-10-11 09:32:27 +00:00
|
|
|
docRootObserver.evade(() => el.remove());
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
disabledElements.delete(id);
|
2018-10-11 09:32:27 +00:00
|
|
|
if (styleElements.delete(id)) {
|
2018-10-11 07:42:23 +00:00
|
|
|
updateCount();
|
|
|
|
}
|
2017-04-28 23:36:10 +00:00
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
|
2018-10-13 13:29:06 +00:00
|
|
|
function applyStyles(sections, done) {
|
|
|
|
if (!Object.keys(sections).length) {
|
2018-10-11 10:13:24 +00:00
|
|
|
if (done) {
|
|
|
|
done();
|
|
|
|
}
|
2018-10-11 07:42:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-09 18:29:49 +00:00
|
|
|
if (!document.documentElement) {
|
|
|
|
new MutationObserver((mutations, observer) => {
|
|
|
|
if (document.documentElement) {
|
|
|
|
observer.disconnect();
|
2018-10-13 13:29:06 +00:00
|
|
|
applyStyles(sections, done);
|
2017-12-09 18:29:49 +00:00
|
|
|
}
|
|
|
|
}).observe(document, {childList: true});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-11 09:32:27 +00:00
|
|
|
if (docRootObserver) {
|
|
|
|
docRootObserver.stop();
|
|
|
|
} else {
|
|
|
|
initDocRootObserver();
|
|
|
|
}
|
2018-10-13 13:29:06 +00:00
|
|
|
for (const section of Object.values(sections)) {
|
|
|
|
applySections(section.id, section.code.join(''));
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2018-10-11 09:32:27 +00:00
|
|
|
docRootObserver.firstStart();
|
2017-11-25 21:56:57 +00:00
|
|
|
|
|
|
|
if (!isOwnPage && !docRewriteObserver && styleElements.size) {
|
|
|
|
initDocRewriteObserver();
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 17:39:10 +00:00
|
|
|
updateExposeIframes();
|
2018-10-11 09:32:27 +00:00
|
|
|
updateCount();
|
2018-10-11 10:13:24 +00:00
|
|
|
if (done) {
|
|
|
|
done();
|
|
|
|
}
|
2017-11-21 06:45:44 +00:00
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
|
2018-10-11 09:32:27 +00:00
|
|
|
function applySections(id, code) {
|
|
|
|
let el = styleElements.get(id) || document.getElementById(ID_PREFIX + id);
|
|
|
|
if (el && CHROME < 3321) {
|
|
|
|
// workaround for Chrome devtools bug fixed in v65
|
|
|
|
el.remove();
|
|
|
|
el = null;
|
2018-01-15 16:42:30 +00:00
|
|
|
}
|
|
|
|
if (!el) {
|
2017-11-25 21:56:57 +00:00
|
|
|
if (document.documentElement instanceof SVGSVGElement) {
|
|
|
|
// SVG document style
|
|
|
|
el = document.createElementNS('http://www.w3.org/2000/svg', 'style');
|
|
|
|
} else if (document instanceof XMLDocument) {
|
|
|
|
// XML document style
|
|
|
|
el = document.createElementNS('http://www.w3.org/1999/xhtml', 'style');
|
|
|
|
} else {
|
|
|
|
// HTML document style; also works on HTML-embedded SVG
|
|
|
|
el = document.createElement('style');
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2018-10-11 09:32:27 +00:00
|
|
|
el.id = ID_PREFIX + id;
|
2018-08-24 18:12:22 +00:00
|
|
|
el.type = 'text/css';
|
2017-11-25 21:56:57 +00:00
|
|
|
// SVG className is not a string, but an instance of SVGAnimatedString
|
|
|
|
el.classList.add('stylus');
|
|
|
|
addStyleElement(el);
|
|
|
|
}
|
2018-10-11 09:32:27 +00:00
|
|
|
if (el.textContent !== code) {
|
|
|
|
setStyleContent(el, code);
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
styleElements.set(id, el);
|
2018-10-11 09:32:27 +00:00
|
|
|
disabledElements.delete(id);
|
2017-11-25 21:56:57 +00:00
|
|
|
return el;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function addStyleElement(newElement) {
|
|
|
|
if (!ROOT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let next;
|
|
|
|
const newStyleId = getStyleId(newElement);
|
|
|
|
for (const el of styleElements.values()) {
|
|
|
|
if (el.parentNode && !el.id.endsWith('-ghost') && getStyleId(el) > newStyleId) {
|
|
|
|
next = el.parentNode === ROOT ? el : null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (next === newElement.nextElementSibling) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-06 10:10:47 +00:00
|
|
|
const insert = () => {
|
2017-12-09 15:41:05 +00:00
|
|
|
ROOT.insertBefore(newElement, next || null);
|
|
|
|
if (disableAll) {
|
|
|
|
newElement.disabled = true;
|
|
|
|
}
|
2018-10-06 10:10:47 +00:00
|
|
|
};
|
|
|
|
if (docRootObserver) {
|
|
|
|
docRootObserver.evade(insert);
|
|
|
|
} else {
|
|
|
|
insert();
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
|
|
|
function replaceAll(newStyles) {
|
|
|
|
const oldStyles = Array.prototype.slice.call(
|
|
|
|
document.querySelectorAll(`style.stylus[id^="${ID_PREFIX}"]`));
|
|
|
|
oldStyles.forEach(el => (el.id += '-ghost'));
|
|
|
|
styleElements.clear();
|
|
|
|
disabledElements.clear();
|
|
|
|
applyStyles(newStyles);
|
2018-10-11 09:32:27 +00:00
|
|
|
const removeOld = () => oldStyles.forEach(el => el.remove());
|
2018-10-10 15:05:20 +00:00
|
|
|
if (docRewriteObserver) {
|
2018-10-11 09:32:27 +00:00
|
|
|
docRootObserver.evade(removeOld);
|
|
|
|
} else {
|
|
|
|
removeOld();
|
2018-10-10 15:05:20 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2017-12-09 18:30:00 +00:00
|
|
|
function applyTransitionPatch() {
|
|
|
|
// CSS transition bug workaround: since we insert styles asynchronously,
|
|
|
|
// the browsers, especially Firefox, may apply all transitions on page load
|
|
|
|
const className = chrome.runtime.id + '-transition-bug-fix';
|
|
|
|
const docId = document.documentElement.id ? '#' + document.documentElement.id : '';
|
|
|
|
document.documentElement.classList.add(className);
|
|
|
|
applySections(0, `
|
2018-10-11 15:39:11 +00:00
|
|
|
${docId}.${CSS.escape(className)}:root * {
|
|
|
|
transition: none !important;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
// repaint
|
|
|
|
// eslint-disable-next-line no-unused-expressions
|
|
|
|
document.documentElement.offsetWidth;
|
|
|
|
removeStyle({id: 0});
|
|
|
|
document.documentElement.classList.remove(className);
|
2017-12-09 18:30:00 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
|
|
|
function getStyleId(el) {
|
|
|
|
return parseInt(el.id.substr(ID_PREFIX.length));
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2018-10-11 09:32:27 +00:00
|
|
|
function orphanCheck(e) {
|
|
|
|
if (e && e.detail.method !== 'orphan') {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
if (chrome.i18n && chrome.i18n.getUILanguage()) {
|
|
|
|
return true;
|
2017-08-20 11:24:50 +00:00
|
|
|
}
|
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
|
2018-01-10 13:33:45 +00:00
|
|
|
[docRewriteObserver, docRootObserver].forEach(ob => ob && ob.disconnect());
|
2017-11-25 21:56:57 +00:00
|
|
|
window.removeEventListener(chrome.runtime.id, orphanCheck, true);
|
2018-01-10 13:44:29 +00:00
|
|
|
try {
|
2018-10-06 05:02:45 +00:00
|
|
|
msg.off(applyOnMessage);
|
2018-01-10 13:44:29 +00:00
|
|
|
} catch (e) {}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
|
|
|
function initDocRewriteObserver() {
|
|
|
|
// detect documentElement being rewritten from inside the script
|
|
|
|
docRewriteObserver = new MutationObserver(mutations => {
|
|
|
|
for (let m = mutations.length; --m >= 0;) {
|
|
|
|
const added = mutations[m].addedNodes;
|
|
|
|
for (let n = added.length; --n >= 0;) {
|
|
|
|
if (added[n].localName === 'html') {
|
|
|
|
reinjectStyles();
|
|
|
|
return;
|
|
|
|
}
|
2017-03-28 04:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
});
|
|
|
|
docRewriteObserver.observe(document, {childList: true});
|
|
|
|
// detect dynamic iframes rewritten after creation by the embedder i.e. externally
|
|
|
|
setTimeout(() => {
|
|
|
|
if (document.documentElement !== ROOT) {
|
|
|
|
reinjectStyles();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// re-add styles if we detect documentElement being recreated
|
|
|
|
function reinjectStyles() {
|
|
|
|
if (!styleElements) {
|
2017-11-22 17:19:01 +00:00
|
|
|
orphanCheck();
|
2017-11-25 21:56:57 +00:00
|
|
|
return;
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
ROOT = document.documentElement;
|
|
|
|
docRootObserver.stop();
|
|
|
|
const imported = [];
|
|
|
|
for (const [id, el] of styleElements.entries()) {
|
|
|
|
const copy = document.importNode(el, true);
|
|
|
|
el.textContent += ' '; // invalidate CSSOM cache
|
|
|
|
imported.push([id, copy]);
|
|
|
|
addStyleElement(copy);
|
|
|
|
}
|
|
|
|
docRootObserver.start();
|
|
|
|
styleElements = new Map(imported);
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-28 04:08:37 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function initDocRootObserver() {
|
|
|
|
let lastRestorationTime = 0;
|
|
|
|
let restorationCounter = 0;
|
|
|
|
let observing = false;
|
|
|
|
let sorting = false;
|
2018-01-10 13:33:45 +00:00
|
|
|
let observer;
|
2017-11-25 21:56:57 +00:00
|
|
|
// allow any types of elements between ours, except for the following:
|
2017-12-05 05:50:07 +00:00
|
|
|
const ORDERED_TAGS = ['head', 'body', 'frameset', 'style', 'link'];
|
2017-11-21 06:45:44 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
init();
|
|
|
|
return;
|
|
|
|
|
|
|
|
function init() {
|
2018-01-10 13:33:45 +00:00
|
|
|
observer = new MutationObserver(sortStyleElements);
|
2018-08-06 14:35:06 +00:00
|
|
|
docRootObserver = {firstStart, start, stop, evade, disconnect: stop};
|
2017-12-05 05:50:07 +00:00
|
|
|
setTimeout(sortStyleElements);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2018-08-06 14:35:06 +00:00
|
|
|
function firstStart() {
|
|
|
|
if (sortStyleMap()) {
|
2017-11-22 17:19:01 +00:00
|
|
|
sortStyleElements();
|
|
|
|
}
|
2018-08-06 14:35:06 +00:00
|
|
|
start();
|
|
|
|
}
|
|
|
|
function start() {
|
2018-01-10 13:33:45 +00:00
|
|
|
if (!observing && ROOT && observer) {
|
|
|
|
observer.observe(ROOT, {childList: true});
|
2017-11-22 17:19:01 +00:00
|
|
|
observing = true;
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
function stop() {
|
2017-11-22 17:19:01 +00:00
|
|
|
if (observing) {
|
2018-01-10 13:33:45 +00:00
|
|
|
observer.takeRecords();
|
|
|
|
observer.disconnect();
|
2017-11-22 17:19:01 +00:00
|
|
|
observing = false;
|
|
|
|
}
|
2017-08-16 11:00:59 +00:00
|
|
|
}
|
2017-12-09 15:41:05 +00:00
|
|
|
function evade(fn) {
|
|
|
|
const wasObserving = observing;
|
|
|
|
if (observing) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
fn();
|
|
|
|
if (wasObserving) {
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
function sortStyleMap() {
|
|
|
|
const list = [];
|
|
|
|
let prevStyleId = 0;
|
|
|
|
let needsSorting = false;
|
|
|
|
for (const entry of styleElements.entries()) {
|
|
|
|
list.push(entry);
|
|
|
|
const el = entry[1];
|
|
|
|
const styleId = getStyleId(el);
|
|
|
|
el.styleId = styleId;
|
|
|
|
needsSorting |= styleId < prevStyleId;
|
|
|
|
prevStyleId = styleId;
|
|
|
|
}
|
|
|
|
if (needsSorting) {
|
|
|
|
styleElements = new Map(list.sort((a, b) => a[1].styleId - b[1].styleId));
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
2018-08-24 18:12:22 +00:00
|
|
|
function sortStyleElements() {
|
|
|
|
if (!observing) return;
|
2017-12-07 07:12:01 +00:00
|
|
|
let prevExpected = document.documentElement.lastElementChild;
|
|
|
|
while (prevExpected && isSkippable(prevExpected, true)) {
|
|
|
|
prevExpected = prevExpected.previousElementSibling;
|
|
|
|
}
|
2018-08-24 18:12:22 +00:00
|
|
|
if (!prevExpected) return;
|
2017-11-25 21:56:57 +00:00
|
|
|
for (const el of styleElements.values()) {
|
|
|
|
if (!isMovable(el)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-05 05:50:07 +00:00
|
|
|
while (true) {
|
|
|
|
const next = prevExpected.nextElementSibling;
|
|
|
|
if (next && isSkippable(next)) {
|
|
|
|
prevExpected = next;
|
2017-12-07 07:12:01 +00:00
|
|
|
} else if (
|
|
|
|
next === el ||
|
2017-12-09 16:37:31 +00:00
|
|
|
next === el.previousElementSibling && next ||
|
2017-12-07 07:12:01 +00:00
|
|
|
moveAfter(el, next || prevExpected)) {
|
2017-12-05 05:50:07 +00:00
|
|
|
prevExpected = el;
|
2017-11-25 21:56:57 +00:00
|
|
|
break;
|
2017-12-05 05:50:07 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
if (sorting) {
|
|
|
|
sorting = false;
|
2018-01-10 13:33:45 +00:00
|
|
|
if (observer) observer.takeRecords();
|
2017-12-07 08:10:32 +00:00
|
|
|
if (!restorationLimitExceeded()) {
|
|
|
|
start();
|
|
|
|
} else {
|
|
|
|
setTimeout(start, 1000);
|
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
function isMovable(el) {
|
|
|
|
return el.parentNode || !disabledElements.has(getStyleId(el));
|
|
|
|
}
|
2017-12-07 07:12:01 +00:00
|
|
|
function isSkippable(el, skipOwnStyles) {
|
2017-11-25 21:56:57 +00:00
|
|
|
return !ORDERED_TAGS.includes(el.localName) ||
|
|
|
|
el.id.startsWith(ID_PREFIX) &&
|
2017-12-07 07:12:01 +00:00
|
|
|
(skipOwnStyles || el.id.endsWith('-ghost')) &&
|
2017-11-25 21:56:57 +00:00
|
|
|
el.localName === 'style' &&
|
|
|
|
el.className === 'stylus';
|
|
|
|
}
|
|
|
|
function moveAfter(el, expected) {
|
|
|
|
if (!sorting) {
|
|
|
|
sorting = true;
|
2018-01-10 18:56:14 +00:00
|
|
|
stop();
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
expected.insertAdjacentElement('afterend', el);
|
2017-11-21 06:45:44 +00:00
|
|
|
if (el.disabled !== disableAll) {
|
|
|
|
// moving an element resets its 'disabled' state
|
|
|
|
el.disabled = disableAll;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
function restorationLimitExceeded() {
|
|
|
|
const t = performance.now();
|
|
|
|
if (t - lastRestorationTime > 1000) {
|
|
|
|
restorationCounter = 0;
|
|
|
|
}
|
|
|
|
lastRestorationTime = t;
|
2017-12-09 15:41:05 +00:00
|
|
|
return ++restorationCounter > 5;
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2017-11-21 06:45:44 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
})();
|