gpt4 book ai didi

javascript - 如何在浏览器窗口中加载javascript?

转载 作者:太空宇宙 更新时间:2023-11-04 03:26:56 24 4
gpt4 key购买 nike

这是在 Electron 中创建窗口的标准方法(来自文档);

let win;
function createWindow(){
// Create the browser window.
win = new BrowserWindow({width: 680, height: 420, frame: false, transparent: true, show: false});

// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'test.html'),
protocol: 'file:',
slashes: true
}));

win.once('ready-to-show', function() {
win.show();
});


// Open the DevTools.
// win.webContents.openDevTools()

// Emitted when the window is closed.
win.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
})

}

工作正常,但是如何在窗口上下文中加载一堆 javascript 文件,例如 jQuery 等,而不使用 <script> test.html 中的标签?

我不想使用脚本标签的原因是因为我可以有很多 .js 文件和 .html 文件,并且我不想每次发生变化时都更新 html。我宁愿将它们放在创建窗口的函数中。

最佳答案

查看文档,BrowserWindow 有一个 webContents property ,这是 WebContents具有 executeJavascript method 的实例它接受要执行的 JavaScript。

因此您可以使用 net module 来阅读脚本,然后通过 executeJavascript 执行它。

关于javascript - 如何在浏览器窗口中加载javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43425241/

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