Skip to content

Commit

Permalink
refactor: switch to the alternative solution
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackGlory committed May 4, 2024
1 parent 607ae4a commit 8a018cb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
45 changes: 23 additions & 22 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ import { migrate } from './migrate.js'
import { each, Deferred } from 'extra-promise'
import { applyPropertyDecorators } from 'extra-proxy'
import { getActiveTab, waitForLaunch, LaunchReason } from 'extra-webextension'
import { IBackgroundAPI } from '@src/contract.js'
import { IBackgroundAPI, SpecialMessage } from '@src/contract.js'
import { ImplementationOf } from 'delight-rpc'
import { createServer } from '@delight-rpc/webextension'
import { updateMenu } from './menu.js'
import { isDev } from '@utils/is-dev.js'
import { assert, isntUndefined } from '@blackglory/prelude'
import { isntUndefined } from '@blackglory/prelude'

let activeFrameId = 0
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (sender.id === chrome.runtime.id) {
switch (message) {
case SpecialMessage.UpdateActiveFrameId: {
if (isntUndefined(sender.frameId)) {
activeFrameId = sender.frameId
sendResponse()
}
break
}
}
}
})

const launched = new Deferred<void>()

Expand Down Expand Up @@ -76,27 +90,14 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
chrome.commands.onCommand.addListener(async (command, tab) => {
tab = tab ?? await getActiveTab()

const tabId = tab.id
assert(isntUndefined(tabId))

const frames = await chrome.webNavigation.getAllFrames({ tabId }) ?? []

const result = await Promise.any(
frames.map(async frame => {
const result = await commandHandlers[command](
{
frameUrl: frame.url
, frameId: frame.frameId
}
, tab
)
assert(isntUndefined(result))

return result
})
const result = await commandHandlers[command](
{ frameId: activeFrameId }
, tab
)

await handleCommandResult(result)
if (result) {
await handleCommandResult(result)
}
})

async function ensureOffscreenDocument(): Promise<void> {
Expand Down
12 changes: 11 additions & 1 deletion src/content-script.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { createServer } from '@delight-rpc/webextension'
import { IFrameAPI } from '@src/contract.js'
import { IFrameAPI, SpecialMessage } from '@src/contract.js'
import { isDev } from '@utils/is-dev.js'

if (isDev()) {
console.info(`[${chrome.runtime.getManifest().name}] The content script is injected`)
}

window.addEventListener('focus', async () => {
try {
await chrome.runtime.sendMessage(SpecialMessage.UpdateActiveFrameId)
} catch (e) {
if (isDev()) {
console.error(e)
}
}
})

createServer<IFrameAPI>({
getActiveElementTextContent
, getDocumentTitle
Expand Down
4 changes: 4 additions & 0 deletions src/contract.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export enum SpecialMessage {
UpdateActiveFrameId = 'UpdateActiveFrameId'
}

export enum StorageItemKey {
Menu = 'menu'
, Config = 'config'
Expand Down
1 change: 0 additions & 1 deletion src/manifest.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
, "clipboardWrite"
, "offscreen"
, "storage"
, "webNavigation"
]
, "host_permissions": [
"<all_urls>"
Expand Down
1 change: 0 additions & 1 deletion src/manifest.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
, "clipboardWrite"
, "offscreen"
, "storage"
, "webNavigation"
]
, "host_permissions": [
"<all_urls>"
Expand Down

0 comments on commit 8a018cb

Please sign in to comment.