feature: add installed query event listener and response

This commit is contained in:
DecentM 2018-01-25 18:26:41 +01:00
parent fa2a014120
commit f420f14dd3
No known key found for this signature in database
GPG Key ID: 6BDA2D2BC5EA5B10

View File

@ -15,11 +15,53 @@ const askHandshake = () => {
}); });
}; };
// Listen for queries by the site and respond with a callback object
const sendInstalledCallback = styleData => {
allowedOrigins.forEach(origin => {
window.postMessage({
'type': 'ouc-is-installed-response',
'style': styleData
}, origin);
});
};
const attachInstalledListeners = () => {
window.addEventListener('message', event => {
if (
event.data
&& event.data.type === 'ouc-is-installed'
&& allowedOrigins.includes(event.origin)
) {
chrome.runtime.sendMessage({
'method': 'findUsercss',
'name': event.data.name,
'namespace': event.data.namespace
}, style => {
if (style) {
sendInstalledCallback({
'installed': true,
'enabled': style.enabled,
'name': event.data.name,
'namespace': event.data.namespace
});
} else {
sendInstalledCallback({
'installed': false,
'name': event.data.name,
'namespace': event.data.namespace
});
}
});
}
});
};
const doHandshake = () => { const doHandshake = () => {
// 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', 'event:install-usercss',
'event:is-installed',
'configure-after-install', 'configure-after-install',
'builtin-editor', 'builtin-editor',
'create-usercss', 'create-usercss',
@ -112,5 +154,6 @@ const attachInstallListeners = () => {
(() => { (() => {
attachHandshakeListeners(); attachHandshakeListeners();
attachInstallListeners(); attachInstallListeners();
attachInstalledListeners();
askHandshake(); askHandshake();
})(); })();