forked from cba85/electron-webview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
111 lines (93 loc) · 2.76 KB
/
main.js
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
99
100
101
102
103
104
105
106
107
108
109
// Electron
const { app, Menu,webContents , desktopCapturer , BrowserWindow} = require("electron");
const remoteMain = require("@electron/remote/main");
const prompt = require('electron-prompt');
remoteMain.initialize();
const electron = require('electron');
const ipcMain = electron.ipcMain;
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.allowRendererProcessReuse = true;
app.on('web-contents-created', function (webContentsCreatedEvent, contents) {
if (contents.getType() === 'webview') {
contents.on('new-window', function (newWindowEvent, url) {
console.log('block');
//newWindowEvent.preventDefault();
mainWindow.loadURL(url);
});
}
});
/*
app.on('browser-window-created', (_, window) => {
console.log("browser-window-created is called.",window.webContents);
require("@electron/remote/main").enable(window.webContents)
})
*/
app.on("ready", () => {
// Main window
const window = require("./src/window");
mainWindow = window.createBrowserWindow(app);
remoteMain.enable(mainWindow.webContents);
var self = mainWindow;
mainWindow.webContents.on('did-attach-webview', () => {
console.log("HERE:");
const all = webContents.getAllWebContents();
all.forEach((item) => {
remoteMain.enable(item);
});
});
// Option 1: Uses Webtag and load a custom html file with external content
mainWindow.loadURL(`file://${__dirname}/index.html`);
// Option 2: Load directly an URL if you don't need interface customization
//mainWindow.loadURL("https://github.com");
// Option 3: Uses BrowserView to load an URL
//const view = require("./src/view");
//view.createBrowserView(mainWindow);
// Display Dev Tools
mainWindow.openDevTools();
// Menu (for standard keyboard shortcuts)
const menu = require("./src/menu");
const template = menu.createTemplate(app.name);
const builtMenu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(builtMenu);
ipcMain.on('select-media', (eventRet, arg)=>
{
let selectedSource;
desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
for (const source of sources)
{
selectedSource = source;
}
eventRet.returnValue = selectedSource;
})
})
ipcMain.on('prompt', (eventRet, arg)=>
{
prompt({
title: arg.title,
width: 300,
height: 200,
label: 'Message',
alwaysOnTop:true,
resizable:false,
type: 'input'
})
.then((r) => {
if(r === null)
{
eventRet.returnValue = "";
console.log('user cancelled');
}
else
{
eventRet.returnValue = r;
console.log('Return is:', r);
}
})
.catch(console.log);
})
});
// Quit when all windows are closed.
app.on("window-all-closed", () => {
app.quit();
});