Skip to content

Commit

Permalink
chore: switch default to mv3
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed May 12, 2024
1 parent f316dee commit deaf19a
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .env/dev-preference.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# Use Chromium Manifest V3 as default development target.

; manifest=chromium-mv2
; manifest=chromium-beta-mv2
; manifest=chromium-beta-mv3
; manifest=chromium-mv3
; manifest=firefox-mv2
; manifest=firefox-mv3
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"start": "dev",
"start:mv2": "dev --manifest 2",
"start:mv3": "dev --manifest 3",
"start:fx": "dev --manifest firefox-mv2",
"start:fx": "dev --manifest firefox-mv3",
"codegen": "gulp codegen-watch",
"build": "build",
"build:mv3": "build --manifest 3",
Expand Down
10 changes: 8 additions & 2 deletions packages/mask/.webpack/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { join, isAbsolute } from 'node:path'

export enum ManifestFile {
ChromiumMV2 = 'chromium-mv2',
ChromiumBetaMV2 = 'chromium-beta-mv2',
ChromiumMV3 = 'chromium-mv3',
ChromiumBetaMV3 = 'chromium-beta-mv3',
FirefoxMV2 = 'firefox-mv2',
FirefoxMV3 = 'firefox-mv3',
SafariMV3 = 'safari-mv3',
Expand Down Expand Up @@ -39,7 +39,7 @@ export function normalizeBuildFlags(flags: BuildFlags): NormalizedFlags {
channel = 'stable',
devtoolsEditorURI = 'vscode://file/{path}:{line}',
sourceMapHideFrameworks = true,
manifestFile = ManifestFile.ChromiumMV2,
manifestFile = ManifestFile.ChromiumMV3,
} = flags
let {
hmr = mode === 'development',
Expand All @@ -50,6 +50,12 @@ export function normalizeBuildFlags(flags: BuildFlags): NormalizedFlags {
} = flags
if (!isAbsolute(outputPath)) outputPath = join(import.meta.dirname, '../../../', outputPath)

if (manifestFile === ManifestFile.FirefoxMV3) {
// TODO: Firefox MV3 blocked by website's CSP
hmr = false
// TODO: "devtools_page" in manifest.json causes all Promises in browser.* hang
devtools = false
}
if (mode === 'production' || profiling) hmr = false
if (!hmr) reactRefresh = false

Expand Down
8 changes: 6 additions & 2 deletions packages/mask/.webpack/manifest/manifest-mv3.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"optional_permissions": ["notifications", "clipboardRead"],
"optional_host_permissions": ["<all_urls>"],
"background": { "service_worker": "/manifest-v3.entry.js" },
"options_ui": { "page": "dashboard.html", "open_in_tab": true },
"icons": {
"16": "assets/16x16.png",
"48": "assets/48x48.png",
Expand All @@ -31,5 +30,10 @@
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'; trusted-types default dompurify mask;"
},
"minimum_chrome_version": "102"
"minimum_chrome_version": "102",
"browser_specific_settings": {
"gecko": {
"id": "{0e57e2ae-3e13-4d5d-9243-6159219852a6}"
}
}
}
1 change: 0 additions & 1 deletion packages/mask/.webpack/manifest/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"permissions": ["storage", "webNavigation", "activeTab"],
"optional_permissions": ["<all_urls>", "notifications", "clipboardRead"],
"background": { "page": "background.html" },
"options_ui": { "page": "dashboard.html", "open_in_tab": true },
"icons": {
"16": "assets/16x16.png",
"48": "assets/48x48.png",
Expand Down
17 changes: 15 additions & 2 deletions packages/mask/.webpack/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function prepareAllManifest(flags: NormalizedFlags, computedFlags: ComputedFlags
)

const manifestFlags: Record<NormalizedFlags['manifestFile'], ManifestPresets> = {
'chromium-beta-mv2': [{ ...flags, channel: 'beta' }, mv2Base],
'chromium-beta-mv3': [{ ...flags, channel: 'beta' }, mv3Base],
'chromium-mv2': [flags, mv2Base],
'chromium-mv3': [flags, mv3Base],
'firefox-mv2': [flags, mv2Base, (manifest: ManifestV2) => manifest.permissions!.push('tabs')],
Expand All @@ -62,10 +62,23 @@ function prepareAllManifest(flags: NormalizedFlags, computedFlags: ComputedFlags
mv3Base,
(manifest: ManifestV3) => {
manifest.host_permissions = (manifest as any).optional_host_permissions
delete (manifest as any).optional_host_permissions
manifest.background = { page: 'background.html' }
manifest.web_accessible_resources?.forEach(
(x) => typeof x === 'object' && delete (x as any).use_dynamic_url,
)
delete manifest.key
},
],
'safari-mv3': [
flags,
mv3Base,
(manifest: ManifestV3) => {
manifest.host_permissions = (manifest as any).optional_host_permissions
delete (manifest as any).optional_host_permissions
delete manifest.key
},
],
'safari-mv3': [flags, mv3Base],
}
const manifest = new Map<NormalizedFlags['manifestFile'], ManifestV2 | ManifestV3>()
for (const fileName in manifestFlags) {
Expand Down
11 changes: 7 additions & 4 deletions packages/mask/background/services/site-adaptors/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Sniffings } from '@masknet/shared-base'
import { maskSDK_URL, injectUserScriptMV2, evaluateContentScript } from '../../utils/injectScript.js'

export async function attachMaskSDKToCurrentActivePage(): Promise<boolean> {
Expand All @@ -17,12 +18,14 @@ async function attachMaskSDK2() {
})
}
async function attachMaskSDK3(id: number) {
const [{ error }] = await browser.scripting.executeScript({
// TODO: Firefox MV3
const target = {
target: { tabId: id },
files: [maskSDK_URL],
// @ts-expect-error Chrome API
world: 'MAIN',
})
world: 'MAIN' as any,
}
if (Sniffings.is_firefox) delete target.world
const [{ error }] = await browser.scripting.executeScript(target)
if (error) throw error
}
export async function developmentMaskSDKReload(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function prepareMainWorldScript(name: string, matches: string[], url: string): S
runAt: 'document_start',
matches,
}
if (Sniffings.is_firefox) delete (result as any).world
return [result]
}

