-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
98 lines (77 loc) · 2.4 KB
/
example.js
File metadata and controls
98 lines (77 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const {
session: Session,
app,
protocol,
BrowserWindow,
Menu
} = require('electron')
const { ExtendedExtensions } = require('./')
const path = require('path')
const EXTENSION_PATH = path.join(__dirname, 'example-extension')
const PROTOCOL_SCHEME = 'example'
const PROTOCOL_PRIVILEGES = {
standard: true,
secure: true,
allowServiceWorkers: true,
supportFetchAPI: true,
corsEnabled: true,
stream: true
}
run()
async function run () {
protocol.registerSchemesAsPrivileged([{
scheme: PROTOCOL_SCHEME,
privileges: PROTOCOL_PRIVILEGES
}])
await app.whenReady()
const session = Session.fromPartition('persist:example')
const browserPrefs = {
webPreferences: {
session,
contextIsolation: true
}
}
function createWindow ({ url, openerTabId }) {
const window = new BrowserWindow(browserPrefs)
window.loadURL(url)
return window.webContents
}
session.protocol.registerStringProtocol(PROTOCOL_SCHEME, async (request, sendResponse) => {
sendResponse({
statusCode: 200,
data: `<pre>Example: ${JSON.stringify(request, null, '\t')}</pre>`
})
})
const extensions = new ExtendedExtensions(session, {
onCreateTab: createWindow
})
extensions.browserActions.on('change', (actions) => {
console.log('new browser action list', actions)
})
session.on('extension-loaded', (event, extension) => {
console.log({ extension })
})
app.on('web-contents-created', (event, webContents) => {
webContents.openDevTools()
webContents.on('context-menu', async (event, params) => {
const items = extensions.contextMenus.getForEvent(webContents, event, params)
const actions = extensions.browserActions.list(webContents.id).map(({ title, extensionId }) => {
return {
label: title,
click: () => {
extensions.browserActions.click(extensionId, webContents.id)
}
}
})
const backgroundPage = await extensions.getBackgroundPage(extension.id)
console.log({ backgroundPage })
const allItems = [...items, { type: 'separator' }, ...actions]
Menu.buildFromTemplate(allItems).popup()
})
})
const extension = await extensions.loadExtension(EXTENSION_PATH)
console.log(extension)
createWindow({ url: 'https://mauve.moe' })
// Test if we can interact with custom protocols (not yet)
createWindow({ url: 'example://example/helloworld.html' })
}