From 66eecf5e37240f1b88ec4b55c8191a06cac3c602 Mon Sep 17 00:00:00 2001 From: Levin Rickert Date: Sun, 16 Feb 2020 15:48:10 +0100 Subject: [PATCH] Unify Chrome and Firefox extensions --- functions/network.js | 7 +++++++ functions/strategies/ExtensionStrategy.js | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 functions/strategies/ExtensionStrategy.js diff --git a/functions/network.js b/functions/network.js index 9294ba036..591d8f4d3 100644 --- a/functions/network.js +++ b/functions/network.js @@ -1,4 +1,5 @@ import AxiosStrategy from "./strategies/AxiosStrategy"; +import ExtensionStrategy from "./strategies/ExtensionStrategy"; import FirefoxStrategy from "./strategies/FirefoxStrategy"; import ChromeStrategy, { hasChromeExtensionInstalled } from "./strategies/ChromeStrategy"; @@ -9,6 +10,12 @@ const isExtensionsAllowed = ({ state }) => { const runAppropriateStrategy = (req, store) => { if (isExtensionsAllowed(store)) { + if (typeof window.__POSTWOMAN_EXTENSION_HOOK__ !== 'undefined') { + return ExtensionStrategy(req, store); + } + + // The following strategies are deprecated and kept to support older version of the extensions + // Chrome Provides a chrome object for scripts to access // Check its availability to say whether you are in Google Chrome if (window.chrome && hasChromeExtensionInstalled()) { diff --git a/functions/strategies/ExtensionStrategy.js b/functions/strategies/ExtensionStrategy.js new file mode 100644 index 000000000..3305f0b6e --- /dev/null +++ b/functions/strategies/ExtensionStrategy.js @@ -0,0 +1,22 @@ +const extensionWithProxy = async (req, { state }) => { + const { data } = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest({ + method: "post", + url: state.postwoman.settings.PROXY_URL || "https://postwoman.apollotv.xyz/", + data: req + }); + return data; +}; + +const extensionWithoutProxy = async (req, _store) => { + const res = await window.__POSTWOMAN_EXTENSION_HOOK__.sendRequest(req); + return res; +}; + +const extensionStrategy = (req, store) => { + if (store.state.postwoman.settings.PROXY_ENABLED) { + return extensionWithProxy(req, store); + } + return extensionWithoutProxy(req, store); +}; + +export default extensionStrategy;