tweak USO handshake to match Stylish

fixes #379
This commit is contained in:
tophf 2018-04-30 19:31:43 +03:00
parent f9dd704134
commit 9aea35799d

View File

@ -2,19 +2,22 @@
'use strict'; 'use strict';
(() => { (() => {
const FIREFOX = !chrome.app;
const VIVALDI = chrome.app && /Vivaldi/.test(navigator.userAgent);
const OPERA = chrome.app && /OPR/.test(navigator.userAgent);
window.dispatchEvent(new CustomEvent(chrome.runtime.id + '-install')); window.dispatchEvent(new CustomEvent(chrome.runtime.id + '-install'));
window.addEventListener(chrome.runtime.id + '-install', orphanCheck, true); window.addEventListener(chrome.runtime.id + '-install', orphanCheck, true);
['Update', 'Install'].forEach(type => document.addEventListener('stylishInstallChrome', onClick);
['', 'Chrome', 'Opera'].forEach(browser => document.addEventListener('stylishUpdateChrome', onClick);
document.addEventListener('stylish' + type + browser, onClick)));
chrome.runtime.onMessage.addListener(onMessage); chrome.runtime.onMessage.addListener(onMessage);
document.addEventListener('DOMContentLoaded', function _() {
document.removeEventListener('DOMContentLoaded', _);
window.postMessage({
direction: 'from-content-script',
message: 'StylishInstalled',
}, '*');
});
let gotBody = false; let gotBody = false;
new MutationObserver(observeDOM).observe(document.documentElement, { new MutationObserver(observeDOM).observe(document.documentElement, {
childList: true, childList: true,
@ -77,14 +80,19 @@
sendEvent({type: 'styleCanBeInstalledChrome'}); sendEvent({type: 'styleCanBeInstalledChrome'});
return; return;
} }
const isCustomizable = /\?/.test(installedStyle.updateUrl);
const md5Url = getMeta('stylish-md5-url'); const md5Url = getMeta('stylish-md5-url');
if (md5Url && installedStyle.md5Url && installedStyle.originalMd5) { if (md5Url && installedStyle.md5Url && installedStyle.originalMd5) {
getResource(md5Url).then(md5 => { getResource(md5Url).then(md5 => {
reportUpdatable(md5 !== installedStyle.originalMd5); reportUpdatable(
isCustomizable ||
md5 !== installedStyle.originalMd5);
}); });
} else { } else {
getStyleJson().then(json => { getStyleJson().then(json => {
reportUpdatable(!json || reportUpdatable(
isCustomizable ||
!json ||
!styleSectionsEqual(json, installedStyle)); !styleSectionsEqual(json, installedStyle));
}); });
} }
@ -105,11 +113,6 @@
function sendEvent(event) { function sendEvent(event) {
sendEvent.lastEvent = event; sendEvent.lastEvent = event;
let {type, detail = null} = event; let {type, detail = null} = event;
if (FIREFOX) {
type = type.replace('Chrome', '');
} else if (OPERA || VIVALDI) {
type = type.replace('Chrome', 'Opera');
}
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