2017-03-26 02:30:59 +00:00
|
|
|
/* eslint no-var: 0 */
|
2019-03-10 02:58:17 +00:00
|
|
|
/* global msg API prefs createStyleInjector */
|
2018-11-07 06:09:29 +00:00
|
|
|
/* 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;
|
2019-03-10 02:58:17 +00:00
|
|
|
const IS_OWN_PAGE = location.protocol.endsWith('-extension:');
|
2018-11-07 06:09:29 +00:00
|
|
|
const setStyleContent = createSetStyleContent();
|
2019-03-10 02:58:17 +00:00
|
|
|
const styleInjector = createStyleInjector({
|
|
|
|
compare: (a, b) => a.id - b.id,
|
|
|
|
setStyleContent,
|
|
|
|
onUpdate: onInjectorUpdate
|
|
|
|
});
|
|
|
|
const docRootObserver = createDocRootObserver({
|
|
|
|
onChange: () => {
|
|
|
|
if (styleInjector.outOfOrder()) {
|
|
|
|
styleInjector.sort();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const docRewriteObserver = createDocRewriteObserver({
|
|
|
|
onChange: () => {
|
|
|
|
docRootObserver.evade(styleInjector.sort);
|
|
|
|
}
|
|
|
|
});
|
2018-11-07 06:09:29 +00:00
|
|
|
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
|
|
|
|
2019-03-10 02:58:17 +00:00
|
|
|
if (!IS_OWN_PAGE) {
|
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);
|
|
|
|
}
|
|
|
|
|
2019-03-10 02:58:17 +00:00
|
|
|
function onInjectorUpdate() {
|
|
|
|
if (!IS_OWN_PAGE && styleInjector.list.length) {
|
|
|
|
docRewriteObserver.start();
|
|
|
|
docRootObserver.start();
|
|
|
|
} else {
|
|
|
|
docRewriteObserver.stop();
|
|
|
|
docRootObserver.stop();
|
|
|
|
}
|
|
|
|
updateCount();
|
|
|
|
updateExposeIframes();
|
|
|
|
}
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
function init() {
|
|
|
|
if (STYLE_VIA_API) {
|
|
|
|
return API.styleViaAPI({method: 'styleApply'});
|
|
|
|
}
|
|
|
|
return API.getSectionsByUrl(getMatchUrl())
|
2019-03-10 02:58:17 +00:00
|
|
|
.then(result =>
|
|
|
|
applyStyles(result)
|
|
|
|
.then(() => {
|
|
|
|
// CSS transition bug workaround: since we insert styles asynchronously,
|
|
|
|
// the browsers, especially Firefox, may apply all transitions on page load
|
|
|
|
if (styleInjector.list.some(s => s.code.includes('transition'))) {
|
|
|
|
applyTransitionPatch();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function pageObject(target) {
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
|
|
|
|
const obj = new window.Object();
|
|
|
|
Object.assign(obj, target);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-09-09 04:45:08 +00:00
|
|
|
let ready;
|
2019-06-01 09:10:44 +00:00
|
|
|
return (el, content, disabled) =>
|
2019-09-09 04:45:08 +00:00
|
|
|
checkPageScript().then(ok => {
|
2018-11-07 06:09:29 +00:00
|
|
|
if (!ok) {
|
|
|
|
el.textContent = content;
|
2019-06-01 09:10:44 +00:00
|
|
|
// https://github.com/openstyles/stylus/issues/693
|
2018-11-07 06:09:29 +00:00
|
|
|
el.disabled = disabled;
|
|
|
|
} else {
|
|
|
|
const detail = pageObject({
|
|
|
|
method: 'setStyleContent',
|
|
|
|
id: el.id,
|
2019-06-01 09:10:44 +00:00
|
|
|
content,
|
|
|
|
disabled
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
|
|
|
window.dispatchEvent(new CustomEvent(EVENT_NAME, {detail}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-09 04:45:08 +00:00
|
|
|
function checkPageScript() {
|
|
|
|
if (!ready) {
|
|
|
|
ready = CHROME || IS_OWN_PAGE || 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) {
|
2019-06-01 09:10:44 +00:00
|
|
|
const {method, id, content, disabled} = e.detail;
|
2018-11-21 17:09:48 +00:00
|
|
|
if (method === 'setStyleContent') {
|
|
|
|
const el = document.getElementById(id);
|
|
|
|
if (!el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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');
|
|
|
|
document.documentElement.appendChild(style);
|
2019-09-11 07:32:34 +00:00
|
|
|
const applied = Boolean(style.sheet);
|
2018-11-21 17:09:48 +00:00
|
|
|
style.remove();
|
2019-09-11 07:32:34 +00:00
|
|
|
return applied;
|
2018-11-21 17:09:48 +00:00
|
|
|
}
|
2018-11-07 06:09:29 +00:00
|
|
|
};
|
|
|
|
const code = `(${scriptContent})(${JSON.stringify(EVENT_NAME)})`;
|
2019-09-09 04:45:08 +00:00
|
|
|
// make sure it works in XML
|
|
|
|
const script = document.createElementNS('http://www.w3.org/1999/xhtml', 'script');
|
2018-11-07 06:09:29 +00:00
|
|
|
const {resolve, promise} = deferred();
|
2019-09-09 04:45:08 +00:00
|
|
|
// use inline script because using src is too slow
|
|
|
|
// https://github.com/openstyles/stylus/pull/766
|
|
|
|
script.text = code;
|
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);
|
2019-09-09 04:45:08 +00:00
|
|
|
(document.head || document.documentElement).appendChild(script);
|
|
|
|
// injection failed if handleInit is not called.
|
|
|
|
resolveFalse();
|
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 (STYLE_VIA_API) {
|
|
|
|
if (request.method === 'urlChanged') {
|
|
|
|
request.method = 'styleReplaceAll';
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2020-02-02 04:36:54 +00:00
|
|
|
if (/^(style|updateCount)/.test(request.method)) {
|
|
|
|
API.styleViaAPI(request);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-13 12:28:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
switch (request.method) {
|
2020-02-02 04:36:54 +00:00
|
|
|
case 'ping':
|
|
|
|
return true;
|
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
case 'styleDeleted':
|
2019-03-10 02:58:17 +00:00
|
|
|
styleInjector.remove(request.style.id);
|
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':
|
2019-03-10 02:58:17 +00:00
|
|
|
if (request.style.enabled) {
|
2018-11-07 06:09:29 +00:00
|
|
|
API.getSectionsByUrl(getMatchUrl(), request.style.id)
|
|
|
|
.then(sections => {
|
|
|
|
if (!sections[request.style.id]) {
|
2019-03-10 02:58:17 +00:00
|
|
|
styleInjector.remove(request.style.id);
|
2018-11-07 06:09:29 +00:00
|
|
|
} else {
|
|
|
|
applyStyles(sections);
|
|
|
|
}
|
|
|
|
});
|
2017-11-25 21:56:57 +00:00
|
|
|
} else {
|
2019-03-10 02:58:17 +00:00
|
|
|
styleInjector.remove(request.style.id);
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
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
|
|
|
|
2019-03-10 02:58:17 +00:00
|
|
|
function doDisableAll(disableAll) {
|
2018-11-07 06:09:29 +00:00
|
|
|
if (STYLE_VIA_API) {
|
|
|
|
API.styleViaAPI({method: 'prefChanged', prefs: {disableAll}});
|
|
|
|
} else {
|
2019-03-10 02:58:17 +00:00
|
|
|
styleInjector.toggle(!disableAll);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-02-02 04:36:54 +00:00
|
|
|
return msg.send({
|
|
|
|
method: 'invokeAPI',
|
|
|
|
name: 'getTabUrlPrefix',
|
|
|
|
args: []
|
|
|
|
})
|
2018-11-07 06:09:29 +00:00
|
|
|
.then(newDomain => {
|
|
|
|
parentDomain = newDomain;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateExposeIframes() {
|
2019-03-10 02:58:17 +00:00
|
|
|
if (!prefs.get('exposeIframes') || window === parent || !styleInjector.list.length) {
|
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;
|
|
|
|
}
|
|
|
|
// we have to send the tabId so we can't use `sendBg` that is used by `API`
|
|
|
|
msg.send({
|
|
|
|
method: 'invokeAPI',
|
|
|
|
name: 'updateIconBadge',
|
2019-03-10 02:58:17 +00:00
|
|
|
args: [styleInjector.list.length]
|
2019-03-03 14:29:26 +00:00
|
|
|
}).catch(console.error);
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 02:58:17 +00:00
|
|
|
function rootReady() {
|
|
|
|
if (document.documentElement) {
|
|
|
|
return Promise.resolve();
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
return new Promise(resolve => {
|
2017-12-09 18:29:49 +00:00
|
|
|
new MutationObserver((mutations, observer) => {
|
|
|
|
if (document.documentElement) {
|
|
|
|
observer.disconnect();
|
2019-03-10 02:58:17 +00:00
|
|
|
resolve();
|
2017-12-09 18:29:49 +00:00
|
|
|
}
|
|
|
|
}).observe(document, {childList: true});
|
2019-03-10 02:58:17 +00:00
|
|
|
});
|
2018-08-24 18:12:22 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 02:58:17 +00:00
|
|
|
function applyStyles(sections) {
|
|
|
|
const styles = Object.values(sections);
|
|
|
|
if (!styles.length) {
|
|
|
|
return Promise.resolve();
|
2018-11-07 06:09:29 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
return rootReady().then(() =>
|
|
|
|
docRootObserver.evade(() =>
|
|
|
|
styleInjector.addMany(
|
|
|
|
styles.map(s => ({id: s.id, code: s.code.join('')}))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
|
|
|
function replaceAll(newStyles) {
|
2019-03-10 02:58:17 +00:00
|
|
|
styleInjector.replaceAll(
|
|
|
|
Object.values(newStyles)
|
|
|
|
.map(s => ({id: s.id, code: s.code.join('')}))
|
|
|
|
);
|
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
|
2019-03-10 02:58:17 +00:00
|
|
|
const el = styleInjector.createStyle('transition-patch');
|
|
|
|
// FIXME: this will trigger docRootObserver and cause a resort. We should
|
|
|
|
// move this function into style-injector.
|
|
|
|
document.documentElement.appendChild(el);
|
|
|
|
setStyleContent(el, `
|
2019-05-26 10:12:02 +00:00
|
|
|
:root:not(#\\0):not(#\\0) * {
|
2018-11-07 06:09:29 +00:00
|
|
|
transition: none !important;
|
|
|
|
}
|
|
|
|
`)
|
2019-05-26 10:12:02 +00:00
|
|
|
.then(afterPaint)
|
2019-01-30 04:30:37 +00:00
|
|
|
.then(() => {
|
2019-05-26 10:12:02 +00:00
|
|
|
el.remove();
|
2019-01-30 04:30:37 +00:00
|
|
|
});
|
2017-12-09 18:30:00 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
|
2019-05-26 10:12:02 +00:00
|
|
|
function afterPaint() {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
setTimeout(resolve);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
2019-03-10 02:58:17 +00:00
|
|
|
styleInjector.clear();
|
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
|
|
|
|
2019-03-10 02:58:17 +00:00
|
|
|
function createDocRewriteObserver({onChange}) {
|
2017-11-25 21:56:57 +00:00
|
|
|
// detect documentElement being rewritten from inside the script
|
2019-03-10 02:58:17 +00:00
|
|
|
let root;
|
2017-11-25 21:56:57 +00:00
|
|
|
let observing = false;
|
2019-03-10 02:58:17 +00:00
|
|
|
let timer;
|
|
|
|
const observer = new MutationObserver(check);
|
|
|
|
return {start, stop};
|
|
|
|
|
2018-08-06 14:35:06 +00:00
|
|
|
function start() {
|
2019-03-10 02:58:17 +00:00
|
|
|
if (observing) return;
|
|
|
|
// detect dynamic iframes rewritten after creation by the embedder i.e. externally
|
|
|
|
root = document.documentElement;
|
|
|
|
timer = setTimeout(check);
|
|
|
|
observer.observe(document, {childList: true});
|
|
|
|
observing = true;
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
|
2017-11-25 21:56:57 +00:00
|
|
|
function stop() {
|
2019-03-10 02:58:17 +00:00
|
|
|
if (!observing) return;
|
|
|
|
clearTimeout(timer);
|
|
|
|
observer.disconnect();
|
|
|
|
observing = false;
|
2017-12-09 15:41:05 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
|
|
|
|
function check() {
|
|
|
|
if (root !== document.documentElement) {
|
|
|
|
root = document.documentElement;
|
|
|
|
onChange();
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDocRootObserver({onChange}) {
|
|
|
|
let digest = 0;
|
|
|
|
let lastCalledTime = NaN;
|
|
|
|
let observing = false;
|
|
|
|
const observer = new MutationObserver(() => {
|
|
|
|
if (digest) {
|
|
|
|
if (performance.now() - lastCalledTime > 1000) {
|
|
|
|
digest = 0;
|
|
|
|
} else if (digest > 5) {
|
|
|
|
throw new Error('The page keeps generating mutations. Skip the event.');
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
if (onChange()) {
|
|
|
|
digest++;
|
|
|
|
lastCalledTime = performance.now();
|
2017-11-22 17:19:01 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
});
|
|
|
|
return {start, stop, evade};
|
|
|
|
|
|
|
|
function start() {
|
|
|
|
if (observing) return;
|
|
|
|
observer.observe(document.documentElement, {childList: true});
|
|
|
|
observing = true;
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
|
|
|
|
function stop() {
|
|
|
|
if (!observing) return;
|
|
|
|
// FIXME: do we need this?
|
|
|
|
observer.takeRecords();
|
|
|
|
observer.disconnect();
|
|
|
|
observing = false;
|
2017-11-21 06:45:44 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
|
|
|
|
function evade(fn) {
|
|
|
|
if (!observing) {
|
|
|
|
return fn();
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2019-03-10 02:58:17 +00:00
|
|
|
stop();
|
|
|
|
const r = fn();
|
|
|
|
start();
|
|
|
|
return r;
|
2017-11-25 21:56:57 +00:00
|
|
|
}
|
2017-11-21 06:45:44 +00:00
|
|
|
}
|
2017-11-25 21:56:57 +00:00
|
|
|
})();
|