2017-03-26 02:30:59 +00:00
|
|
|
/* eslint no-var: 0 */
|
|
|
|
'use strict';
|
2017-03-15 13:11:33 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
(() => {
|
2017-12-09 17:34:09 +00:00
|
|
|
if (typeof window.applyOnMessage === 'function') {
|
|
|
|
// some weird bug in new Chrome: the content script gets injected multiple times
|
|
|
|
return;
|
|
|
|
}
|
2018-01-15 16:42:30 +00:00
|
|
|
const CHROME = chrome.app ? parseInt(navigator.userAgent.match(/Chrom\w+\/(?:\d+\.){2}(\d+)|$/)[1]) : NaN;
|
2017-11-25 21:56:57 +00:00
|
|
|
var ID_PREFIX = 'stylus-';
|
|
|
|
var ROOT = document.documentElement;
|
|
|
|
var isOwnPage = location.protocol.endsWith('-extension:');
|
|
|
|
var disableAll = false;
|
|
|
|
var exposeIframes = false;
|
|
|
|
var styleElements = new Map();
|
|
|
|
var disabledElements = new Map();
|
|
|
|
var retiredStyleTimers = new Map();
|
|
|
|
var docRewriteObserver;
|
|
|
|
var docRootObserver;
|
|
|
|
|
2018-09-05 12:58:15 +00:00
|
|
|
// 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 FF_BUG461 = !CHROME && !isOwnPage && !Event.prototype.getPreventDefault;
|
|
|
|
const pageContextQueue = [];
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
requestStyles();
|
|
|
|
chrome.runtime.onMessage.addListener(applyOnMessage);
|
|
|
|
window.applyOnMessage = applyOnMessage;
|
|
|
|
|
|
|
|
if (!isOwnPage) {
|
|
|
|
window.dispatchEvent(new CustomEvent(chrome.runtime.id));
|
|
|
|
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-04 10:08:19 +00:00
|
|
|
// FIXME: does it work with styleViaAPI?
|
2018-10-04 10:05:41 +00:00
|
|
|
prefs.subscribe(['disableAll'], (key, value) => doDisableAll(value));
|
|
|
|
prefs.subscribe(['exposeIframes'], (key, value) => doExposeIframes(value));
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function requestStyles(options, callback = applyStyles) {
|
|
|
|
if (!chrome.app && document instanceof XMLDocument) {
|
|
|
|
chrome.runtime.sendMessage({method: 'styleViaAPI', action: 'styleApply'});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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) {}
|
|
|
|
}
|
|
|
|
const request = Object.assign({
|
2018-01-19 12:37:26 +00:00
|
|
|
method: 'getStylesForFrame',
|
2017-11-25 21:56:57 +00:00
|
|
|
asHash: true,
|
2018-01-10 16:51:05 +00:00
|
|
|
matchUrl,
|
2017-11-25 21:56:57 +00:00
|
|
|
}, options);
|
|
|
|
// On own pages we request the styles directly to minimize delay and flicker
|
2018-01-01 17:02:49 +00:00
|
|
|
if (typeof API === 'function') {
|
|
|
|
API.getStyles(request).then(callback);
|
2018-08-09 17:33:04 +00:00
|
|
|
} else if (!CHROME && getStylesFallback(request)) {
|
|
|
|
// NOP
|
2017-11-25 21:56:57 +00:00
|
|
|
} else {
|
|
|
|
chrome.runtime.sendMessage(request, callback);
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
|
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}
|
|
|
|
*/
|
|
|
|
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) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function applyOnMessage(request, sender, sendResponse) {
|
|
|
|
if (request.styles === 'DIY') {
|
|
|
|
// Do-It-Yourself tells our built-in pages to fetch the styles directly
|
|
|
|
// which is faster because IPC messaging JSON-ifies everything internally
|
|
|
|
requestStyles({}, styles => {
|
|
|
|
request.styles = styles;
|
|
|
|
applyOnMessage(request);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2017-04-21 08:59:01 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
if (!chrome.app && document instanceof XMLDocument && request.method !== 'ping') {
|
|
|
|
request.action = request.method;
|
|
|
|
request.method = 'styleViaAPI';
|
|
|
|
request.styles = null;
|
|
|
|
if (request.style) {
|
|
|
|
request.style.sections = null;
|
|
|
|
}
|
|
|
|
chrome.runtime.sendMessage(request);
|
|
|
|
return;
|
2017-11-13 12:28:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
switch (request.method) {
|
|
|
|
case 'styleDeleted':
|
|
|
|
removeStyle(request);
|
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
case 'styleUpdated':
|
|
|
|
if (request.codeIsUpdated === false) {
|
|
|
|
applyStyleState(request.style);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (request.style.enabled) {
|
|
|
|
removeStyle({id: request.style.id, retire: true});
|
|
|
|
requestStyles({id: request.style.id});
|
|
|
|
} 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) {
|
|
|
|
requestStyles({id: request.style.id});
|
|
|
|
}
|
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
case 'styleApply':
|
|
|
|
applyStyles(request.styles);
|
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
case 'styleReplaceAll':
|
|
|
|
replaceAll(request.styles);
|
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
case 'ping':
|
|
|
|
sendResponse(true);
|
|
|
|
break;
|
|
|
|
}
|
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;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function doExposeIframes(state = exposeIframes) {
|
2018-01-20 13:26:13 +00:00
|
|
|
if (state === exposeIframes ||
|
|
|
|
state === true && typeof exposeIframes === 'string' ||
|
|
|
|
window === parent) {
|
2017-04-21 08:59:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
exposeIframes = state;
|
|
|
|
const attr = document.documentElement.getAttribute('stylus-iframe');
|
2018-01-19 12:37:26 +00:00
|
|
|
if (state && state !== attr) {
|
|
|
|
document.documentElement.setAttribute('stylus-iframe', state);
|
|
|
|
} else if (!state && attr !== undefined) {
|
2017-11-25 21:56:57 +00:00
|
|
|
document.documentElement.removeAttribute('stylus-iframe');
|
2017-04-12 06:15:57 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +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 {
|
|
|
|
requestStyles({id});
|
|
|
|
}
|
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
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-07-12 20:02:17 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function removeStyle({id, retire = false}) {
|
|
|
|
const el = document.getElementById(ID_PREFIX + id);
|
|
|
|
if (el) {
|
|
|
|
if (retire) {
|
|
|
|
// to avoid page flicker when the style is updated
|
|
|
|
// instead of removing it immediately we rename its ID and queue it
|
|
|
|
// to be deleted in applyStyles after a new version is fetched and applied
|
2018-01-10 13:33:45 +00:00
|
|
|
const deadID = id + '-ghost';
|
2017-11-25 21:56:57 +00:00
|
|
|
el.id = ID_PREFIX + deadID;
|
|
|
|
// in case something went wrong and new style was never applied
|
|
|
|
retiredStyleTimers.set(deadID, setTimeout(removeStyle, 1000, {id: deadID}));
|
|
|
|
} else {
|
2017-12-09 15:41:05 +00:00
|
|
|
docRootObserver.evade(() => el.remove());
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
styleElements.delete(ID_PREFIX + id);
|
|
|
|
disabledElements.delete(id);
|
|
|
|
retiredStyleTimers.delete(id);
|
2017-04-28 23:36:10 +00:00
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function applyStyles(styles) {
|
|
|
|
if (!styles) {
|
|
|
|
// Chrome is starting up
|
|
|
|
requestStyles();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-09 18:29:49 +00:00
|
|
|
|
|
|
|
if (!document.documentElement) {
|
|
|
|
new MutationObserver((mutations, observer) => {
|
|
|
|
if (document.documentElement) {
|
|
|
|
observer.disconnect();
|
|
|
|
applyStyles(styles);
|
|
|
|
}
|
|
|
|
}).observe(document, {childList: true});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
if ('disableAll' in styles) {
|
|
|
|
doDisableAll(styles.disableAll);
|
|
|
|
}
|
|
|
|
if ('exposeIframes' in styles) {
|
|
|
|
doExposeIframes(styles.exposeIframes);
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 16:51:05 +00:00
|
|
|
const gotNewStyles = styles.length || styles.needTransitionPatch;
|
2017-11-25 21:56:57 +00:00
|
|
|
if (gotNewStyles) {
|
|
|
|
if (docRootObserver) {
|
|
|
|
docRootObserver.stop();
|
|
|
|
} else {
|
|
|
|
initDocRootObserver();
|
2017-09-03 08:12:18 +00:00
|
|
|
}
|
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 (styles.needTransitionPatch) {
|
2017-12-09 18:30:00 +00:00
|
|
|
applyTransitionPatch();
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gotNewStyles) {
|
|
|
|
for (const id in styles) {
|
2018-01-10 16:51:05 +00:00
|
|
|
const sections = styles[id];
|
|
|
|
if (!Array.isArray(sections)) continue;
|
|
|
|
applySections(id, sections.map(({code}) => code).join('\n'));
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2018-08-06 14:35:06 +00:00
|
|
|
docRootObserver.firstStart();
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 12:58:15 +00:00
|
|
|
if (FF_BUG461 && (gotNewStyles || styles.needTransitionPatch)) {
|
|
|
|
setContentsInPageContext();
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
if (!isOwnPage && !docRewriteObserver && styleElements.size) {
|
|
|
|
initDocRewriteObserver();
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
if (retiredStyleTimers.size) {
|
|
|
|
setTimeout(() => {
|
|
|
|
for (const [id, timer] of retiredStyleTimers.entries()) {
|
|
|
|
removeStyle({id});
|
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-11-21 06:45:44 +00:00
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function applySections(styleId, code) {
|
|
|
|
const id = ID_PREFIX + styleId;
|
|
|
|
let el = styleElements.get(id) || document.getElementById(id);
|
2018-01-15 16:42:30 +00:00
|
|
|
if (el && el.textContent !== code) {
|
|
|
|
if (CHROME < 3321) {
|
|
|
|
// workaround for Chrome devtools bug fixed in v65
|
|
|
|
el.remove();
|
|
|
|
el = null;
|
2018-09-05 12:58:15 +00:00
|
|
|
} else if (FF_BUG461) {
|
|
|
|
pageContextQueue.push({id: el.id, el, code});
|
2018-01-15 16:42:30 +00:00
|
|
|
} else {
|
|
|
|
el.textContent = code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-08-24 18:12:22 +00:00
|
|
|
el.id = id;
|
|
|
|
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');
|
2018-09-05 12:58:15 +00:00
|
|
|
if (FF_BUG461) {
|
|
|
|
pageContextQueue.push({id: el.id, el, code});
|
2018-08-24 18:12:22 +00:00
|
|
|
} else {
|
|
|
|
el.textContent = code;
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
addStyleElement(el);
|
|
|
|
}
|
|
|
|
styleElements.set(id, el);
|
|
|
|
disabledElements.delete(Number(styleId));
|
|
|
|
return el;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
|
2018-09-05 12:58:15 +00:00
|
|
|
function setContentsInPageContext() {
|
|
|
|
try {
|
2018-09-11 14:40:36 +00:00
|
|
|
(document.head || ROOT).appendChild(document.createElement('script')).text = `(${queue => {
|
2018-09-05 12:58:15 +00:00
|
|
|
document.currentScript.remove();
|
2018-09-11 14:40:36 +00:00
|
|
|
for (const {id, code} of queue) {
|
2018-09-11 14:39:43 +00:00
|
|
|
const el = document.getElementById(id) ||
|
|
|
|
document.querySelector('style.stylus[id="' + id + '"]');
|
|
|
|
if (!el) continue;
|
|
|
|
const {disabled} = el.sheet;
|
|
|
|
el.textContent = code;
|
|
|
|
el.sheet.disabled = disabled;
|
2018-09-05 12:58:15 +00:00
|
|
|
}
|
2018-09-11 14:40:36 +00:00
|
|
|
}})(${JSON.stringify(pageContextQueue)})`;
|
2018-09-05 12:58:15 +00:00
|
|
|
} catch (e) {}
|
|
|
|
let failedSome;
|
|
|
|
for (const {el, code} of pageContextQueue) {
|
|
|
|
if (el.textContent !== code) {
|
|
|
|
el.textContent = code;
|
|
|
|
failedSome = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (failedSome) {
|
|
|
|
console.debug('Could not set code of some styles in page context, ' +
|
|
|
|
'see https://github.com/openstyles/stylus/issues/461');
|
|
|
|
}
|
|
|
|
pageContextQueue.length = 0;
|
2018-08-24 18:12:22 +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;
|
|
|
|
}
|
2017-12-09 15:41:05 +00:00
|
|
|
docRootObserver.evade(() => {
|
|
|
|
ROOT.insertBefore(newElement, next || null);
|
|
|
|
if (disableAll) {
|
|
|
|
newElement.disabled = true;
|
|
|
|
}
|
|
|
|
});
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
|
|
|
function replaceAll(newStyles) {
|
2017-12-09 17:34:09 +00:00
|
|
|
if ('disableAll' in newStyles &&
|
|
|
|
disableAll === newStyles.disableAll &&
|
2017-12-09 18:16:41 +00:00
|
|
|
styleElements.size === countStylesInHash(newStyles) &&
|
2017-12-09 17:34:09 +00:00
|
|
|
[...styleElements.values()].every(el =>
|
|
|
|
el.disabled === disableAll &&
|
|
|
|
el.parentNode === ROOT &&
|
|
|
|
el.textContent === (newStyles[getStyleId(el)] || []).map(({code}) => code).join('\n'))) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
const oldStyles = Array.prototype.slice.call(
|
|
|
|
document.querySelectorAll(`style.stylus[id^="${ID_PREFIX}"]`));
|
|
|
|
oldStyles.forEach(el => (el.id += '-ghost'));
|
|
|
|
styleElements.clear();
|
|
|
|
disabledElements.clear();
|
|
|
|
[...retiredStyleTimers.values()].forEach(clearTimeout);
|
|
|
|
retiredStyleTimers.clear();
|
|
|
|
applyStyles(newStyles);
|
2017-12-09 17:34:09 +00:00
|
|
|
docRootObserver.evade(() =>
|
|
|
|
oldStyles.forEach(el => el.remove()));
|
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, `
|
|
|
|
${docId}.${className}:root * {
|
|
|
|
transition: none !important;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
setTimeout(() => {
|
|
|
|
removeStyle({id: 0});
|
|
|
|
document.documentElement.classList.remove(className);
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
|
2017-12-09 18:16:41 +00:00
|
|
|
function countStylesInHash(styleHash) {
|
|
|
|
let num = 0;
|
|
|
|
for (const k in styleHash) {
|
|
|
|
num += !isNaN(parseInt(k)) ? 1 : 0;
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function orphanCheck() {
|
|
|
|
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 {
|
|
|
|
chrome.runtime.onMessage.removeListener(applyOnMessage);
|
|
|
|
} 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
|
|
|
})();
|