久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在電子中加載本地 html 文件的簡單方法

Easy way to load local html file in electron(在電子中加載本地 html 文件的簡單方法)
本文介紹了在電子中加載本地 html 文件的簡單方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我一直在嘗試將小型 webapp 轉換為電子應用程序.它工作得很好,除了我需要將一堆文件(.html)加載到主 DOM 中.在 webapp 中,我只是使用了 $.get,但是在 electron 中我該怎么做呢?我嘗試查看 DOC,但除了 IPC 管道(我不太明白)之外,我找不到簡單的方法.

I've been trying to convert a small webapp into an electron application. It's working perfectly, except I need to load a bunch of files (.html) into the main DOM. In the webapp, I just used $.get, but how can I do it in electron? I try looking at the DOC but I cannot find an easy way to do that, beside an IPC pipe (and I don't quite get it).

誰能指出我正確的方向?

Could anyone point me to the right direction?

編輯

我會在這里澄清.我有一個啟動 BrowserWindow 的主進程

I'll clarify here. I have a main process that start a BrowserWindow

mainWindow = new BrowserWindow({width: 800, height: 600})

然后,在通過 <script> 標簽導入的 js 文件中,我想在對話框中加載并附加一些文件:

and then, in a js file imported via a <script> tag, I want to get load and attach some file inside a dialog:

$('.dialog').load('pages/hello.html', {})

親切的問候

推薦答案

在 Electron 中你可以使用 fs.readFile

In Electron you can do it with fs.readFile

所以:

const fs = require('fs');
const { promisify } = require('util');
const path = require('path');
const readFile = promisify(fs.readFile);

async function loadHTML(html){
    const render = await readFile(path.join(__dirname, html), 'utf-8');
    const parser = new DOMParser();
    const childrenArray = parser.parseFromString(render,'text/html').querySelector('body').childNodes;
    const frag = document.createDocumentFragment();
    childrenArray.forEach(item => {
        frag.appendChild(item);
    });
    document.body.appendChild(frag);
};

loadHTML('/path/to/my/index.html');

如果我沒有錯字,它應該可以工作.它將文件作為字符串讀取,因此您需要使用 DOMParser 解析此字符串.

If I don't have a Typo, it should work. it reads the file as a string so you need to parse this String with the DOMParser.

這篇關于在電子中加載本地 html 文件的簡單方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 福利片在线 | 日韩毛片播放 | 韩日视频在线观看 | 成人亚洲性情网站www在线观看 | 久久精品亚洲精品国产欧美 | 午夜精品久久久久久久久久久久 | 亚洲福利一区二区 | 久久久精品 | www.男人天堂.com | 亚洲一区二区精品视频在线观看 | 激情欧美一区二区三区 | 香蕉视频久久久 | 国产女人与拘做视频免费 | 国产黄色免费网站 | 欧美精品一区二区三区四区 | 久久高清 | 亚洲一级黄色 | 99久久久久国产精品免费 | 国产高清精品在线 | 日韩在线播放第一页 | 国产午夜精品久久久久 | 日韩在线视频免费观看 | 欧美久久视频 | 2021天天干夜夜爽 | 欧美一区二区三区 | 毛片毛片毛片毛片毛片 | 日韩a在线| 中文字幕精 | 久久久久久成人 | 亚洲精品二区 | 亚洲激情综合网 | 日韩一区二区三区视频 | 精品中文在线 | 天堂一区 | 日韩成人免费av | 欧美精品一区二区三区在线 | 国产成人精品免高潮在线观看 | 欧美片网站免费 | 在线中文字幕视频 | 久久精品国产一区二区电影 | 国产精品看片 |