Electron学习 第九章嵌入网页

electron嵌入网页


一、创建项目

1. 创建mian.js

代码如下(示例):

// main.js
const electron = require("electron")
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const globalShortcut = electron.globalShortcut

// 声明要打开的窗口
let mainWindow = null

app.on("ready",()=>{
    mainWindow = new BrowserWindow({
        width:800,
        height:800,
        webPreferences:{
            nodeIntegration:true, //开启node接口
            contextIsolation:false,
            webSecurity: false,
        }
    });

    // 打开新窗口 BrosweWindow 初始化
    require('@electron/remote/main').initialize();
    require("@electron/remote/main").enable(mainWindow.webContents);

    // 打开调试工具
    mainWindow.webContents.openDevTools();
    // mainWindow.loadFile("index.html");
    mainWindow.loadFile("./html/index3.html"); // 在浏览器打开网页


    // 嵌入网页
    // BrowserView 主线程模块,在主进程中使用
    let view = new BrowserView()
    mainWindow.setBrowserView(view);
    view.setBounds({
        x:0,
        y:120,
        width:400,
        height:400
    })    
    view.setBackgroundColor("#F5F5F5");

})


// 当所有窗口被关闭了,退出。
app.on('window-all-closed', function() {
    if (process.platform != 'darwin') {
      app.quit();
    }
});


2. 创建页面 html

index.html
代码如下(示例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>内嵌网页</title>
</head>
<body>
    <webview id="foo" src="https://www.github.com/" style="display:inline-flex; width:640px; height:480px"></webview>


</body>
</html>

二、验证代码

1. 运行项目

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值