Skip to content

Commit

Permalink
Merge background scripts into one file
Browse files Browse the repository at this point in the history
  • Loading branch information
lk-geimfari committed Dec 4, 2023
1 parent 9fef3cb commit 6cb9ed0
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 60 deletions.
34 changes: 0 additions & 34 deletions src/chrome/js/background.js

This file was deleted.

Expand Up @@ -12,25 +12,34 @@ import {
handleTabState,
} from 'Background/handlers'

import browser from './browser-api'

browser.alarms.onAlarm.addListener(handleOnAlarm)
browser.proxy.onError.addListener(handleProxyError)
browser.runtime.onStartup.addListener(handleStartup)
browser.runtime.onInstalled.addListener(handleInstalled)
browser.runtime.onUpdateAvailable.addListener(handleOnUpdateAvailable)
browser.tabs.onUpdated.addListener(handleTabState)
browser.tabs.onCreated.addListener(handleTabCreate)
browser.storage.onChanged.addListener(handleStorageChanged)
browser.storage.onChanged.addListener(handleIgnoredHostsChange)
browser.storage.onChanged.addListener(handleCustomProxiedDomainsChange)

browser.webRequest.onBeforeRequest.addListener(
handleBeforeRequest, {
urls: [
'http://*/*',
'https://*/*',
],
types: ['main_frame'],
},
)
if (browser.isFirefox) {
browser.proxy.onError.addListener(handleProxyError)
browser.webRequest.onBeforeRequest.addListener(
handleBeforeRequest, {
urls: ['http://*/*', 'https://*/*'],
types: ['main_frame'],
},
)

browser.tabs.onUpdated.addListener(handleTabState)
browser.tabs.onCreated.addListener(handleTabCreate)
browser.webRequest.onErrorOccurred.addListener(handleProxyError, { urls: ['<all_urls>'] })
browser.runtime.onUpdateAvailable.addListener(handleOnUpdateAvailable)
browser.webRequest.onErrorOccurred.addListener(handleProxyError, { urls: ['<all_urls>'] })
} else {
chrome.webNavigation.onBeforeNavigate.addListener(
handleBeforeRequest, {
urls: ['http://*/*', 'https://*/*'],
types: ['main_frame'],
},
)
chrome.proxy.onProxyError.addListener(handleProxyError)
}
4 changes: 2 additions & 2 deletions src/shared/js/background/browser-api.js
Expand Up @@ -26,10 +26,10 @@ export const getBrowserInfo = () => {
*/
const getBrowser = () => {
if (typeof browser !== 'undefined') {
browser.IS_FIREFOX = true
browser.isFirefox = true
return browser
}
chrome.IS_FIREFOX = false
chrome.isFirefox = false
return chrome
}

Expand Down
6 changes: 3 additions & 3 deletions src/shared/js/background/proxy.js
Expand Up @@ -31,7 +31,7 @@ class ProxyManager {
}

async requestIncognitoAccess () {
if (browser.IS_FIREFOX) {
if (browser.isFirefox) {
const isAllowedIncognitoAccess =
await browser.extension.isAllowedIncognitoAccess()

Expand All @@ -46,7 +46,7 @@ class ProxyManager {
}

async grantIncognitoAccess () {
if (browser.IS_FIREFOX) {
if (browser.isFirefox) {
await browser.browserAction.setBadgeText({ text: '' })
await browser.storage.local.set({
privateBrowsingPermissionsRequired: false,
Expand Down Expand Up @@ -74,7 +74,7 @@ class ProxyManager {
proxyServerProtocol,
})

if (browser.IS_FIREFOX) {
if (browser.isFirefox) {
const blob = new Blob([pacData], {
type: 'application/x-ns-proxy-autoconfig',
})
Expand Down
4 changes: 2 additions & 2 deletions src/shared/js/background/settings.js
Expand Up @@ -13,7 +13,7 @@ class Settings {
const title = this.getName()
const path = Browser.runtime.getURL(`images/icons/128x128/${filename}.png`)

if (Browser.IS_FIREFOX) {
if (Browser.isFirefox) {
Browser.browserAction.setIcon({ tabId, path })
Browser.browserAction.setTitle({ title, tabId })
} else {
Expand Down Expand Up @@ -61,7 +61,7 @@ class Settings {
showNotifications: false,
})

if (Browser.IS_FIREFOX) {
if (Browser.isFirefox) {
await Browser.browserAction.setBadgeText({ text: '' })
}

Expand Down
2 changes: 1 addition & 1 deletion src/shared/js/pages/options.js
Expand Up @@ -82,7 +82,7 @@ import * as server from 'Background/server'
proxyStatus.hidden = false
}

if (browser.IS_FIREFOX) {
if (browser.isFirefox) {
const allowedIncognitoAccess =
await browser.extension.isAllowedIncognitoAccess()
const { privateBrowsingPermissionsRequired } =
Expand Down
4 changes: 2 additions & 2 deletions src/shared/js/pages/popup.js
Expand Up @@ -265,7 +265,7 @@ import {
if (extensionEnabled) {
statusImage.setAttribute('src', 'images/icons/512x512/normal.png')

if (Browser.IS_FIREFOX) {
if (Browser.isFirefox) {
Browser.extension.isAllowedIncognitoAccess()
.then((allowedIncognitoAccess) => {
Browser.storage.local
Expand Down Expand Up @@ -370,7 +370,7 @@ import {

ProxyManager.controlledByOtherExtensions().then(
(controlledByOtherExtensions) => {
if (!Browser.IS_FIREFOX && controlledByOtherExtensions) {
if (!Browser.isFirefox && controlledByOtherExtensions) {
controlledByOtherExtensionsButton.hidden = false
}
},
Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Expand Up @@ -32,7 +32,7 @@ const webWorkerConfig = {
mode: NODE_ENV,
target: isFirefox ? 'webworker' : 'web',
entry: {
background: './src/chrome/js/background.js',
background: './src/shared/js/background/background.js',
},
output: {
path: resolve(`dist/chrome/${OUTPUT_SUB_DIR}`),
Expand Down Expand Up @@ -268,7 +268,7 @@ const webConfig = {
}

if (isFirefox) {
webConfig.entry.background = `./src/firefox/js/background.js`
webConfig.entry.background = `./src/shared/js/background/background.js`
webConfig.entry.incognito_required = `./src/firefox/js/pages/incognito-required.js`
webConfig.entry.installed = './src/firefox/js/pages/installed.js'
webConfig.plugins.push(new HTMLWebpackPlugin({
Expand Down

0 comments on commit 6cb9ed0

Please sign in to comment.