Skip to content

Commit

Permalink
refactor: remove webextension-polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackGlory committed Dec 20, 2023
1 parent f6b5999 commit 597519d
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 27 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@types/urijs": "^1.19.19",
"@types/webextension-polyfill": "^0.10.0",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"autoprefixer": "^10.4.16",
Expand Down Expand Up @@ -94,7 +93,6 @@
"remark-stringify": "^10.0.2",
"unified": "^10.1.2",
"urijs": "^1.19.11",
"url-operator": "^0.3.1",
"webextension-polyfill": "^0.10.0"
"url-operator": "^0.3.1"
}
}
3 changes: 1 addition & 2 deletions src/background/handlers/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browser from 'webextension-polyfill'
import { Awaitable } from '@blackglory/prelude'

export interface IInfo {
Expand All @@ -13,7 +12,7 @@ export interface IInfo {

export type CommandHandler = (
info: IInfo
, tab: browser.Tabs.Tab
, tab: chrome.tabs.Tab
) => Awaitable<CommandResult | undefined>

export interface CommandResult {
Expand Down
11 changes: 5 additions & 6 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import browser from 'webextension-polyfill'
import { commandHandlers, handleCommandResult } from './handlers/index.js'
import { initStorage, getMenu, getConfig, setConfig, setMenu } from './storage.js'
import { migrate } from './migrate.js'
Expand Down Expand Up @@ -61,7 +60,7 @@ waitForLaunch().then(async details => {
await injectContentScripts()
})

browser.contextMenus.onClicked.addListener(async (info, tab) => {
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
const result = await commandHandlers[info.menuItemId](
info
, tab ?? await getActiveTab()
Expand All @@ -72,7 +71,7 @@ browser.contextMenus.onClicked.addListener(async (info, tab) => {
}
})

browser.commands.onCommand.addListener(async (command, tab) => {
chrome.commands.onCommand.addListener(async (command, tab) => {
const result = await commandHandlers[command](
{}
, tab ?? await getActiveTab()
Expand Down Expand Up @@ -100,12 +99,12 @@ async function ensureOffscreenDocument(): Promise<void> {
* 将内容脚本尽可能注入到打开的标签页里.
*/
async function injectContentScripts(): Promise<void> {
const manifest = browser.runtime.getManifest()
const manifest = chrome.runtime.getManifest()
const contentScripts = manifest.content_scripts?.[0].js ?? []
await each(await browser.tabs.query({}), async ({ id }) => {
await each(await chrome.tabs.query({}), async ({ id }) => {
if (id) {
try {
await browser.scripting.executeScript({
await chrome.scripting.executeScript({
target: {
tabId: id
, allFrames: true
Expand Down
7 changes: 3 additions & 4 deletions src/background/menu.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import browser from 'webextension-polyfill'
import { getMenu } from './storage.js'
import { i18n } from '@utils/i18n.js'
import { convertMenuContextToBrowserContextType } from '@utils/menu-context.js'

export async function updateMenu(): Promise<void> {
const menu = await getMenu()

await browser.contextMenus.removeAll()
await chrome.contextMenus.removeAll()
for (const { context, items } of menu) {
for (const item of items) {
const props: browser.Menus.CreateCreatePropertiesType = {
const props: chrome.contextMenus.CreateProperties = {
id: item.id
, visible: item.visible
, type: 'normal'
, title: i18n(item.id)
, contexts: [convertMenuContextToBrowserContextType(context)]
}

browser.contextMenus.create(props)
chrome.contextMenus.create(props)
}
}
}
3 changes: 1 addition & 2 deletions src/content-script.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import brwoser from 'webextension-polyfill'
import { createServer } from '@delight-rpc/webextension'
import { IFrameAPI } from '@src/contract.js'

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

createServer<IFrameAPI>({
Expand Down
4 changes: 1 addition & 3 deletions src/utils/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import browser from 'webextension-polyfill'

export function i18n(message: string): string {
return browser.i18n.getMessage(message)
return chrome.i18n.getMessage(message)
}
3 changes: 1 addition & 2 deletions src/utils/menu-context.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import browser from 'webextension-polyfill'
import { MenuContext } from '@src/contract.js'
import { i18n } from '@utils/i18n.js'

export function convertMenuContextToBrowserContextType(
menuContext: MenuContext
): browser.Menus.ContextType {
): chrome.contextMenus.ContextType {
switch (menuContext) {
case MenuContext.Page: return 'page'
case MenuContext.Frame: return 'frame'
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1011,11 +1011,6 @@
resolved "https://registry.yarnpkg.com/@types/urijs/-/urijs-1.19.19.tgz#2789369799907fc11e2bc6e3a00f6478c2281b95"
integrity sha512-FDJNkyhmKLw7uEvTxx5tSXfPeQpO0iy73Ry+PmYZJvQy0QIWX8a7kJ4kLWRf+EbTPJEPDSgPXHaM7pzr5lmvCg==

"@types/webextension-polyfill@^0.10.0":
version "0.10.0"
resolved "https://registry.yarnpkg.com/@types/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz#e87b5e2c101599779a584cdb043887ad73b37b0e"
integrity sha512-If4EcaHzYTqcbNMp/FdReVdRmLL/Te42ivnJII551bYjhX19bWem5m14FERCqdJA732OloGuxCRvLBvcMGsn4A==

"@typescript-eslint/eslint-plugin@^5.56.0":
version "5.56.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.56.0.tgz#e4fbb4d6dd8dab3e733485c1a44a02189ae75364"
Expand Down

0 comments on commit 597519d

Please sign in to comment.