From c34b054642ba2aa0dcc4b1b35bbb4895af5fa943 Mon Sep 17 00:00:00 2001 From: tophf Date: Fri, 30 Jul 2021 17:04:15 +0300 Subject: [PATCH] refactor usw content script (#1300) * extract handlers to async functions * limit event source to the current window --- content/install-hook-userstylesworld.js | 70 +++++++++++-------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/content/install-hook-userstylesworld.js b/content/install-hook-userstylesworld.js index 59a3ee68..66801e12 100644 --- a/content/install-hook-userstylesworld.js +++ b/content/install-hook-userstylesworld.js @@ -2,48 +2,42 @@ 'use strict'; (() => { - const allowedOrigin = 'https://userstyles.world'; + const ORIGIN = 'https://userstyles.world'; + const HANDLERS = Object.assign(Object.create(null), { - const sendPostMessage = message => { - if (allowedOrigin === location.origin) { - window.postMessage(message, location.origin); - } - }; + async 'usw-ready'() { + send({type: 'usw-remove-stylus-button'}); + if (location.pathname === '/api/oauth/style/new') { + const styleId = Number(new URLSearchParams(location.search).get('vendor_data')); + const data = await API.data.pop('usw' + styleId); + send({type: 'usw-fill-new-style', data}); + } + }, - const onPageLoaded = event => { - if (event.data - && allowedOrigin === event.origin - ) { - switch (event.data.type) { - case 'usw-ready': { - sendPostMessage({type: 'usw-remove-stylus-button'}); - - if (location.pathname === '/api/oauth/style/new') { - const styleId = Number(new URLSearchParams(location.search).get('vendor_data')); - API.data.pop('usw' + styleId).then(data => { - sendPostMessage({type: 'usw-fill-new-style', data}); - }); - } - break; - } - case 'usw-style-info-request': { - switch (event.data.requestType) { - case 'installed': { - API.styles.find({updateUrl: `https://userstyles.world/api/style/${event.data.styleID}.user.css`}) - .then(style => { - sendPostMessage({ - type: 'usw-style-info-response', - data: {installed: Boolean(style), requestType: 'installed'}, - }); - }); - break; - } - } + async 'usw-style-info-request'(data) { + switch (data.requestType) { + case 'installed': { + const updateUrl = `${ORIGIN}/api/style/${data.styleID}.user.css`; + const style = await API.styles.find({updateUrl}); + send({ + type: 'usw-style-info-response', + data: {installed: Boolean(style), requestType: 'installed'}, + }); break; } } - } - }; + }, + }); - window.addEventListener('message', onPageLoaded); + window.addEventListener('message', ({data, source, origin}) => { + // Some browser don't reveal `source` to extensions e.g. Firefox + if (data && (source ? source === window : origin === ORIGIN)) { + const fn = HANDLERS[data.type]; + if (fn) fn(data); + } + }); + + function send(msg) { + window.postMessage(msg, ORIGIN); + } })();