-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1010 from madfish-solutions/TW-881-not-found-erro…
…r-appears-after-clicking-on-decline-or-confirm-after-waiting-on-the-confirm-operation-page-for-30-s TW-881 Make background worker keeper script come back
- Loading branch information
Showing
9 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import 'lib/keep-bg-worker-alive/script'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
Inject this script into ServiceWorker. | ||
*/ | ||
|
||
import browser from 'webextension-polyfill'; | ||
|
||
import { BACKGROUND_IS_WORKER, KEEP_BACKGROUND_WORKER_ALIVE } from './utils'; | ||
|
||
if (BACKGROUND_IS_WORKER) { | ||
browser.runtime.onMessage.addListener((message: unknown) => | ||
isKeepAliveMessage(message) ? Promise.resolve(true) : void 0 | ||
); | ||
|
||
browser.runtime.onConnect.addListener(port => { | ||
if (port.name !== KEEP_BACKGROUND_WORKER_ALIVE) return; | ||
|
||
port.onMessage.addListener((message: unknown) => { | ||
if (isKeepAliveMessage(message)) port.postMessage(true); | ||
}); | ||
}); | ||
} | ||
|
||
const isKeepAliveMessage = (message: any) => | ||
typeof message === 'object' && message !== null && message.type === KEEP_BACKGROUND_WORKER_ALIVE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
Inject this script into Content Script of every possible opened tab. | ||
*/ | ||
|
||
import type Browser from 'webextension-polyfill'; | ||
|
||
import { browser, BACKGROUND_IS_WORKER, KEEP_BACKGROUND_WORKER_ALIVE, ping } from './utils'; | ||
|
||
if (BACKGROUND_IS_WORKER) { | ||
let port: Browser.Runtime.Port; | ||
(function connect() { | ||
port = browser.runtime.connect({ name: KEEP_BACKGROUND_WORKER_ALIVE }); | ||
port.onDisconnect.addListener(connect); | ||
})(); | ||
|
||
ping(port); | ||
|
||
setInterval(() => ping(port), 1_000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type Browser from 'webextension-polyfill'; | ||
|
||
export const browser = (() => { | ||
// @ts-ignore | ||
const browser: typeof Browser | undefined = globalThis.chrome || globalThis.browser; | ||
if (browser == null) throw new Error('Not browser extension'); | ||
return browser; | ||
})(); | ||
|
||
const MANIFEST_VERSION = browser.runtime.getManifest().manifest_version; | ||
|
||
export const BACKGROUND_IS_WORKER = MANIFEST_VERSION === 3; | ||
|
||
export const KEEP_BACKGROUND_WORKER_ALIVE = 'KEEP_BACKGROUND_WORKER_ALIVE'; | ||
|
||
/** | ||
* Avoiding "Extension context invalidated" error logs. | ||
*/ | ||
const contentScriptIsStillValid = () => Boolean(browser.runtime.id); | ||
|
||
export const ping = (port?: Browser.Runtime.Port) => { | ||
if (contentScriptIsStillValid() === false) return; | ||
|
||
const message = { type: KEEP_BACKGROUND_WORKER_ALIVE }; | ||
|
||
browser.runtime.sendMessage(message); | ||
|
||
port?.postMessage(message); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters