Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firefox Extension compatibility #494

Merged
merged 3 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions functions/network.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import AxiosStrategy from "./strategies/AxiosStrategy";
import ProxyStrategy from "./strategies/ProxyStrategy";
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);
}

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 };
1 change: 0 additions & 1 deletion functions/strategies/AxiosStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import axios from "axios";

const axiosStrategy = async (req, _store) => {
const res = await axios(req);
window.$nuxt.$loading.finish();
return res;
};

Expand Down
15 changes: 15 additions & 0 deletions functions/strategies/FirefoxStrategy.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 0 additions & 1 deletion functions/strategies/ProxyStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const proxyStrategy = async (req, store) => {
"https://postwoman.apollotv.xyz/",
req
);
window.$nuxt.$loading.finish();
return data;
};

Expand Down