打开一个文件,点击electron.js中触摸栏上的按钮



我在加载文件的程序中实现了一个触摸条按钮。我的问题是:当我按下触摸条按钮时,页面加载后,我如何点击网站上的特定按钮?这是我的main.js文件:

const { app, BrowserWindow, TouchBar } = require('electron')
const { TouchBarButton } = TouchBar
const path = require('path')
let window
function createWindow() {
const win = new BrowserWindow({
width: 1366,
height: 758,
minWidth: 1300,
minHeight: 700,
icon: __dirname + 'logoSquare.icns',
webPreferences: {
//preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile('login.html')
win.maximize()
return win
}
app.whenReady().then(() => {
window = createWindow()
const button = new TouchBarButton({
label: `🛫 Add Flight`,
accessibilityLabel: 'Add Flight',
backgroundColor: '#a20021',
click: () => {
window.loadFile('./flights.html')
},
});
const touchBar = new TouchBar({
items: [
button,
],
})
window.setTouchBar(touchBar);
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})

我刚开始学习electron.js,还找不到解决方案。

我找到了另一种方法来解决这个问题。我发送了一个查询,并用JavaScript获得了该信息。这是负责发送查询的main.js文件:

const button = new TouchBarButton({
label: `🛫 Add Flight`,
accessibilityLabel: 'Add Flight',
backgroundColor: '#3a2e39',
click: () => {
//window.loadFile('./flights.html?addflight=1')
window.loadURL('file://'+__dirname+'/flights.html?addflight=1')
},
})

这是负责用JavaScript:接收flights.html文件中数据的代码

function addFlightRedir() {
if (location.search == '?addflight=1') {
addNewFlight(); // This is the code that I wanted to execute
}
}
addFlightRedir();

由于它可以与标准JS一起重复使用,现在我也可以将它实现到程序的其他区域(独立于触摸条按钮(。

最新更新