Skip to content

Commit

Permalink
feat: support encrypt preload.js
Browse files Browse the repository at this point in the history
  • Loading branch information
dusionlike committed Aug 10, 2022
1 parent 177865b commit 8207267
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion playground/electron-builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"directories": {
"output": "release/${version}"
},
"files": ["main.js", "renderer"],
"files": ["main.js", "preload.js", "renderer"],
"mac": {
"artifactName": "${productName}_${version}.${ext}",
"target": ["dmg"]
Expand Down
5 changes: 5 additions & 0 deletions playground/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const path = require('path')
const { app, BrowserWindow } = require('electron')

function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
sandbox: false,
preload: path.join(__dirname, 'preload.js'),
},
})

if (!app.isPackaged) {
Expand Down
11 changes: 11 additions & 0 deletions playground/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
window.addEventListener('DOMContentLoaded', () => {
const el = document.createElement('p')
el.innerHTML = 'preload.js is encrypted'
document.body.append(el)

for (const type of ['chrome', 'node', 'electron']) {
const el = document.createElement('p')
el.innerHTML = `${type}-version: ${process.versions[type]}`
document.body.append(el)
}
})
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ exports.default = async function (context: AfterPackContext) {
'utf-8'
)

// 将renderer preload.js加密
const rendererPreloadJsPath = path.join(mainDir, 'preload.js')
if (fs.existsSync(rendererPreloadJsPath)) {
const rendererPreloadJsCPath = path.join(mainDir, 'preload-c.jsc')
await compileToBytenode(rendererPreloadJsPath, rendererPreloadJsCPath)
await fs.promises.writeFile(
rendererPreloadJsPath,
`"use strict";require('bytenode');require('v8').setFlagsFromString('--no-lazy');require('./preload-c.jsc');`,
'utf-8'
)
}

const rendererDir = path.join(mainDir, 'renderer')

// 加密渲染进程
Expand All @@ -64,7 +76,7 @@ exports.default = async function (context: AfterPackContext) {
'utf-8'
)

await fs.promises.rm(tempAppDir, { recursive: true })
// await fs.promises.rm(tempAppDir, { recursive: true })
}

/**
Expand Down

0 comments on commit 8207267

Please sign in to comment.