notify USO earlier in install.js by relaying xhr

This commit is contained in:
tophf 2017-04-20 04:46:04 +03:00
parent 98c34da9e7
commit aa5fc9f640
5 changed files with 55 additions and 48 deletions

View File

@ -33,6 +33,7 @@ globals:
getStylesSafe: false getStylesSafe: false
saveStyleSafe: false saveStyleSafe: false
sessionStorageHash: false sessionStorageHash: false
download: false
# localization.js # localization.js
template: false template: false
t: false t: false

View File

@ -86,6 +86,12 @@ function onRuntimeMessage(request, sender, sendResponse) {
} }
} }
break; break;
case 'download':
download(request.url)
.then(sendResponse)
.catch(() => sendResponse(null));
return KEEP_CHANNEL_OPEN;
} }
} }

View File

@ -18,19 +18,16 @@ function waitForBody() {
if (!document.body) { if (!document.body) {
return; return;
} }
this.disconnect(); this.disconnect();
rebrand([{addedNodes: [document.body]}]); rebrand([{addedNodes: [document.body]}]);
new MutationObserver(rebrand) new MutationObserver(rebrand)
.observe(document.body, {childList: true, subtree: true}); .observe(document.body, {childList: true, subtree: true});
document.addEventListener('DOMContentLoaded', function _() { chrome.runtime.sendMessage({
document.removeEventListener('DOMContentLoaded', _); method: 'getStyles',
chrome.runtime.sendMessage({ url: getMeta('stylish-id-url') || location.href
method: 'getStyles', }, checkUpdatability);
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 // because USO tries to use a global "event" variable deprecated in Firefox
detail = cloneInto(detail, document); // eslint-disable-line no-undef 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) { function getResource(url) {
if (url.startsWith('#')) {
return Promise.resolve(document.getElementById(url.slice(1)).textContent);
}
return new Promise(resolve => { return new Promise(resolve => {
const xhr = new XMLHttpRequest(); if (url.startsWith('#')) {
xhr.onloadend = () => resolve(xhr.status < 400 ? xhr.responseText : null); resolve(document.getElementById(url.slice(1)).textContent);
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 { } else {
xhr.open('GET', url); chrome.runtime.sendMessage({method: 'download', url}, resolve);
xhr.send();
} }
}); });
} }
@ -148,17 +138,18 @@ function rebrand(mutations, observer) {
observer.disconnect(); observer.disconnect();
const elements = document.getElementsByClassName('install-status'); const elements = document.getElementsByClassName('install-status');
for (let i = elements.length; --i >= 0;) { for (let i = elements.length; --i >= 0;) {
const el = elements[i]; const walker = document.createTreeWalker(elements[i], NodeFilter.SHOW_TEXT);
if (!el.textContent.includes('Stylish')) {
continue;
}
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
while (walker.nextNode()) { while (walker.nextNode()) {
const node = walker.currentNode; const node = walker.currentNode;
const text = node.nodeValue; 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'); 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() { function orphanCheck() {
const port = chrome.runtime.connect(); const port = chrome.runtime.connect();
if (port) { if (port) {

View File

@ -558,7 +558,7 @@ class Updater {
} }
checkMd5() { checkMd5() {
return Updater.download(this.md5Url).then( return download(this.md5Url).then(
md5 => (md5.length == 32 md5 => (md5.length == 32
? this.decideOnMd5(md5 != this.md5) ? this.decideOnMd5(md5 != this.md5)
: this.onFailure(-1)), : this.onFailure(-1)),
@ -573,7 +573,7 @@ class Updater {
} }
checkFullCode({forceUpdate = false} = {}) { checkFullCode({forceUpdate = false} = {}) {
return Updater.download(this.url).then( return download(this.url).then(
text => this.handleJson(forceUpdate, JSON.parse(text)), text => this.handleJson(forceUpdate, JSON.parse(text)),
status => this.onFailure(status)); status => this.onFailure(status));
} }
@ -620,25 +620,6 @@ class Updater {
filterAndAppend({entry: this.element}); 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();
}
});
}
} }

View File

@ -306,3 +306,18 @@ function deleteStyleSafe({id, notify = true} = {}) {
return id; 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);
});
}