2017-03-26 02:30:59 +00:00
|
|
|
// Not using some slow features of ES6, see http://kpdecker.github.io/six-speed/
|
|
|
|
// like destructring, classes, defaults, spread, calculated key names
|
|
|
|
/* eslint no-var: 0 */
|
|
|
|
'use strict';
|
2017-03-15 13:11:33 +00:00
|
|
|
|
2017-03-28 09:54:22 +00:00
|
|
|
var isOwnPage = location.href.startsWith('chrome-extension:');
|
2017-03-26 02:30:59 +00:00
|
|
|
var disableAll = false;
|
|
|
|
var styleElements = new Map();
|
2017-04-01 12:21:31 +00:00
|
|
|
var disabledElements = new Map();
|
2015-07-12 20:02:17 +00:00
|
|
|
var retiredStyleIds = [];
|
2015-04-30 12:32:19 +00:00
|
|
|
|
2015-05-06 16:53:41 +00:00
|
|
|
requestStyles();
|
2017-03-26 02:30:59 +00:00
|
|
|
chrome.runtime.onMessage.addListener(applyOnMessage);
|
|
|
|
|
2017-04-04 01:14:28 +00:00
|
|
|
if (!isOwnPage) {
|
|
|
|
window.dispatchEvent(new CustomEvent(chrome.runtime.id));
|
|
|
|
window.addEventListener(chrome.runtime.id, orphanCheck, true);
|
|
|
|
}
|
2015-05-06 16:53:41 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
function requestStyles(options) {
|
|
|
|
// If this is a Stylish page (Edit Style or Manage Styles),
|
|
|
|
// we'll request the styles directly to minimize delay and flicker,
|
2017-03-28 09:54:22 +00:00
|
|
|
// unless Chrome is still starting up and the background page isn't fully loaded.
|
2017-03-26 02:30:59 +00:00
|
|
|
// (Note: in this case the function may be invoked again from applyStyles.)
|
|
|
|
const request = Object.assign({
|
|
|
|
method: 'getStyles',
|
|
|
|
matchUrl: location.href,
|
|
|
|
enabled: true,
|
|
|
|
asHash: true,
|
|
|
|
}, options);
|
|
|
|
if (typeof getStylesSafe !== 'undefined') {
|
|
|
|
getStylesSafe(request).then(applyStyles);
|
|
|
|
} else {
|
|
|
|
chrome.runtime.sendMessage(request, applyStyles);
|
|
|
|
}
|
2015-03-31 21:35:06 +00:00
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
|
2017-03-14 12:18:58 +00:00
|
|
|
|
|
|
|
function applyOnMessage(request, sender, sendResponse) {
|
2017-03-27 09:11:29 +00:00
|
|
|
// Do-It-Yourself tells our built-in pages to fetch the styles directly
|
|
|
|
// which is faster because IPC messaging JSON-ifies everything internally
|
|
|
|
if (request.styles == 'DIY') {
|
|
|
|
getStylesSafe({
|
|
|
|
matchUrl: location.href,
|
|
|
|
enabled: true,
|
|
|
|
asHash: true,
|
|
|
|
}).then(styles =>
|
|
|
|
applyOnMessage(Object.assign(request, {styles})));
|
|
|
|
return;
|
|
|
|
}
|
2017-03-31 08:46:18 +00:00
|
|
|
switch (request.method) {
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
case 'styleDeleted':
|
|
|
|
removeStyle(request.id, document);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'styleUpdated':
|
|
|
|
if (request.codeIsUpdated === false) {
|
|
|
|
applyStyleState(request.style.id, request.style.enabled, document);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!request.style.enabled) {
|
|
|
|
removeStyle(request.style.id, document);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
retireStyle(request.style.id);
|
|
|
|
// fallthrough to 'styleAdded'
|
|
|
|
|
|
|
|
case 'styleAdded':
|
|
|
|
if (request.style.enabled) {
|
2017-04-12 06:15:57 +00:00
|
|
|
requestStyles({id: request.style.id});
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'styleApply':
|
|
|
|
applyStyles(request.styles);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'styleReplaceAll':
|
|
|
|
replaceAll(request.styles, document);
|
|
|
|
break;
|
|
|
|
|
2017-03-31 08:46:18 +00:00
|
|
|
case 'prefChanged':
|
|
|
|
if ('disableAll' in request.prefs) {
|
|
|
|
doDisableAll(request.prefs.disableAll);
|
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ping':
|
|
|
|
sendResponse(true);
|
|
|
|
break;
|
|
|
|
}
|
2017-03-14 12:18:58 +00:00
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-04-12 06:15:57 +00:00
|
|
|
function doDisableAll(disable, doc = document) {
|
|
|
|
if (doc == document && !disable === !disableAll) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
disableAll = disable;
|
2017-04-12 06:15:57 +00:00
|
|
|
if (disable && doc.iframeObserver) {
|
|
|
|
doc.iframeObserver.stop();
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-04-12 06:15:57 +00:00
|
|
|
Array.prototype.forEach.call(doc.styleSheets, stylesheet => {
|
|
|
|
if (stylesheet.ownerNode.matches('stylus[id^="stylus-"]')
|
|
|
|
&& stylesheet.disabled != disable) {
|
|
|
|
stylesheet.disabled = disable;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-04-12 06:15:57 +00:00
|
|
|
});
|
|
|
|
for (const iframe of getDynamicIFrames(doc)) {
|
|
|
|
if (!disable) {
|
|
|
|
// update the IFRAME if it was created while the observer was disconnected
|
|
|
|
addDocumentStylesToIFrame(iframe);
|
|
|
|
}
|
|
|
|
doDisableAll(disable, iframe.contentDocument);
|
|
|
|
}
|
|
|
|
if (!disable && doc.readyState != 'loading' && doc.iframeObserver) {
|
|
|
|
doc.iframeObserver.start();
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-03-17 18:03:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
function applyStyleState(id, enabled, doc) {
|
2017-04-01 12:21:31 +00:00
|
|
|
const inCache = disabledElements.get(id);
|
|
|
|
const inDoc = doc.getElementById('stylus-' + id);
|
|
|
|
if (enabled && inDoc || !enabled && !inDoc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (enabled && !inDoc && !inCache) {
|
2017-03-26 02:30:59 +00:00
|
|
|
requestStyles({id});
|
2017-04-01 12:21:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (enabled && inCache) {
|
|
|
|
const el = inCache.cloneNode(true);
|
2017-04-12 06:15:57 +00:00
|
|
|
doc.documentElement.appendChild(el);
|
2017-04-01 12:21:31 +00:00
|
|
|
el.sheet.disabled = disableAll;
|
|
|
|
processDynamicIFrames(doc, applyStyleState, id, enabled);
|
|
|
|
disabledElements.delete(id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!enabled && inDoc) {
|
2017-04-12 06:15:57 +00:00
|
|
|
if (!inCache) {
|
|
|
|
disabledElements.set(id, inDoc);
|
|
|
|
}
|
2017-04-01 12:21:31 +00:00
|
|
|
inDoc.remove();
|
2017-04-12 06:15:57 +00:00
|
|
|
if (doc.location.href == 'about:srcdoc') {
|
|
|
|
const original = doc.getElementById('stylus-' + id);
|
|
|
|
if (original) {
|
|
|
|
original.remove();
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 12:21:31 +00:00
|
|
|
processDynamicIFrames(doc, applyStyleState, id, enabled);
|
|
|
|
return;
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
Improve style caching, cache requests too, add code:false mode
Previously, when a cache was invalidated and every tab/iframe issued a getStyles request, we previous needlessly accessed IndexedDB for each of these requests. It happened because 1) the global cachedStyles was created only at the end of the async DB-reading, 2) and each style record is retrieved asynchronously so the single threaded JS engine interleaved all these operations. It could easily span a few seconds when many tabs are open and you have like 100 styles.
Now, in getStyles: all requests issued while cachedStyles is being populated are queued and invoked at the end.
Now, in filterStyles: all requests are cached using the request's options combined in a string as a key. It also helps on each navigation because we monitor page loading process at different stages: before, when committed, history traversal, requesting applicable styles by a content script. Icon badge update also may issue a copy of the just issued request by one of the navigation listeners.
Now, the caches are invalidated smartly: style add/update/delete/toggle only purges filtering cache, and modifies style cache in-place without re-reading the entire IndexedDB.
Now, code:false mode for manage page that only needs style meta. It reduces the transferred message size 10-100 times thus reducing the overhead caused by to internal JSON-fication in the extensions API.
Also fast&direct getStylesSafe for own pages; code cosmetics
2017-03-17 22:50:35 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2015-03-03 16:48:29 +00:00
|
|
|
function removeStyle(id, doc) {
|
2017-03-28 09:54:22 +00:00
|
|
|
[doc.getElementById('stylus-' + id)].forEach(e => e && e.remove());
|
2017-04-01 12:21:31 +00:00
|
|
|
if (doc == document) {
|
|
|
|
styleElements.delete('stylus-' + id);
|
|
|
|
disabledElements.delete(id);
|
|
|
|
if (!styleElements.size) {
|
2017-04-12 06:15:57 +00:00
|
|
|
doc.iframeObserver.disconnect();
|
2017-04-01 12:21:31 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
processDynamicIFrames(doc, removeStyle, id);
|
2015-01-30 03:32:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2015-07-12 20:02:17 +00:00
|
|
|
// 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
|
|
|
|
function retireStyle(id, doc) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const deadID = 'ghost-' + id;
|
|
|
|
if (!doc) {
|
|
|
|
doc = document;
|
|
|
|
retiredStyleIds.push(deadID);
|
|
|
|
styleElements.delete('stylus-' + id);
|
2017-04-01 12:21:31 +00:00
|
|
|
disabledElements.delete(id);
|
2017-03-26 02:30:59 +00:00
|
|
|
// in case something went wrong and new style was never applied
|
|
|
|
setTimeout(removeStyle, 1000, deadID, doc);
|
|
|
|
}
|
|
|
|
const el = doc.getElementById('stylus-' + id);
|
|
|
|
if (el) {
|
|
|
|
el.id = 'stylus-' + deadID;
|
|
|
|
}
|
|
|
|
processDynamicIFrames(doc, retireStyle, id);
|
2015-07-12 20:02:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2015-01-30 16:36:46 +00:00
|
|
|
function applyStyles(styleHash) {
|
2017-03-26 02:30:59 +00:00
|
|
|
if (!styleHash) { // Chrome is starting up
|
|
|
|
requestStyles();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ('disableAll' in styleHash) {
|
|
|
|
doDisableAll(styleHash.disableAll);
|
|
|
|
delete styleHash.disableAll;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const styleId in styleHash) {
|
|
|
|
applySections(styleId, styleHash[styleId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (styleElements.size) {
|
|
|
|
// when site response is application/xml Chrome displays our style elements
|
|
|
|
// under document.documentElement as plain text so we need to move them into HEAD
|
|
|
|
// which is already autogenerated at this moment
|
|
|
|
if (document.head && document.head.firstChild && document.head.firstChild.id == 'xml-viewer-style') {
|
|
|
|
for (const id of styleElements.keys()) {
|
|
|
|
document.head.appendChild(document.getElementById(id));
|
|
|
|
}
|
|
|
|
}
|
2017-04-12 06:15:57 +00:00
|
|
|
initObservers();
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (retiredStyleIds.length) {
|
|
|
|
setTimeout(function() {
|
|
|
|
while (retiredStyleIds.length) {
|
|
|
|
removeStyle(retiredStyleIds.shift(), document);
|
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2015-01-30 16:36:46 +00:00
|
|
|
function applySections(styleId, sections) {
|
2017-03-26 02:30:59 +00:00
|
|
|
let el = document.getElementById('stylus-' + styleId);
|
|
|
|
// Already there.
|
|
|
|
if (el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (document.documentElement instanceof SVGSVGElement) {
|
|
|
|
// SVG document, make an SVG style element.
|
|
|
|
el = document.createElementNS('http://www.w3.org/2000/svg', 'style');
|
2017-04-06 13:02:38 +00:00
|
|
|
} else if (document instanceof XMLDocument) {
|
|
|
|
el = document.createElementNS('http://www.w3.org/1999/xhtml', 'style');
|
2017-03-26 02:30:59 +00:00
|
|
|
} else {
|
|
|
|
// This will make an HTML style element. If there's SVG embedded in an HTML document, this works on the SVG too.
|
|
|
|
el = document.createElement('style');
|
|
|
|
}
|
|
|
|
el.setAttribute('id', 'stylus-' + styleId);
|
|
|
|
el.setAttribute('class', 'stylus');
|
|
|
|
el.setAttribute('type', 'text/css');
|
|
|
|
el.appendChild(document.createTextNode(sections.map(section => section.code).join('\n')));
|
|
|
|
addStyleElement(el, document);
|
|
|
|
styleElements.set(el.id, el);
|
2017-04-01 12:21:31 +00:00
|
|
|
disabledElements.delete(styleId);
|
2015-03-03 16:48:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
function addStyleElement(el, doc) {
|
|
|
|
if (!doc.documentElement || doc.getElementById(el.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
doc.documentElement.appendChild(doc.importNode(el, true))
|
|
|
|
.disabled = disableAll;
|
|
|
|
for (const iframe of getDynamicIFrames(doc)) {
|
|
|
|
if (iframeIsLoadingSrcDoc(iframe)) {
|
|
|
|
addStyleToIFrameSrcDoc(iframe, el);
|
|
|
|
} else {
|
|
|
|
addStyleElement(el, iframe.contentDocument);
|
|
|
|
}
|
|
|
|
}
|
2015-01-30 03:32:14 +00:00
|
|
|
}
|
2015-02-23 05:14:22 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2015-04-30 12:32:19 +00:00
|
|
|
function addDocumentStylesToIFrame(iframe) {
|
2017-03-26 02:30:59 +00:00
|
|
|
const doc = iframe.contentDocument;
|
|
|
|
const srcDocIsLoading = iframeIsLoadingSrcDoc(iframe);
|
|
|
|
for (const el of styleElements.values()) {
|
|
|
|
if (srcDocIsLoading) {
|
|
|
|
addStyleToIFrameSrcDoc(iframe, el);
|
|
|
|
} else {
|
|
|
|
addStyleElement(el, doc);
|
|
|
|
}
|
|
|
|
}
|
2017-04-12 06:15:57 +00:00
|
|
|
initObservers(doc);
|
2015-04-30 12:32:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-04-12 06:15:57 +00:00
|
|
|
function addDocumentStylesToAllIFrames(doc = document) {
|
|
|
|
getDynamicIFrames(doc).forEach(addDocumentStylesToIFrame);
|
2015-12-24 21:39:50 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 09:54:22 +00:00
|
|
|
|
2015-03-03 16:48:29 +00:00
|
|
|
// Only dynamic iframes get the parent document's styles. Other ones should get styles based on their own URLs.
|
|
|
|
function getDynamicIFrames(doc) {
|
2017-04-12 06:15:57 +00:00
|
|
|
return Array.prototype.filter.call(doc.getElementsByTagName('iframe'), iframeIsDynamic);
|
2015-03-03 16:48:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2015-03-03 16:48:29 +00:00
|
|
|
function iframeIsDynamic(f) {
|
2017-03-26 02:30:59 +00:00
|
|
|
let href;
|
|
|
|
if (f.src && f.src.startsWith('http') && new URL(f.src).origin != location.origin) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
href = f.contentDocument.location.href;
|
|
|
|
} catch (ex) {
|
|
|
|
// Cross-origin, so it's not a dynamic iframe
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return href == document.location.href || href.startsWith('about:');
|
2015-03-03 16:48:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
function processDynamicIFrames(doc, fn, ...args) {
|
2017-04-12 06:15:57 +00:00
|
|
|
var iframes = doc.getElementsByTagName('iframe');
|
|
|
|
for (var i = 0, il = iframes.length; i < il; i++) {
|
|
|
|
var iframe = iframes[i];
|
2017-03-26 02:30:59 +00:00
|
|
|
if (iframeIsDynamic(iframe)) {
|
|
|
|
fn(...args, iframe.contentDocument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-30 12:32:19 +00:00
|
|
|
function iframeIsLoadingSrcDoc(f) {
|
2017-03-26 02:30:59 +00:00
|
|
|
return f.srcdoc && f.contentDocument.all.length <= 3;
|
|
|
|
// 3 nodes or less in total (html, head, body) == new empty iframe about to be overwritten by its 'srcdoc'
|
2015-04-30 12:32:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
function addStyleToIFrameSrcDoc(iframe, el) {
|
|
|
|
if (disableAll) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
iframe.srcdoc += el.outerHTML;
|
|
|
|
// make sure the style is added in case srcdoc was malformed
|
|
|
|
setTimeout(addStyleElement, 100, el, iframe.contentDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function replaceAll(newStyles, doc) {
|
2017-04-12 06:15:57 +00:00
|
|
|
Array.prototype.forEach.call(doc.querySelectorAll('STYLE.stylus[id^="stylus-"]'),
|
|
|
|
e => (e.id += '-ghost'));
|
2017-03-26 02:30:59 +00:00
|
|
|
processDynamicIFrames(doc, replaceAll, newStyles);
|
|
|
|
if (doc == document) {
|
|
|
|
styleElements.clear();
|
2017-04-01 12:21:31 +00:00
|
|
|
disabledElements.clear();
|
2017-03-26 02:30:59 +00:00
|
|
|
applyStyles(newStyles);
|
|
|
|
replaceAllpass2(newStyles, doc);
|
|
|
|
}
|
2015-04-30 12:32:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
function replaceAllpass2(newStyles, doc) {
|
2017-04-12 06:15:57 +00:00
|
|
|
const oldStyles = doc.querySelectorAll('STYLE.stylus[id$="-ghost"]');
|
2017-03-26 02:30:59 +00:00
|
|
|
processDynamicIFrames(doc, replaceAllpass2, newStyles);
|
2017-04-12 06:15:57 +00:00
|
|
|
Array.prototype.forEach.call(oldStyles,
|
|
|
|
e => e.remove());
|
2015-02-23 05:14:22 +00:00
|
|
|
}
|
2015-03-03 16:48:29 +00:00
|
|
|
|
2017-03-26 02:30:59 +00:00
|
|
|
|
2017-04-12 06:15:57 +00:00
|
|
|
function onDOMContentLoaded({target = document} = {}) {
|
|
|
|
addDocumentStylesToAllIFrames(target);
|
|
|
|
if (target.iframeObserver) {
|
|
|
|
target.iframeObserver.start();
|
|
|
|
}
|
|
|
|
}
|
2017-03-28 04:08:37 +00:00
|
|
|
|
2017-04-12 06:15:57 +00:00
|
|
|
|
|
|
|
function initObservers(doc = document) {
|
|
|
|
if (isOwnPage || doc.rewriteObserver) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
initIFrameObserver(doc);
|
|
|
|
initDocRewriteObserver(doc);
|
|
|
|
if (doc.readyState != 'loading') {
|
|
|
|
onDOMContentLoaded({target: doc});
|
|
|
|
} else {
|
|
|
|
doc.addEventListener('DOMContentLoaded', onDOMContentLoaded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function initIFrameObserver(doc = document) {
|
|
|
|
if (!initIFrameObserver.methods) {
|
|
|
|
initIFrameObserver.methods = {
|
|
|
|
start() {
|
|
|
|
this.observe(this.doc, {childList: true, subtree: true});
|
|
|
|
},
|
|
|
|
stop() {
|
|
|
|
this.disconnect();
|
|
|
|
getDynamicIFrames(this.doc).forEach(iframe => {
|
|
|
|
const observer = iframe.contentDocument.iframeObserver;
|
|
|
|
if (observer) {
|
|
|
|
observer.stop();
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2017-04-12 06:15:57 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
doc.iframeObserver = Object.assign(
|
|
|
|
new MutationObserver(iframeObserver),
|
|
|
|
initIFrameObserver.methods, {
|
|
|
|
iframes: doc.getElementsByTagName('iframe'),
|
|
|
|
doc,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function iframeObserver(mutations, observer) {
|
|
|
|
// autoupdated HTMLCollection is superfast
|
|
|
|
if (!observer.iframes[0]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// use a much faster method for very complex pages with lots of mutations
|
|
|
|
// (observer usually receives 1k-10k mutations per call)
|
|
|
|
if (mutations.length > 1000) {
|
|
|
|
addDocumentStylesToAllIFrames(observer.doc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (var m = 0, ml = mutations.length; m < ml; m++) {
|
|
|
|
var added = mutations[m].addedNodes;
|
|
|
|
for (var n = 0, nl = added.length; n < nl; n++) {
|
|
|
|
var node = added[n];
|
|
|
|
// process only ELEMENT_NODE
|
|
|
|
if (node.nodeType != 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var iframes = node.localName === 'iframe' ? [node] :
|
|
|
|
node.children.length && node.getElementsByTagName('iframe');
|
|
|
|
if (iframes.length) {
|
|
|
|
// move the check out of current execution context
|
|
|
|
// because some same-domain (!) iframes fail to load when their 'contentDocument' is accessed (!)
|
|
|
|
// namely gmail's old chat iframe talkgadget.google.com
|
|
|
|
setTimeout(testIFrames, 0, iframes);
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-28 04:08:37 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
|
|
|
|
|
2017-04-12 06:15:57 +00:00
|
|
|
function testIFrames(iframes) {
|
|
|
|
for (const iframe of iframes) {
|
|
|
|
if (iframeIsDynamic(iframe)) {
|
|
|
|
addDocumentStylesToIFrame(iframe);
|
|
|
|
}
|
2017-03-28 09:54:22 +00:00
|
|
|
}
|
2017-04-12 06:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function initDocRewriteObserver(doc = document) {
|
2017-03-28 09:54:22 +00:00
|
|
|
// re-add styles if we detect documentElement being recreated
|
2017-04-12 06:15:57 +00:00
|
|
|
doc.rewriteObserver = new MutationObserver(docRewriteObserver);
|
|
|
|
doc.rewriteObserver.observe(doc, {childList: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function docRewriteObserver(mutations) {
|
|
|
|
for (const mutation of mutations) {
|
|
|
|
for (const node of mutation.addedNodes) {
|
|
|
|
if (node.localName != 'html') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const doc = node.ownerDocument;
|
|
|
|
for (const [id, el] of styleElements.entries()) {
|
|
|
|
if (!doc.getElementById(id)) {
|
|
|
|
doc.documentElement.appendChild(el);
|
2017-03-28 04:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-12 06:15:57 +00:00
|
|
|
initObservers(doc);
|
|
|
|
return;
|
2017-03-28 04:08:37 +00:00
|
|
|
}
|
2017-03-26 02:30:59 +00:00
|
|
|
}
|
2015-04-30 12:32:19 +00:00
|
|
|
}
|
2017-03-28 04:08:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
function orphanCheck() {
|
|
|
|
const port = chrome.runtime.connect();
|
|
|
|
if (port) {
|
|
|
|
port.disconnect();
|
2017-04-04 01:14:28 +00:00
|
|
|
//console.debug('orphanCheck: still connected');
|
2017-03-28 04:08:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-04 01:14:28 +00:00
|
|
|
//console.debug('orphanCheck: disconnected');
|
2017-03-28 04:08:37 +00:00
|
|
|
|
|
|
|
// we're orphaned due to an extension update
|
|
|
|
// we can detach the mutation observer
|
|
|
|
// we can detach event listeners
|
2017-04-12 06:15:57 +00:00
|
|
|
(function unbind(doc) {
|
|
|
|
if (doc.iframeObserver) {
|
|
|
|
doc.iframeObserver.disconnect();
|
|
|
|
delete doc.iframeObserver;
|
|
|
|
}
|
|
|
|
if (doc.rewriteObserver) {
|
|
|
|
doc.rewriteObserver.disconnect();
|
|
|
|
delete doc.rewriteObserver;
|
|
|
|
}
|
|
|
|
doc.removeEventListener('DOMContentLoaded', onDOMContentLoaded);
|
|
|
|
getDynamicIFrames(doc).forEach(iframe => unbind(iframe.contentDocument));
|
|
|
|
})(document);
|
2017-04-04 01:14:28 +00:00
|
|
|
window.removeEventListener(chrome.runtime.id, orphanCheck, true);
|
2017-03-28 04:08:37 +00:00
|
|
|
// we can't detach chrome.runtime.onMessage because it's no longer connected internally
|
|
|
|
// we can destroy our globals in this context to free up memory
|
|
|
|
[ // functions
|
|
|
|
'addDocumentStylesToAllIFrames',
|
|
|
|
'addDocumentStylesToIFrame',
|
|
|
|
'addStyleElement',
|
|
|
|
'addStyleToIFrameSrcDoc',
|
|
|
|
'applyOnMessage',
|
|
|
|
'applySections',
|
|
|
|
'applyStyles',
|
|
|
|
'doDisableAll',
|
|
|
|
'getDynamicIFrames',
|
|
|
|
'iframeIsDynamic',
|
|
|
|
'iframeIsLoadingSrcDoc',
|
2017-03-28 09:54:22 +00:00
|
|
|
'initDocRewriteObserver',
|
|
|
|
'initIFrameObserver',
|
2017-03-28 04:08:37 +00:00
|
|
|
'orphanCheck',
|
2017-03-28 09:54:22 +00:00
|
|
|
'processDynamicIFrames',
|
2017-03-28 04:08:37 +00:00
|
|
|
'removeStyle',
|
|
|
|
'replaceAll',
|
|
|
|
'replaceAllpass2',
|
|
|
|
'requestStyles',
|
|
|
|
'retireStyle',
|
|
|
|
'styleObserver',
|
|
|
|
// variables
|
2017-03-28 09:54:22 +00:00
|
|
|
'docRewriteObserver',
|
2017-03-28 04:08:37 +00:00
|
|
|
'iframeObserver',
|
|
|
|
'retiredStyleIds',
|
2017-03-28 09:54:22 +00:00
|
|
|
'styleElements',
|
2017-03-28 04:08:37 +00:00
|
|
|
].forEach(fn => (window[fn] = null));
|
|
|
|
}
|