api-client/functions/strategies/ChromeStrategy.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-02-24 18:44:50 +00:00
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
2020-02-20 03:22:42 +00:00
export const hasChromeExtensionInstalled = () =>
2020-02-24 18:44:50 +00:00
document.getElementById('chromePWExtensionDetect') !== null
2020-02-19 03:59:49 +00:00
const chromeWithoutProxy = (req, _store) =>
new Promise((resolve, reject) => {
chrome.runtime.sendMessage(
EXTENSION_ID,
{
2020-02-24 18:44:50 +00:00
messageType: 'send-req',
2020-02-19 03:59:49 +00:00
data: {
2020-02-24 18:44:50 +00:00
config: req,
},
2020-02-19 03:59:49 +00:00
},
2020-02-20 03:22:42 +00:00
({ data }) => {
if (data.error) {
2020-02-24 18:44:50 +00:00
reject(data.error)
2020-02-19 03:59:49 +00:00
} else {
2020-02-24 18:44:50 +00:00
resolve(data.response)
2020-02-19 03:59:49 +00:00
}
}
2020-02-24 18:44:50 +00:00
)
})
2020-02-19 03:59:49 +00:00
const chromeWithProxy = (req, { state }) =>
new Promise((resolve, reject) => {
chrome.runtime.sendMessage(
EXTENSION_ID,
{
2020-02-24 18:44:50 +00:00
messageType: 'send-req',
2020-02-19 03:59:49 +00:00
data: {
config: {
2020-02-24 18:44:50 +00:00
method: 'post',
url: state.postwoman.settings.PROXY_URL || 'https://postwoman.apollotv.xyz/',
data: req,
},
},
2020-02-19 03:59:49 +00:00
},
2020-02-20 03:22:42 +00:00
({ data }) => {
if (data.error) {
2020-02-24 18:44:50 +00:00
reject(error)
2020-02-19 03:59:49 +00:00
} else {
2020-02-24 18:44:50 +00:00
resolve(data.response.data)
}
}
2020-02-24 18:44:50 +00:00
)
})
const chromeStrategy = (req, store) => {
if (store.state.postwoman.settings.PROXY_ENABLED) {
2020-02-24 18:44:50 +00:00
return chromeWithProxy(req, store)
} else {
2020-02-24 18:44:50 +00:00
return chromeWithoutProxy(req, store)
}
2020-02-24 18:44:50 +00:00
}
2020-02-24 18:44:50 +00:00
export default chromeStrategy