diff --git a/README.md b/README.md index 529a1da32d..193f0bbece 100644 --- a/README.md +++ b/README.md @@ -220,11 +220,13 @@ _**All `i18n` contributions are welcome to `i18n` [branch](https://github.com/li - **[Proxy β](https://github.com/postwoman-io/postwoman-proxy)** - A simple proxy server created for Postwoman - **[CLI β](https://github.com/postwoman-io/postwoman-cli)** - A CLI solution for Postwoman - - **[Browser Extensions](https://github.com/AndrewBastin/postwoman-firefox)** - Browser extensions that simplifies access to Postwoman + - **Browser Extensions** - Browser extensions that simplifies access to Postwoman - > [![Firefox](https://raw.github.com/alrra/browser-logos/master/src/firefox/firefox_16x16.png) **Firefox**](https://addons.mozilla.org/en-US/firefox/addon/postwoman)  |  ![Chrome](https://raw.github.com/alrra/browser-logos/master/src/chrome/chrome_16x16.png) **Chrome (coming soon)** + [![Firefox](https://raw.github.com/alrra/browser-logos/master/src/firefox/firefox_16x16.png) **Firefox**](https://addons.mozilla.org/en-US/firefox/addon/postwoman) ([GitHub](https://github.com/AndrewBastin/postwoman-firefox))  |  [![Chrome](https://raw.github.com/alrra/browser-logos/master/src/chrome/chrome_16x16.png) **Chrome**](https://chrome.google.com/webstore/detail/postwoman-extension-for-c/amknoiejhlmhancpahfcfcfhllgkpbld) ([GitHub](https://github.com/AndrewBastin/postwoman-chrome)) -_Add-ons are developed and maintained under **[Official Postwoman Organization](https://github.com/postwoman-io)**_ + >**Extensions fixes `CORS` issues.** + +_Add-ons are developed and maintained under **[Official Postwoman Organization](https://github.com/postwoman-io)**._ **To find out more, please check out [Postwoman Wiki](https://github.com/liyasthomas/postwoman/wiki).** diff --git a/functions/network.js b/functions/network.js index a2c885be5e..f56c526761 100644 --- a/functions/network.js +++ b/functions/network.js @@ -1,8 +1,13 @@ import AxiosStrategy from "./strategies/AxiosStrategy"; import FirefoxStrategy from "./strategies/FirefoxStrategy"; - +import ChromeStrategy, { hasChromeExtensionInstalled } from "./strategies/ChromeStrategy"; const runAppropriateStrategy = (req, store) => { + // Chrome Provides a chrome object for scripts to access + // Check its availability to say whether you are in Google Chrome + if (window.chrome && hasChromeExtensionInstalled()) { + return ChromeStrategy(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) { diff --git a/functions/strategies/ChromeStrategy.js b/functions/strategies/ChromeStrategy.js new file mode 100644 index 0000000000..03c05ea0ca --- /dev/null +++ b/functions/strategies/ChromeStrategy.js @@ -0,0 +1,56 @@ +const EXTENSION_ID = "amknoiejhlmhancpahfcfcfhllgkpbld"; + +// Check if the Chrome Extension is present +// The Chrome extension injects an empty span to help detection. +// Also check for the presence of window.chrome object to confirm smooth operations +export const hasChromeExtensionInstalled = () => { + return document.getElementById("chromePWExtensionDetect") !== null; +} + +const chromeWithoutProxy = (req, _store) => new Promise((resolve, reject) => { + chrome.runtime.sendMessage( + EXTENSION_ID, { + messageType: "send-req", + data: { + config: req + } + }, (message) => { + if (message.data.error) { + reject(message.data.error); + } else { + resolve(message.data.response); + } + } + ); +}); + +const chromeWithProxy = (req, { state }) => new Promise((resolve, reject) => { + chrome.runtime.sendMessage( + EXTENSION_ID, { + messageType: "send-req", + data: { + config: { + method: "post", + url: state.postwoman.settings.PROXY_URL || "https://postwoman.apollotv.xyz/", + data: req + } + } + }, (message) => { + if (message.data.error) { + reject(error); + } else { + resolve(message.data.response.data); + } + } + ) +}); + +const chromeStrategy = (req, store) => { + if (store.state.postwoman.settings.PROXY_ENABLED) { + return chromeWithProxy(req, store); + } else { + return chromeWithoutProxy(req, store); + } +} + +export default chromeStrategy; diff --git a/functions/strategies/FirefoxStrategy.js b/functions/strategies/FirefoxStrategy.js index afe63498cc..9160a27d31 100644 --- a/functions/strategies/FirefoxStrategy.js +++ b/functions/strategies/FirefoxStrategy.js @@ -6,7 +6,7 @@ const firefoxWithProxy = (req, { state }) => if (event.detail.error) { reject(JSON.parse(event.detail.error)); } else { - resolve(JSON.parse(event.detail.response)); + resolve(JSON.parse(event.detail.response).data); } }; diff --git a/layouts/default.vue b/layouts/default.vue index 4da707eec8..4002a8875e 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -470,27 +470,33 @@
- + + +
@@ -612,6 +618,7 @@