2017-03-26 02:30:59 +00:00
|
|
|
/* eslint no-var: 0 */
|
2018-10-10 16:54:38 +00:00
|
|
|
/* global msg API prefs */
|
2017-03-26 02:30:59 +00:00
|
|
|
'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 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 = [];
|
|
|
|
|
2018-10-06 09:47:43 +00:00
|
|
|
// FIXME: styleViaAPI
|
2018-10-06 17:42:43 +00:00
|
|
|
// FIXME: getStylesFallback?
|
2018-10-06 09:47:43 +00:00
|
|
|
if (!chrome.app && document instanceof XMLDocument) {
|
|
|
|
API.styleViaAPI({action: 'styleApply'});
|
|
|
|
} else {
|
|
|
|
API.getSectionsByUrl(getMatchUrl(), {enabled: true})
|
|
|
|
.then(result => {
|
|
|
|
const styles = Object.values(result);
|
2018-10-10 17:22:13 +00:00
|
|
|
// CSS transition bug workaround: since we insert styles asynchronously,
|
|
|
|
// the browsers, especially Firefox, may apply all transitions on page load
|
2018-10-06 09:47:43 +00:00
|
|
|
if (styles.some(s => s.code.includes('transition'))) {
|
|
|
|
applyTransitionPatch();
|
|
|
|
}
|
|
|
|
applyStyles(styles);
|
|
|
|
});
|
|
|
|
}
|
2018-10-06 05:02:45 +00:00
|
|
|
msg.onTab(applyOnMessage);
|
2017-11-25 21:56:57 +00:00
|
|
|
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-10 17:39:10 +00:00
|
|
|
let parentDomain;
|
|
|
|
|
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));
|
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-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-10-06 07:16:00 +00:00
|
|
|
function buildSections(cache) {
|
|
|
|
return Object.values(cache);
|
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}
|
|
|
|
*/
|
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) {
|
2017-11-25 21:56:57 +00:00
|
|
|
if (!chrome.app && document instanceof XMLDocument && request.method !== 'ping') {
|
|
|
|
request.action = request.method;
|
2018-10-04 04:46:19 +00:00
|
|
|
request.method = null;
|
2017-11-25 21:56:57 +00:00
|
|
|
request.styles = null;
|
|
|
|
if (request.style) {
|
|
|
|
request.style.sections = null;
|
|
|
|
}
|
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-06 09:47:43 +00:00
|
|
|
API.getSectionsByUrl(getMatchUrl(), {id: request.style.id})
|
2018-10-06 17:42:43 +00:00
|
|
|
.then(sections => {
|
|
|
|
if (!sections[request.style.id]) {
|
|
|
|
removeStyle(request.style);
|
|
|
|
} else {
|
|
|
|
applyStyles(buildSections(sections));
|
|
|
|
}
|
|
|
|
});
|
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-06 09:47:43 +00:00
|
|
|
API.getSectionsByUrl(getMatchUrl(), {id: request.style.id})
|
2018-10-05 10:47:52 +00:00
|
|
|
.then(buildSections)
|
|
|
|
.then(applyStyles);
|
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 'styleApply':
|
|
|
|
applyStyles(request.styles);
|
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2018-10-07 13:20:39 +00:00
|
|
|
case 'urlChanged':
|
2018-10-10 15:05:20 +00:00
|
|
|
API.getSectionsByUrl(getMatchUrl(), {enabled: true})
|
|
|
|
.then(buildSections)
|
|
|
|
.then(replaceAll);
|
2018-10-07 13:20:39 +00:00
|
|
|
break;
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
case 'ping':
|
2018-10-06 05:02:45 +00:00
|
|
|
return true;
|
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;
|
|
|
|
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 17:39:10 +00:00
|
|
|
return API.getTabDomain()
|
|
|
|
.then(newDomain => {
|
|
|
|
parentDomain = newDomain;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateExposeIframes() {
|
|
|
|
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
|
|
|
|
|
|
|
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-06 09:47:43 +00:00
|
|
|
API.getSectionsByUrl(getMatchUrl(), {id})
|
2018-10-05 10:47:52 +00:00
|
|
|
.then(buildSections)
|
|
|
|
.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
|
|
|
}
|
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) {
|
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;
|
|
|
|
}
|
|
|
|
|
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 (gotNewStyles) {
|
2018-10-04 04:46:19 +00:00
|
|
|
for (const section of styles) {
|
|
|
|
applySections(section.id, section.code);
|
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-10-04 04:46:19 +00:00
|
|
|
// FIXME
|
|
|
|
// if (FF_BUG461 && (gotNewStyles || styles.needTransitionPatch)) {
|
|
|
|
// setContentsInPageContext();
|
|
|
|
// }
|
2018-09-05 12:58:15 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-10-10 17:39:10 +00:00
|
|
|
|
|
|
|
updateExposeIframes();
|
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
|
|
|
|
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();
|
|
|
|
[...retiredStyleTimers.values()].forEach(clearTimeout);
|
|
|
|
retiredStyleTimers.clear();
|
|
|
|
applyStyles(newStyles);
|
2018-10-10 15:05:20 +00:00
|
|
|
if (docRewriteObserver) {
|
|
|
|
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
|
|
|
|
|
|
|
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 {
|
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
|
|
|
})();
|