Skip to content

Commit

Permalink
Compitible with mv2
Browse files Browse the repository at this point in the history
  • Loading branch information
sheepzh committed Jul 4, 2023
1 parent 3c10c3e commit 13f7dd8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
22 changes: 22 additions & 0 deletions src/api/chrome/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IS_MV3 } from "@util/constant/environment"
import { handleError } from "./common"

export async function executeScript(tabId: number, files: string[]): Promise<void> {
if (IS_MV3) {
try {
await chrome.scripting.executeScript({ target: { tabId }, files })
} catch {
}
} else {
await Promise.all(files.map(file => executeScriptMv2(tabId, file)))
}
}

function executeScriptMv2(tabId: number, file: string): Promise<void> {
return new Promise(resolve => {
chrome.tabs.executeScript(tabId, { file }, () => {
handleError('executeScript')
resolve()
})
})
}
11 changes: 7 additions & 4 deletions src/background/browser-action-menu-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
import { OPTION_ROUTE } from "../app/router/constants"
import { getAppPageUrl, getGuidePageUrl, GITHUB_ISSUE_ADD, SOURCE_CODE_PAGE, TU_CAO_PAGE } from "@util/constant/url"
import { t2Chrome } from "@i18n/chrome/t"
import { IS_SAFARI } from "@util/constant/environment"
import { IS_MV3, IS_SAFARI } from "@util/constant/environment"
import { createTab } from "@api/chrome/tab"
import { getRuntimeId } from "@api/chrome/runtime"
import { createContextMenu } from "@api/chrome/context-menu"
import { getRuntimeId } from "@api/chrome/runtime"
import { locale } from "@i18n"
import { START_ROUTE } from "@guide/router/constants"

const APP_PAGE_URL = getAppPageUrl(true)

const baseProps: Partial<chrome.contextMenus.CreateProperties> = {
contexts: ['action'],
const baseProps: Partial<ChromeContextMenuCreateProps> = {
// Cast unknown to fix the error with manifestV2
// Because 'browser_action' will be replaced with 'action' in union type chrome.contextMenus.ContextType since V3
// But 'action' does not work in V2
contexts: [IS_MV3 ? 'action' : 'browser_action'],
visible: true
}

Expand Down
5 changes: 4 additions & 1 deletion src/background/content-script-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export default function init(dispatcher: MessageDispatcher) {
// Judge is in whitelist
.register<string, boolean>('cs.isInWhitelist', host => whitelistService.include(host))
// Need to print the information of today
.register<void, boolean>('cs.printTodayInfo', async () => !!(await optionService.getAllOption())?.printInConsole)
.register<void, boolean>('cs.printTodayInfo', async () => {
const option = await optionService.getAllOption()
return !!option.printInConsole
})
// Get today info
.register<string, timer.stat.Result>('cs.getTodayInfo', host => statService.getResult(host, new Date()))
// More minutes
Expand Down
5 changes: 1 addition & 4 deletions src/background/install-handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { onInstalled } from "@api/chrome/runtime"
import { executeScript } from "@api/chrome/script"
import { createTab, listTabs } from "@api/chrome/tab"
import metaService from "@service/meta-service"
import { getGuidePageUrl } from "@util/constant/url"
Expand All @@ -10,10 +11,6 @@ async function onFirstInstall() {
metaService.updateInstallTime(new Date())
}

function executeScript(tabId: number, files: string[]) {
chrome.scripting.executeScript({ target: { tabId }, files }).catch(err => console.log(err))
}

async function reloadContentScript() {
const files = chrome.runtime.getManifest().content_scripts?.[0]?.js
if (!files?.length) {
Expand Down
2 changes: 1 addition & 1 deletion src/background/whitelist-menu-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ async function init() {
optionService.addOptionChangeListener(option => visible = option.displayWhitelistMenu)
}

export default init
export default init

0 comments on commit 13f7dd8

Please sign in to comment.