refactor(opnusercss-hook): modularize logic

This commit is contained in:
DecentM 2018-01-09 16:14:56 +01:00
parent b9e6b660a7
commit b65896294e
No known key found for this signature in database
GPG Key ID: 6BDA2D2BC5EA5B10

View File

@ -1,29 +1,25 @@
'use strict'; 'use strict';
(() => {
const manifest = chrome.runtime.getManifest(); const manifest = chrome.runtime.getManifest();
const allowedOrigins = [ const allowedOrigins = [
'https://openusercss.org', 'https://openusercss.org',
'https://openusercss.com' 'https://openusercss.com'
]; ];
const askHandshake = () => {
// Tell the page that we exist and that it should send the handshake // Tell the page that we exist and that it should send the handshake
allowedOrigins.forEach(origin => { allowedOrigins.forEach(origin => {
window.postMessage({ window.postMessage({
'type': 'ouc-begin-handshake' 'type': 'ouc-begin-handshake'
}, origin); }, origin);
}); });
};
// Wait for the handshake const doHandshake = () => {
window.addEventListener('message', event => {
if (
event.data
&& event.data.type === 'ouc-handshake-question'
&& allowedOrigins.includes(event.origin)
) {
// This is a representation of features that Stylus is capable of // This is a representation of features that Stylus is capable of
const implementedFeatures = [ const implementedFeatures = [
'install-usercss', 'install-usercss',
'install-usercss-event',
'configure-after-install', 'configure-after-install',
'builtin-editor', 'builtin-editor',
'create-usercss', 'create-usercss',
@ -64,6 +60,22 @@
} }
}, origin); }, origin);
}); });
};
const attachHandshakeListeners = () => {
// Wait for the handshake request, then start it
window.addEventListener('message', event => {
if (
event.data
&& event.data.type === 'ouc-handshake-question'
&& allowedOrigins.includes(event.origin)
) {
doHandshake();
} }
}); });
};
(() => {
attachHandshakeListeners();
askHandshake();
})(); })();