From 5070d9fe95f50081d928a32788aef5cfca17ddd2 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Thu, 16 Jan 2020 01:26:25 -0500 Subject: [PATCH] Added FirefoxStrategy to interact with the Firefox extension for Postwoman --- functions/network.js | 5 +++++ functions/strategies/FirefoxStrategy.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 functions/strategies/FirefoxStrategy.js diff --git a/functions/network.js b/functions/network.js index cf16b4c55..7ed233cf8 100644 --- a/functions/network.js +++ b/functions/network.js @@ -4,6 +4,11 @@ import FirefoxStrategy from "./strategies/FirefoxStrategy"; const runAppropriateStrategy = (req, store) => { + // The firefox plugin injects a function to send requests through it + // If that is available, then we can use the FirefoxStrategy + if (window.firefoxExtSendRequest) { + return FirefoxStrategy(req, store); + } if (store.state.postwoman.settings.PROXY_ENABLED) { return ProxyStrategy(req, store); diff --git a/functions/strategies/FirefoxStrategy.js b/functions/strategies/FirefoxStrategy.js new file mode 100644 index 000000000..77b15f453 --- /dev/null +++ b/functions/strategies/FirefoxStrategy.js @@ -0,0 +1,15 @@ +const firefoxStrategy = (req, _store) => new Promise((resolve, reject) => { + + const eventListener = (event) => { + window.removeEventListener("firefoxExtSendRequestComplete", eventListener); + + if (event.detail.error) reject(JSON.parse(event.detail.error)); + else resolve(JSON.parse(event.detail.response)); + }; + + window.addEventListener("firefoxExtSendRequestComplete", eventListener); + + window.firefoxExtSendRequest(req); +}); + +export default firefoxStrategy;