學(xué)習(xí)electron第六章嵌入網(wǎng)頁和打開子窗口

electron 版本號(hào) v15.3.0

嵌入網(wǎng)頁

第一步

創(chuàng)建修改main.js

var electron = require('electron')

const {BrowserView} = require('electron')

var app = electron.app

var BrowserWindow = electron.BrowserWindow

var mainWindow = null

app.on('ready',()=>{
    mainWindow = new BrowserWindow({
        width:1200,
        height:1200,
        webPreferences: {
            nodeIntegration: true, //設(shè)置開啟nodejs環(huán)境
            contextIsolation: false,
        }
    })
    require('@electron/remote/main').initialize()
    require('@electron/remote/main').enable(mainWindow.webContents)
    require('./main/Menu.js')
    mainWindow.loadFile('demo3.html')
    mainWindow.webContents.openDevTools()

    // 使用windowsView 打開子窗口
    const view = new BrowserView()
    mainWindow.setBrowserView(view)
    view.setBounds({
        x:0,
        y:120,
        width:800,
        height:800
    })
    view.webContents.loadURL('http://www.itdecent.cn/u/e9a31a3c3bdb')

    mainWindow.on('closed',()=>{
        mainWindow = null
    })
})

第二步

驗(yàn)證測(cè)試
輸入

electron .

可以展示以下內(nèi)容代表完成

image.png

紅色為本次修改或新增內(nèi)容

image.png
image.png

問題

我自己的本地環(huán)境,如果main.js不加上
mainWindow.webContents.openDevTools() // 啟動(dòng)打開調(diào)試器
是不會(huì)展示嵌入頁面的,暫時(shí)也沒找到原因,這個(gè)坑先留下,以后知道如何解決了再來更新,解決方法

打開子窗口

第一步

修改demo3.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>Document</title>
</head>
<body>
    <h1>
        <a id="aHref" href="http://www.itdecent.cn/u/e9a31a3c3bdb">簡書地址</a>
    </h1>
    <button id = "mybtn">打開一個(gè)子窗口</button>
    <script src="./render/demo3.js"></script>
    
</body>
</html>

第二步

修改demo3.js

const {shell} = require('electron')

var aHref = document.querySelector('#aHref')

aHref.onclick = function(e){
    e.preventDefault()
    var href = this.getAttribute('href')
    shell.openExternal(href)
}

var mybtn = document.querySelector('#mybtn')
mybtn.onclick = function(){
    window.open('http://www.itdecent.cn/u/e9a31a3c3bdb')
}

第三步

測(cè)試

electron .

執(zhí)行后展示以下內(nèi)容則代表完成

image.png

紅色為本次修改或新增的內(nèi)容

image.png
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者,不喜歡去冒險(xiǎn),但是人生放棄了冒險(xiǎn),也就放棄了無數(shù)的可能。 ...
    yichen大刀閱讀 7,648評(píng)論 0 4
  • 公元:2019年11月28日19時(shí)42分農(nóng)歷:二零一九年 十一月 初三日 戌時(shí)干支:己亥乙亥己巳甲戌當(dāng)月節(jié)氣:立冬...
    石放閱讀 7,408評(píng)論 0 2
  • 今天上午陪老媽看病,下午健身房跑步,晚上想想今天還沒有斷舍離,馬上做,衣架和旁邊的的布衣架,一看亂亂,又想想自己是...
    影子3623253閱讀 3,061評(píng)論 3 8

友情鏈接更多精彩內(nèi)容