From aa5fc9f640065e96b0df29ce295665d60364246f Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 20 Apr 2017 04:46:04 +0300 Subject: [PATCH] notify USO earlier in install.js by relaying xhr --- .eslintrc | 1 + background.js | 6 ++++++ install.js | 58 +++++++++++++++++++++++++++------------------------ manage.js | 23 ++------------------ messaging.js | 15 +++++++++++++ 5 files changed, 55 insertions(+), 48 deletions(-) diff --git a/.eslintrc b/.eslintrc index 2dfd33c9..b20ffc9d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -33,6 +33,7 @@ globals: getStylesSafe: false saveStyleSafe: false sessionStorageHash: false + download: false # localization.js template: false t: false diff --git a/background.js b/background.js index b4a7f552..88acd85d 100644 --- a/background.js +++ b/background.js @@ -86,6 +86,12 @@ function onRuntimeMessage(request, sender, sendResponse) { } } break; + + case 'download': + download(request.url) + .then(sendResponse) + .catch(() => sendResponse(null)); + return KEEP_CHANNEL_OPEN; } } diff --git a/install.js b/install.js index f78ca970..9927bb8a 100644 --- a/install.js +++ b/install.js @@ -18,19 +18,16 @@ function waitForBody() { if (!document.body) { return; } - this.disconnect(); + rebrand([{addedNodes: [document.body]}]); new MutationObserver(rebrand) .observe(document.body, {childList: true, subtree: true}); - document.addEventListener('DOMContentLoaded', function _() { - document.removeEventListener('DOMContentLoaded', _); - chrome.runtime.sendMessage({ - method: 'getStyles', - url: getMeta('stylish-id-url') || location.href - }, checkUpdatability); - }); + chrome.runtime.sendMessage({ + method: 'getStyles', + url: getMeta('stylish-id-url') || location.href + }, checkUpdatability); } @@ -71,7 +68,9 @@ function sendEvent(type, detail = null) { // because USO tries to use a global "event" variable deprecated in Firefox detail = cloneInto(detail, document); // eslint-disable-line no-undef } - document.dispatchEvent(new CustomEvent(type, detail)); + onDOMready().then(() => { + document.dispatchEvent(new CustomEvent(type, detail)); + }); } @@ -121,20 +120,11 @@ function getMeta(name) { function getResource(url) { - if (url.startsWith('#')) { - return Promise.resolve(document.getElementById(url.slice(1)).textContent); - } return new Promise(resolve => { - const xhr = new XMLHttpRequest(); - xhr.onloadend = () => resolve(xhr.status < 400 ? xhr.responseText : null); - if (url.length > 2000) { - const [mainUrl, query] = url.split('?'); - xhr.open('POST', mainUrl, true); - xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); - xhr.send(query); + if (url.startsWith('#')) { + resolve(document.getElementById(url.slice(1)).textContent); } else { - xhr.open('GET', url); - xhr.send(); + chrome.runtime.sendMessage({method: 'download', url}, resolve); } }); } @@ -148,17 +138,18 @@ function rebrand(mutations, observer) { observer.disconnect(); const elements = document.getElementsByClassName('install-status'); for (let i = elements.length; --i >= 0;) { - const el = elements[i]; - if (!el.textContent.includes('Stylish')) { - continue; - } - const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT); + const walker = document.createTreeWalker(elements[i], NodeFilter.SHOW_TEXT); while (walker.nextNode()) { const node = walker.currentNode; const text = node.nodeValue; - if (text.includes('Stylish') && node.parentNode.localName != 'a') { + const parent = node.parentNode; + const extensionHelp = /stylish_chrome/.test(parent.href); + if (text.includes('Stylish') && (parent.localName != 'a' || extensionHelp)) { node.nodeValue = text.replace(/Stylish/g, 'Stylus'); } + if (extensionHelp) { + parent.href = 'http://add0n.com/stylus.html'; + } } } } @@ -213,6 +204,19 @@ function styleSectionsEqual({sections: a}, {sections: b}) { } +function onDOMready() { + if (document.readyState != 'loading') { + return Promise.resolve(); + } + return new Promise(resolve => { + document.addEventListener('DOMContentLoaded', function _() { + document.removeEventListener('DOMContentLoaded', _); + resolve(); + }); + }); +} + + function orphanCheck() { const port = chrome.runtime.connect(); if (port) { diff --git a/manage.js b/manage.js index 0a995de4..464f47ca 100644 --- a/manage.js +++ b/manage.js @@ -558,7 +558,7 @@ class Updater { } checkMd5() { - return Updater.download(this.md5Url).then( + return download(this.md5Url).then( md5 => (md5.length == 32 ? this.decideOnMd5(md5 != this.md5) : this.onFailure(-1)), @@ -573,7 +573,7 @@ class Updater { } checkFullCode({forceUpdate = false} = {}) { - return Updater.download(this.url).then( + return download(this.url).then( text => this.handleJson(forceUpdate, JSON.parse(text)), status => this.onFailure(status)); } @@ -620,25 +620,6 @@ class Updater { filterAndAppend({entry: this.element}); } } - - static download(url) { - return new Promise((resolve, reject) => { - const xhr = new XMLHttpRequest(); - xhr.onloadend = () => (xhr.status == 200 - ? resolve(xhr.responseText) - : reject(xhr.status)); - if (url.length > 2000) { - const [mainUrl, query] = url.split('?'); - xhr.open('POST', mainUrl, true); - xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); - xhr.send(query); - } else { - xhr.open('GET', url); - xhr.send(); - } - }); - } - } diff --git a/messaging.js b/messaging.js index a5062b99..c0020345 100644 --- a/messaging.js +++ b/messaging.js @@ -306,3 +306,18 @@ function deleteStyleSafe({id, notify = true} = {}) { return id; }); } + + +function download(url) { + return new Promise((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.timeout = 10e3; + xhr.onloadend = () => (xhr.status == 200 + ? resolve(xhr.responseText) + : reject(xhr.status)); + const [mainUrl, query] = url.split('?'); + xhr.open(query ? 'POST' : 'GET', mainUrl, true); + xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + xhr.send(query); + }); +}