Electron包装网站的问题

时间:2021-08-26 11:47:22   收藏:0   阅读:35

原文链接

原文链接

Preface

最近尝试了很多不错的在线工具,只是每次都要进入网站,有点麻烦,于是想到之前了解过的electron,尝试一下打包成本地应用。

Contents

1.下载所有源文件

通过开发者工具,‘copy all as Node.js fetch‘,然后配合 node-fetch 库,将需要用到的资源下载到本地:

const fs = require(‘fs‘)
const fetch = require(‘node-fetch‘);

function checkAndWrite(filepath,res) {
   console.log("ok , here it is:",filepath);
   //todo create directory loop  
   const dirpath = filepath.substr(0,filepath.lastIndexOf("/"));
   console.log(dirpath);
   if(!fs.existsSync(dirpath)){
       fs.mkdir(dirpath,{recursive:true},(err)=>{
           if(err != null) {
               console.log("mkdir err:",err);
               return;
           }
           const dest = fs.createWriteStream(filepath);
           res.body.pipe(dest);
       })
       return;
   }

   const dest = fs.createWriteStream(filepath);
   res.body.pipe(dest);
}
// 这个是主页
fetch("https://material.io/resources/color/", {
 "headers": {
   "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
   "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
   "cache-control": "no-cache",
   "pragma": "no-cache",
   "sec-ch-ua": "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\"",
   "sec-ch-ua-mobile": "?0",
   "sec-fetch-dest": "document",
   "sec-fetch-mode": "navigate",
   "sec-fetch-site": "none",
   "sec-fetch-user": "?1",
   "upgrade-insecure-requests": "1",
   "cookie": "_ga=GA1.2.682063148.1629876102; _gid=GA1.2.1595024389.1629876102"
 },
 "referrerPolicy": "strict-origin-when-cross-origin",
 "body": null,
 "method": "GET",
 "mode": "cors"
}).then(res=>{
   checkAndWrite(‘./html/main.html‘,res)
})

// 这个是其中的一个资源
fetch("https://material.io/resources/color/styles/vendor-bab328c105.css", {
 "headers": {
   "accept": "text/css,*/*;q=0.1",
   "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
   "cache-control": "no-cache",
   "pragma": "no-cache",
   "sec-ch-ua": "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\"",
   "sec-ch-ua-mobile": "?0",
   "sec-fetch-dest": "style",
   "sec-fetch-mode": "no-cors",
   "sec-fetch-site": "same-origin",
   "cookie": "_ga=GA1.2.682063148.1629876102; _gid=GA1.2.1595024389.1629876102; _gat=1"
 },
 "referrer": "https://material.io/resources/color/",
 "referrerPolicy": "strict-origin-when-cross-origin",
 "body": null,
 "method": "GET",
 "mode": "cors"
}).then(res=>{
   checkAndWrite(‘./html/styles/vendor-bab328c105.css‘,res)
})

2.在electron中加载

mainWindow = new BrowserWindow({width: 1000, height: 800, webPreferences:{webSecurity:false}})

mainWindow.loadURL(url.format({
    pathname:path.join(__dirname,"/html/main.html"),
    protocol: "file",
    slashes: true
}))

3. 注意的点

效果图

技术分享图片
技术分享图片

源代码

原文:https://www.cnblogs.com/adoontheway/p/15188084.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!