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
saveStyleSafe: false
sessionStorageHash: false
download: false
# localization.js
template: false
t: false

View File

@ -86,6 +86,12 @@ function onRuntimeMessage(request, sender, sendResponse) {
}
}
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) {
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);
});
}
@ -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
}
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) {

View File

@ -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();
}
});
}
}

View File

@ -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);
});
}