2017-03-26 02:30:59 +00:00
|
|
|
/* eslint no-var: 0 */
|
2018-11-07 06:09:29 +00:00
|
|
|
/* global msg API prefs */
|
|
|
|
/* exported APPLY */
|
2017-03-26 02:30:59 +00:00
|
|
|
'use strict';
|
2017-03-15 13:11:33 +00:00
|
|
|
|
2018-11-07 06:09:29 +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-11-07 06:09:29 +00:00
|
|
|
const STYLE_VIA_API = !chrome.app && document instanceof XMLDocument;
|
2017-11-25 21:56:57 +00:00
|
|
|
var ID_PREFIX = 'stylus-';
|
2018-11-07 06:09:29 +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-11-07 06:09:29 +00:00
|
|
|
const setStyleContent = createSetStyleContent();
|
|
|
|
const initializing = init();
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
msg.onTab(applyOnMessage);
|
2017-11-25 21:56:57 +00:00
|
|
|
|
|
|
|
if (!isOwnPage) {
|
2018-11-07 06:09:29 +00:00
|
|
|
window.dispatchEvent(new CustomEvent(chrome.runtime.id, {
|
|
|
|
detail: pageObject({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-11-07 06:09:29 +00:00
|
|
|
let parentDomain;
|
|
|
|
|
|
|
|
prefs.subscribe(['disableAll'], (key, value) => doDisableAll(value));
|
|
|
|
if (window !== parent) {
|
|
|
|
prefs.subscribe(['exposeIframes'], updateExposeIframes);
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
if (STYLE_VIA_API) {
|
|
|
|
return API.styleViaAPI({method: 'styleApply'});
|
|
|
|
}
|
|
|
|
return API.getSectionsByUrl(getMatchUrl())
|
|
|
|
.then(result => {
|
|
|
|
ROOT = document.documentElement;
|
|
|
|
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'))) {
|
|
|
|
applyTransitionPatch();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-28 04:57:44 +00:00
|
|
|
let ready;
|
2018-11-07 06:09:29 +00:00
|
|
|
return (el, content) =>
|
2018-11-28 04:57:44 +00:00
|
|
|
checkPageScript().then(ok => {
|
2018-11-07 06:09:29 +00:00
|
|
|
if (!ok) {
|
|
|
|
const disabled = el.disabled;
|
|
|
|
el.textContent = content;
|
|
|
|
el.disabled = disabled;
|
|
|
|
} else {
|
|
|
|
const detail = pageObject({
|
|
|
|
method: 'setStyleContent',
|
|
|
|
id: el.id,
|
|
|
|
content
|
|
|
|
});
|
|
|
|
window.dispatchEvent(new CustomEvent(EVENT_NAME, {detail}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-28 04:57:44 +00:00
|
|
|
function checkPageScript() {
|
|
|
|
if (!ready) {
|
|
|
|
ready = CHROME || isOwnPage || Event.prototype.getPreventDefault ?
|
|
|
|
Promise.resolve(false) : injectPageScript();
|
|
|
|
}
|
|
|
|
return ready;
|
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function injectPageScript() {
|
|
|
|
const scriptContent = EVENT_NAME => {
|
|
|
|
document.currentScript.remove();
|
2018-11-21 17:09:48 +00:00
|
|
|
const available = checkStyleApplied();
|
|
|
|
if (available) {
|
|
|
|
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.disabled;
|
|
|
|
el.textContent = content;
|
|
|
|
el.disabled = disabled;
|
|
|
|
} else if (method === 'orphan') {
|
|
|
|
window.removeEventListener(EVENT_NAME, handler);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2018-11-21 17:09:48 +00:00
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
window.dispatchEvent(new CustomEvent(EVENT_NAME, {detail: {
|
|
|
|
method: 'init',
|
|
|
|
available
|
|
|
|
}}));
|
|
|
|
|
|
|
|
function checkStyleApplied() {
|
|
|
|
const style = document.createElement('style');
|
|
|
|
style.textContent = ':root{--stylus-applied:1}';
|
|
|
|
document.documentElement.appendChild(style);
|
|
|
|
const applied = getComputedStyle(document.documentElement)
|
|
|
|
.getPropertyValue('--stylus-applied');
|
|
|
|
style.remove();
|
|
|
|
return Boolean(applied);
|
|
|
|
}
|
2018-11-07 06:09:29 +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;
|
2018-12-29 02:55:52 +00:00
|
|
|
script.onerror = resolveFalse;
|
|
|
|
window.addEventListener('error', resolveFalse);
|
2018-11-21 17:09:48 +00:00
|
|
|
window.addEventListener(EVENT_NAME, handleInit);
|
2018-11-28 04:57:44 +00:00
|
|
|
(document.head || document.documentElement).appendChild(script);
|
2018-11-21 17:09:48 +00:00
|
|
|
return promise.then(result => {
|
|
|
|
script.remove();
|
|
|
|
window.removeEventListener(EVENT_NAME, handleInit);
|
2018-12-29 02:55:52 +00:00
|
|
|
window.removeEventListener('error', resolveFalse);
|
2018-11-21 17:09:48 +00:00
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
2018-12-29 02:55:52 +00:00
|
|
|
function resolveFalse() {
|
|
|
|
resolve(false);
|
|
|
|
}
|
|
|
|
|
2018-11-21 17:09:48 +00:00
|
|
|
function handleInit(e) {
|
|
|
|
if (e.detail.method === 'init') {
|
|
|
|
resolve(e.detail.available);
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function deferred() {
|
|
|
|
const o = {};
|
|
|
|
o.promise = new Promise((resolve, reject) => {
|
|
|
|
o.resolve = resolve;
|
|
|
|
o.reject = reject;
|
|
|
|
});
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-07 06:09:29 +00:00
|
|
|
return matchUrl;
|
2018-08-09 17:33:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function applyOnMessage(request) {
|
|
|
|
if (request.method === 'ping') {
|
|
|
|
return true;
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
if (STYLE_VIA_API) {
|
|
|
|
if (request.method === 'urlChanged') {
|
|
|
|
request.method = 'styleReplaceAll';
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +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-11-07 06:09:29 +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':
|
|
|
|
if (request.codeIsUpdated === false) {
|
|
|
|
applyStyleState(request.style);
|
2018-11-07 06:09:29 +00:00
|
|
|
} else if (request.style.enabled) {
|
|
|
|
API.getSectionsByUrl(getMatchUrl(), request.style.id)
|
|
|
|
.then(sections => {
|
|
|
|
if (!sections[request.style.id]) {
|
|
|
|
removeStyle(request.style);
|
|
|
|
} else {
|
|
|
|
applyStyles(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-11-07 06:09:29 +00:00
|
|
|
API.getSectionsByUrl(getMatchUrl(), request.style.id)
|
|
|
|
.then(applyStyles);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
case 'urlChanged':
|
|
|
|
API.getSectionsByUrl(getMatchUrl())
|
|
|
|
.then(replaceAll);
|
2017-11-25 21:56:57 +00:00
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
case 'backgroundReady':
|
|
|
|
initializing
|
|
|
|
.catch(err => {
|
|
|
|
if (msg.RX_NO_RECEIVER.test(err.message)) {
|
|
|
|
return init();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.error);
|
2017-11-25 21:56:57 +00:00
|
|
|
break;
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
case 'updateCount':
|
|
|
|
updateCount();
|
2017-11-25 21:56:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
|
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-11-07 06:09:29 +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-11-07 06:09:29 +00:00
|
|
|
function fetchParentDomain() {
|
|
|
|
if (parentDomain) {
|
|
|
|
return Promise.resolve();
|
2017-04-21 08:59:01 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
return API.getTabUrlPrefix()
|
|
|
|
.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-11-07 06:09:29 +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-11-07 06:09:29 +00:00
|
|
|
function updateCount() {
|
|
|
|
if (window !== parent) {
|
|
|
|
// we don't care about iframes
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (/^\w+?-extension:\/\/.+(popup|options)\.html$/.test(location.href)) {
|
|
|
|
// popup and the option page are not tabs
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (STYLE_VIA_API) {
|
|
|
|
API.styleViaAPI({method: 'updateCount'}).catch(msg.ignoreError);
|
|
|
|
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]
|
|
|
|
}).catch(msg.ignoreError);
|
|
|
|
}
|
|
|
|
|
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-11-07 06:09:29 +00:00
|
|
|
return API.getSectionsByUrl(getMatchUrl(), id)
|
|
|
|
.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-11-07 06:09:29 +00:00
|
|
|
updateCount();
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-07-12 20:02:17 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function removeStyle({id}) {
|
2017-11-25 21:56:57 +00:00
|
|
|
const el = document.getElementById(ID_PREFIX + id);
|
|
|
|
if (el) {
|
2018-11-07 06:09:29 +00:00
|
|
|
docRootObserver.evade(() => el.remove());
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
|
|
|
disabledElements.delete(id);
|
2018-11-07 06:09:29 +00:00
|
|
|
if (styleElements.delete(id)) {
|
|
|
|
updateCount();
|
|
|
|
}
|
2017-04-28 23:36:10 +00:00
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function applyStyles(sections, done) {
|
|
|
|
if (!Object.keys(sections).length) {
|
|
|
|
if (done) {
|
|
|
|
done();
|
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-09 18:29:49 +00:00
|
|
|
|
|
|
|
if (!document.documentElement) {
|
|
|
|
new MutationObserver((mutations, observer) => {
|
|
|
|
if (document.documentElement) {
|
|
|
|
observer.disconnect();
|
2018-11-07 06:09:29 +00:00
|
|
|
applyStyles(sections, done);
|
2017-12-09 18:29:49 +00:00
|
|
|
}
|
|
|
|
}).observe(document, {childList: true});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
if (docRootObserver) {
|
|
|
|
docRootObserver.stop();
|
|
|
|
} else {
|
|
|
|
initDocRootObserver();
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
const pending = [];
|
|
|
|
for (const section of Object.values(sections)) {
|
|
|
|
pending.push(applySections(section.id, section.code.join('')));
|
2018-09-05 12:58:15 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
docRootObserver.firstStart();
|
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-11-07 06:09:29 +00:00
|
|
|
updateExposeIframes();
|
|
|
|
updateCount();
|
|
|
|
if (done) {
|
|
|
|
Promise.all(pending).then(done);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2017-11-21 06:45:44 +00:00
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
|
2018-11-07 06:09:29 +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-11-07 06:09:29 +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-11-07 06:09:29 +00:00
|
|
|
let settingStyle;
|
|
|
|
if (el.textContent !== code) {
|
|
|
|
settingStyle = setStyleContent(el, code);
|
2018-09-05 12:58:15 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
styleElements.set(id, el);
|
|
|
|
disabledElements.delete(id);
|
|
|
|
return Promise.resolve(settingStyle);
|
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;
|
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
const insert = () => {
|
2017-12-09 15:41:05 +00:00
|
|
|
ROOT.insertBefore(newElement, next || null);
|
|
|
|
if (disableAll) {
|
|
|
|
newElement.disabled = true;
|
|
|
|
}
|
2018-11-07 06:09:29 +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-11-07 06:09:29 +00:00
|
|
|
const removeOld = () => oldStyles.forEach(el => el.remove());
|
|
|
|
if (docRewriteObserver) {
|
|
|
|
docRootObserver.evade(removeOld);
|
|
|
|
} else {
|
|
|
|
removeOld();
|
|
|
|
}
|
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-11-07 06:09:29 +00:00
|
|
|
${docId}.${CSS.escape(className)}:root * {
|
|
|
|
transition: none !important;
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
.then(() => {
|
|
|
|
// 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-11-07 06:09:29 +00:00
|
|
|
function orphanCheck(e) {
|
|
|
|
if (e && e.detail.method !== 'orphan') {
|
|
|
|
return;
|
2017-12-09 18:16:41 +00:00
|
|
|
}
|
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-11-07 06:09:29 +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
|
|
|
})();
|