feat: 新增包文件
This commit is contained in:
3
packages/electron-chrome-extensions/script/native-messaging-host/.gitignore
vendored
Normal file
3
packages/electron-chrome-extensions/script/native-messaging-host/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
crxtesthost
|
||||
crxtesthost.blob
|
||||
crxtesthost.exe
|
||||
106
packages/electron-chrome-extensions/script/native-messaging-host/build.js
Executable file
106
packages/electron-chrome-extensions/script/native-messaging-host/build.js
Executable file
@@ -0,0 +1,106 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { promises: fs } = require('node:fs')
|
||||
const path = require('node:path')
|
||||
const os = require('node:os')
|
||||
const util = require('node:util')
|
||||
const cp = require('node:child_process')
|
||||
const exec = util.promisify(cp.exec)
|
||||
|
||||
const basePath = 'script/native-messaging-host/'
|
||||
const outDir = path.join(__dirname, '.')
|
||||
const exeName = `crxtesthost${process.platform === 'win32' ? '.exe' : ''}`
|
||||
const seaBlobName = 'crxtesthost.blob'
|
||||
|
||||
async function createSEA() {
|
||||
await fs.rm(path.join(outDir, seaBlobName), { force: true })
|
||||
await fs.rm(path.join(outDir, exeName), { force: true })
|
||||
|
||||
await exec('node --experimental-sea-config sea-config.json', { cwd: outDir })
|
||||
await fs.cp(process.execPath, path.join(outDir, exeName))
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
await exec(`codesign --remove-signature ${exeName}`, { cwd: outDir })
|
||||
}
|
||||
|
||||
console.info(`Building ${exeName}…`)
|
||||
const buildCmd = [
|
||||
'npx postject',
|
||||
`${basePath}${exeName}`,
|
||||
'NODE_SEA_BLOB',
|
||||
`${basePath}${seaBlobName}`,
|
||||
'--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
|
||||
...(process.platform === 'darwin' ? ['--macho-segment-name NODE_SEA'] : []),
|
||||
]
|
||||
await exec(buildCmd.join(' '), { cwd: outDir })
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
await exec(`codesign --sign - ${exeName}`, { cwd: outDir })
|
||||
}
|
||||
}
|
||||
|
||||
async function installConfig(extensionIds) {
|
||||
console.info(`Installing config…`)
|
||||
|
||||
const hostName = 'com.crx.test'
|
||||
const manifest = {
|
||||
name: hostName,
|
||||
description: 'electron-chrome-extensions test',
|
||||
path: path.join(outDir, exeName),
|
||||
type: 'stdio',
|
||||
allowed_origins: extensionIds.map((id) => `chrome-extension://${id}/`),
|
||||
}
|
||||
|
||||
const writeManifest = async (manifestPath) => {
|
||||
await fs.mkdir(manifestPath, { recursive: true })
|
||||
const filePath = path.join(manifestPath, `${hostName}.json`)
|
||||
const data = Buffer.from(JSON.stringify(manifest, null, 2))
|
||||
await fs.writeFile(filePath, data)
|
||||
return filePath
|
||||
}
|
||||
|
||||
switch (process.platform) {
|
||||
case 'darwin': {
|
||||
const manifestDir = path.join(
|
||||
os.homedir(),
|
||||
'Library',
|
||||
'Application Support',
|
||||
'Electron',
|
||||
'NativeMessagingHosts',
|
||||
)
|
||||
await writeManifest(manifestDir)
|
||||
break
|
||||
}
|
||||
case 'win32': {
|
||||
const manifestDir = path.join(
|
||||
os.homedir(),
|
||||
'AppData',
|
||||
'Roaming',
|
||||
'Electron',
|
||||
'NativeMessagingHosts',
|
||||
)
|
||||
const manifestPath = await writeManifest(manifestDir)
|
||||
const registryKey = `HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\${hostName}`
|
||||
await exec(`reg add "${registryKey}" /ve /t REG_SZ /d "${manifestPath}" /f`, {
|
||||
stdio: 'inherit',
|
||||
})
|
||||
break
|
||||
}
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const extensionIdsArg = process.argv[2]
|
||||
if (!extensionIdsArg) {
|
||||
console.error('Must pass in csv of allowed extension IDs')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const extensionIds = extensionIdsArg.split(',')
|
||||
await createSEA()
|
||||
await installConfig(extensionIds)
|
||||
}
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,26 @@
|
||||
const fs = require('node:fs')
|
||||
|
||||
function readMessage() {
|
||||
let buffer = Buffer.alloc(4)
|
||||
if (fs.readSync(0, buffer, 0, 4, null) !== 4) {
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
let messageLength = buffer.readUInt32LE(0)
|
||||
let messageBuffer = Buffer.alloc(messageLength)
|
||||
fs.readSync(0, messageBuffer, 0, messageLength, null)
|
||||
|
||||
return JSON.parse(messageBuffer.toString())
|
||||
}
|
||||
|
||||
function sendMessage(message) {
|
||||
let json = JSON.stringify(message)
|
||||
let buffer = Buffer.alloc(4 + json.length)
|
||||
buffer.writeUInt32LE(json.length, 0)
|
||||
buffer.write(json, 4)
|
||||
|
||||
fs.writeSync(1, buffer)
|
||||
}
|
||||
|
||||
const message = readMessage()
|
||||
sendMessage(message)
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"main": "main.js",
|
||||
"output": "crxtesthost.blob"
|
||||
}
|
||||
Reference in New Issue
Block a user