Expand All @@ -62,6 +63,10 @@ async function prepareContentScript(matches: string[]): Promise<Scripting.Regist
runAt: 'document_idle',
matches,
}
if (globalThis.navigator?.userAgent.includes('Firefox')) return [xrayScript, content]
if (Sniffings.is_firefox) {
delete (xrayScript as any).world
delete (content as any).world
return [xrayScript, content]
}
return [content]
}
9 changes: 6 additions & 3 deletions packages/mask/background/utils/injectScript.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Sniffings } from '@masknet/shared-base'
import { memoize } from 'lodash-es'

export const injectedScriptURL = '/js/injected-script.js'
Expand All @@ -12,11 +13,13 @@ export async function evaluateContentScript(tabId: number | undefined, frameId?:
tabId = activeTab[0].id
}
if (!tabId) return
await browser.scripting.executeScript({
const script = {
target: { tabId, frameIds: frameId ? [frameId] : undefined },
files: await fetchInjectContentScriptList(),
world: 'ISOLATED',
})
world: 'ISOLATED' as any,
}
if (Sniffings.is_firefox) delete script.world
await browser.scripting.executeScript(script)
} else {
for (const script of await fetchInjectContentScriptList()) {
await browser.tabs.executeScript(tabId, {
Expand Down
4 changes: 2 additions & 2 deletions packages/scripts/src/extension/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const ciBuild: TaskFunction = series(
zipTo('MaskNetwork.chromium.zip', ManifestFile.ChromiumMV2),
zipTo('MaskNetwork.firefox.zip', ManifestFile.FirefoxMV2, true),
zipTo('MaskNetwork.firefox-mv3.zip', ManifestFile.FirefoxMV3, true),
zipTo('MaskNetwork.chromium-beta.zip', ManifestFile.ChromiumBetaMV2),
zipTo('MaskNetwork.chromium-beta.zip', ManifestFile.ChromiumBetaMV3),
zipTo('MaskNetwork.chromium-mv3.zip', ManifestFile.ChromiumMV3),
)
export const buildChrome: TaskFunction = series(
Expand All @@ -33,7 +33,7 @@ function zipTo(
): TaskFunction {
const f: TaskFunction = async () => {
await copyFile(new URL(`manifest-${withManifestFile}.json`, BUILD_PATH), new URL('manifest.json', BUILD_PATH))
if (!reproducible && withManifestFile === ManifestFile.ChromiumBetaMV2) {
if (!reproducible && withManifestFile === ManifestFile.ChromiumBetaMV3) {
await copyFile(new URL('build-info-beta.json', BUILD_PATH), new URL('build-info.json', BUILD_PATH))
}
const { cmd } = await import('web-ext')
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/src/extension/dotenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function parseManifest(manifest: '2' | '3' | 2 | 3 | undefined | Manifest
if (manifest === 2 || manifest === '2') return ManifestFile.ChromiumMV2
if (manifest === 3 || manifest === '3') return ManifestFile.ChromiumMV3
if (typeof manifest === 'string') return manifest
return ManifestFile.ChromiumMV2
return ManifestFile.ChromiumMV3
}
function parseBoolean(val: string | undefined) {
if (val === undefined) return undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/scripts/src/extension/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function buildExtensionFlag(name: string, args: BuildFlagsExtended): Task
return f
}
export const buildBaseExtension: TaskFunction = buildExtensionFlag('default', {
manifestFile: ManifestFile.ChromiumMV2,
manifestFile: ManifestFile.ChromiumMV3,
channel: 'stable',
mode: 'production',
})
Expand All @@ -40,7 +40,7 @@ export async function extensionWatch(f: Function | BuildFlagsExtended) {
if (typeof f === 'function')
return awaitChildProcess(
webpack({
manifestFile: ManifestFile.ChromiumMV2,
manifestFile: ManifestFile.ChromiumMV3,
channel: 'stable',
mode: 'development',
}),
Expand Down

0 comments on commit deaf19a

Please sign in to comment.