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;
|
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 docRewriteObserver;
|
|
|
|
var docRootObserver;
|
|
|
|
|
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-11 09:32:27 +00:00
|
|
|
applyStyles(styles, () => {
|
|
|
|
if (styles.some(s => s.code.includes('transition'))) {
|
|
|
|
applyTransitionPatch();
|
|
|
|
}
|
|
|
|
});
|
2018-10-06 09:47:43 +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, {
|
|
|
|
detail: {method: 'orphan'}
|
|
|
|
}));
|
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: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-11 09:32:27 +00:00
|
|
|
const setStyleContent = createSetStyleContent();
|
|
|
|
|
|
|
|
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 FF_BUG461 = !CHROME && !isOwnPage && !Event.prototype.getPreventDefault;
|
|
|
|
const EVENT_NAME = chrome.runtime.id;
|
|
|
|
if (CHROME || isOwnPage || Event.prototype.getPreventDefault || !injectPageScript()) {
|
|
|
|
return (el, content) => {
|
|
|
|
// FIXME: do we have to keep el.sheet.disabled?
|
|
|
|
el.textContent = content;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return (el, content) => {
|
|
|
|
window.dispatchEvent(EVENT_NAME, new CustomEvent({detail: {
|
|
|
|
method: 'setStyleContent',
|
|
|
|
id: el.id,
|
|
|
|
content
|
|
|
|
}}));
|
|
|
|
};
|
|
|
|
|
|
|
|
function injectPageScript() {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
const scriptContent = EVENT_NAME => {
|
|
|
|
document.currentScript.remove();
|
|
|
|
window.dispatchEvent(new CustomEvent(EVENT_NAME, {
|
|
|
|
detail: {method: 'pageScriptOK'}
|
|
|
|
}));
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
script.text = `(${scriptContent})(${JSON.stringify(EVENT_NAME)})`;
|
|
|
|
let ok = false;
|
|
|
|
const check = e => {
|
|
|
|
if (e.detail.method === 'pageScriptOK') {
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window.addEventListener(EVENT_NAME, check, true);
|
|
|
|
ROOT.appendChild(script);
|
|
|
|
window.removeEventListener(EVENT_NAME, check, true);
|
|
|
|
script.remove();
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 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;
|
|
|
|
}
|
|
|
|
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]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-11 07:42:23 +00:00
|
|
|
return 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
|
|
|
}
|
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-11 09:32:27 +00:00
|
|
|
function applyStyles(styles, done) {
|
2018-10-11 07:42:23 +00:00
|
|
|
if (!styles.length) {
|
2018-10-11 09:32:27 +00:00
|
|
|
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-11 09:32:27 +00:00
|
|
|
applyStyles(styles, 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();
|
|
|
|
}
|
|
|
|
for (const section of styles) {
|
|
|
|
applySections(section.id, section.code);
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-10 17:39:10 +00:00
|
|
|
updateExposeIframes();
|
2018-10-11 09:32:27 +00:00
|
|
|
updateCount();
|
|
|
|
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, `
|
|
|
|
${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
|
|
|
|
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
|
|
|
})();
|