From 5d6b106145c90ea1b59f98ebfe0bc691780d407f Mon Sep 17 00:00:00 2001 From: DecentM Date: Tue, 9 Jan 2018 16:16:48 +0100 Subject: [PATCH] feature(openusercss-hook): add event based install functionality --- content/install-hook-openusercss.js | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/content/install-hook-openusercss.js b/content/install-hook-openusercss.js index 4a392088..184985d9 100644 --- a/content/install-hook-openusercss.js +++ b/content/install-hook-openusercss.js @@ -75,7 +75,43 @@ const attachHandshakeListeners = () => { }); }; +const sendInstallCallback = data => { + // Send an install callback to the site in order to let it know + // we were able to install the theme and it may display a success message + allowedOrigins.forEach(origin => { + window.postMessage({ + 'type': 'ouc-install-callback', + 'enabled': data.enabled, + 'key': data.key + }, origin); + }); +}; + +const attachInstallListeners = () => { + // Wait for an install event, then save the theme + window.addEventListener('message', event => { + if ( + event.data + && event.data.type === 'ouc-install-usercss' + && allowedOrigins.includes(event.origin) + ) { + chrome.runtime.sendMessage({ + 'method': 'saveUsercss', + 'reason': 'install', + 'name': event.data.title, + 'sourceCode': event.data.code, + }, response => { + sendInstallCallback({ + 'enabled': response.enabled, + 'key': event.data.key + }); + }); + } + }); +}; + (() => { attachHandshakeListeners(); + attachInstallListeners(); askHandshake(); })();