久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 成人一区二区三区在线观看 | 亚洲精品一二区 | 日本一道本 | 国产精品免费一区二区三区四区 | 亚洲视频免费一区 | 操操日 | 91中文字幕在线 | 中文字幕第十一页 | 日韩aⅴ在线观看 | 激情一区二区三区 | 国产精品久久久久久久7777 | 亚洲成人一区二区在线 | 午夜国产| 久久国产精品一区 | 久久国产精品无码网站 | 国产一区二区三区视频 | 日本不卡一区二区三区 | 国产欧美性成人精品午夜 | 亚洲精品久久久久中文字幕欢迎你 | 网站国产| 亚洲在线视频 | 在线天堂免费中文字幕视频 | 久久www免费人成看片高清 | 精品国产一区二区 | 激情综合五月 | 日韩电影一区二区三区 | 亚洲一区视频在线 | 欧美精品v国产精品v日韩精品 | 欧美一区二区三区视频 | 成人羞羞国产免费视频 | 精品国产一区久久 | 日韩av.com | 麻豆国产一区二区三区四区 | 成人日韩 | 久久av一区二区三区 | 日日干夜夜干 | 久久最新精品 | 国产日韩欧美精品一区二区三区 | 一区二区三区四区在线播放 | 亚洲一av| 偷拍亚洲色图 |