gpt4 book ai didi

node.js - 如何在 Electron 版的Webview中创建上下文菜单

转载 作者:行者123 更新时间:2023-12-03 12:37:52 25 4
gpt4 key购买 nike

我正在开发一个小型 Electron 应用程序,但似乎遇到了障碍。我的应用程序中有一个webview加载到www.google.com。 Web View 可以很好地加载,但是当我右键单击Web View 中的任何位置时,都不会创建上下文菜单,单击页面的任何部分(不是Web View )都会导致成功创建上下文菜单。我正在使用 Electron 内容菜单创建菜单,并且阅读了git存储库中包含的文档:https://github.com/sindresorhus/electron-context-menu#readme,但没有任何值(value)。任何帮助将不胜感激,因为我确实需要上下文菜单出现在我的Web View 中。
Main.js

// Modules to control application life and create native browser window
const {app, BrowserWindow, Menu, shell} = require('electron')
const path = require('path')
var holder;
var checkmax = 0;

function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 1919,
height: 1079,
frame: true,
webPreferences: {
webviewTag: true,
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true

}
})

// and load the index.html of the app.
mainWindow.loadFile('index.html');
holder = mainWindow;


// Open the DevTools.
// mainWindow.webContents.openDevTools()
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()

app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})

//testing context
const contextMenu = require('electron-context-menu');

contextMenu({
prepend: (defaultActions, params, browserWindow) => [
{
label: 'Rainbow',
// Only show it when right-clicking images
visible: params.mediaType === 'image'
},
{
label: 'Search Google for “{selection}”',
// Only show it when right-clicking text
visible: params.selectionText.trim().length > 0,
click: () => {
shell.openExternal(`https://google.com/search?q=${encodeURIComponent(params.selectionText)}`);
}
}
]
});
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body style="background-color:red;">
<webview style="position: absolute; top: 10vh; left:0; width:100vw; height: 90vh;" src="https://www.google.com/"></webview>
</body>
</html>

最佳答案

经过一番尝试和错误后,我发现了这一点,然后我想到了这一点,它适用于初始化后页面内所有生成的Web View 。
这在Main.js中

var menu = new Menu();

//Basic Menu For Testing
menu.append(new MenuItem({ label: 'MenuItem1', click: function() { console.log("YES");
} }));
menu.append(new MenuItem({ type: 'separator' }));
menu.append(new MenuItem({ label: 'MenuItem2', type: 'checkbox', checked: true }));
app.on("web-contents-created", (...[/* event */, webContents]) => {

//Webview is being shown here as a window type
console.log(webContents.getType())
webContents.on("context-menu", (event, click) => {
event.preventDefault();
console.log(webContents.getType())
menu.popup(webContents);
}, false);
});

关于node.js - 如何在 Electron 版的Webview中创建上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65728995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com