[Chrome] restore the install/update button functionality on USO

resolves #354 in Chrome only since the USO site is broken in FF
(the site checks for chrome-extension:// URL which should be moz-extension://)
This commit is contained in:
tophf 2018-02-27 21:30:29 +03:00
parent d1f35a15e3
commit d4b4b07ab3

View File

@ -15,17 +15,32 @@
chrome.runtime.onMessage.addListener(onMessage); chrome.runtime.onMessage.addListener(onMessage);
let gotBody = false;
new MutationObserver((mutations, observer) => { new MutationObserver((mutations, observer) => {
if (document.body) { if (!gotBody) {
observer.disconnect(); if (!document.body) return;
gotBody = true;
// TODO: remove the following statement when USO pagination title is fixed // TODO: remove the following statement when USO pagination title is fixed
document.title = document.title.replace(/^(\d+)&\w+=/, '#$1: '); document.title = document.title.replace(/^(\d+)&\w+=/, '#$1: ');
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
method: 'getStyles', method: 'getStyles',
md5Url: getMeta('stylish-md5-url') || location.href md5Url: getMeta('stylish-md5-url') || location.href
}, checkUpdatability); }, checkUpdatability);
return;
} }
}).observe(document.documentElement, {childList: true}); if (document.getElementById('install_button')) {
observer.disconnect();
onDOMready().then(() => {
requestAnimationFrame(() => {
sendEvent(sendEvent.lastEvent);
});
});
return;
}
}).observe(document.documentElement, {
childList: true,
subtree: true,
});
function onMessage(msg, sender, sendResponse) { function onMessage(msg, sender, sendResponse) {
switch (msg.method) { switch (msg.method) {
@ -58,7 +73,7 @@
detail: installedStyle && installedStyle.updateUrl, detail: installedStyle && installedStyle.updateUrl,
})); }));
if (!installedStyle) { if (!installedStyle) {
sendEvent('styleCanBeInstalledChrome'); sendEvent({type: 'styleCanBeInstalledChrome'});
return; return;
} }
const md5Url = getMeta('stylish-md5-url'); const md5Url = getMeta('stylish-md5-url');
@ -74,29 +89,32 @@
} }
function reportUpdatable(isUpdatable) { function reportUpdatable(isUpdatable) {
sendEvent( sendEvent({
isUpdatable type: isUpdatable
? 'styleCanBeUpdatedChrome' ? 'styleCanBeUpdatedChrome'
: 'styleAlreadyInstalledChrome', : 'styleAlreadyInstalledChrome',
{ detail: {
updateUrl: installedStyle.updateUrl updateUrl: installedStyle.updateUrl
} },
); });
} }
} }
function sendEvent(type, detail = null) { function sendEvent(event) {
sendEvent.lastEvent = event;
let {type, detail = null} = event;
if (FIREFOX) { if (FIREFOX) {
type = type.replace('Chrome', ''); type = type.replace('Chrome', '');
} else if (OPERA || VIVALDI) { } else if (OPERA || VIVALDI) {
type = type.replace('Chrome', 'Opera'); type = type.replace('Chrome', 'Opera');
} }
detail = {detail};
if (typeof cloneInto !== 'undefined') { if (typeof cloneInto !== 'undefined') {
// Firefox requires explicit cloning, however USO can't process our messages anyway // Firefox requires explicit cloning, however USO can't process our messages anyway
// 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); detail = cloneInto({detail}, document);
} else {
detail = {detail};
} }
onDOMready().then(() => { onDOMready().then(() => {
document.dispatchEvent(new CustomEvent(type, detail)); document.dispatchEvent(new CustomEvent(type, detail));
@ -164,7 +182,7 @@
if (!isNew && style.updateUrl.includes('?')) { if (!isNew && style.updateUrl.includes('?')) {
enableUpdateButton(true); enableUpdateButton(true);
} else { } else {
sendEvent('styleInstalledChrome'); sendEvent({type: 'styleInstalledChrome'});
} }
} }
); );