From 4b48d8a8c8a28ad69c21ae03056d0fc876c76755 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Thu, 16 Jan 2020 01:25:43 -0500 Subject: [PATCH 1/2] Refactor to bring the nuxt loading bar code out of the strategies --- functions/network.js | 11 +++++++++-- functions/strategies/AxiosStrategy.js | 1 - functions/strategies/ProxyStrategy.js | 1 - 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/functions/network.js b/functions/network.js index 97881ee84..cf16b4c55 100644 --- a/functions/network.js +++ b/functions/network.js @@ -1,12 +1,19 @@ import AxiosStrategy from "./strategies/AxiosStrategy"; import ProxyStrategy from "./strategies/ProxyStrategy"; +import FirefoxStrategy from "./strategies/FirefoxStrategy"; + + +const runAppropriateStrategy = (req, store) => { -const sendNetworkRequest = (req, store) => { if (store.state.postwoman.settings.PROXY_ENABLED) { return ProxyStrategy(req, store); } return AxiosStrategy(req, store); -}; +} + +const sendNetworkRequest = (req, store) => + runAppropriateStrategy(req, store) + .finally(() => window.$nuxt.$loading.finish()); export { sendNetworkRequest }; diff --git a/functions/strategies/AxiosStrategy.js b/functions/strategies/AxiosStrategy.js index de74ec377..9bef30aef 100644 --- a/functions/strategies/AxiosStrategy.js +++ b/functions/strategies/AxiosStrategy.js @@ -2,7 +2,6 @@ import axios from "axios"; const axiosStrategy = async (req, _store) => { const res = await axios(req); - window.$nuxt.$loading.finish(); return res; }; diff --git a/functions/strategies/ProxyStrategy.js b/functions/strategies/ProxyStrategy.js index 945482f48..0b6f50b0f 100644 --- a/functions/strategies/ProxyStrategy.js +++ b/functions/strategies/ProxyStrategy.js @@ -6,7 +6,6 @@ const proxyStrategy = async (req, store) => { "https://postwoman.apollotv.xyz/", req ); - window.$nuxt.$loading.finish(); return data; }; From 5070d9fe95f50081d928a32788aef5cfca17ddd2 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Thu, 16 Jan 2020 01:26:25 -0500 Subject: [PATCH 2/2] 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;