惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Palo Alto Networks Blog
P
Privacy & Cybersecurity Law Blog
H
Hacker News: Front Page
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
IT之家
IT之家
B
Blog
Google DeepMind News
Google DeepMind News
U
Unit 42
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
美团技术团队
Y
Y Combinator Blog
爱范儿
爱范儿
L
Lohrmann on Cybersecurity
O
OpenAI News
酷 壳 – CoolShell
酷 壳 – CoolShell
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
T
Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
TaoSecurity Blog
TaoSecurity Blog
Simon Willison's Weblog
Simon Willison's Weblog
Security Archives - TechRepublic
Security Archives - TechRepublic
M
MIT News - Artificial intelligence
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tenable Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
小众软件
小众软件
罗磊的独立博客
B
Blog RSS Feed
S
SegmentFault 最新的问题
L
LINUX DO - 热门话题
Scott Helme
Scott Helme
G
GRAHAM CLULEY
Martin Fowler
Martin Fowler
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed
Security Latest
Security Latest

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
2019-01-23 · via 轶哥博客

使用场景

Electron 内置 ChromiumNode.js,为了提升用户体验,通常 Electron 封装的前端静态文件存储在客户端本地。但总有一些特殊情况,会使用到部分远程页面。

比如微信扫码登录,需要远程访问微信的登录站点,获得扫码结果后跳转到我们自己的服务器,然后通知到主进程来关闭对应的渲染进程。目前这个问题有更高效的解决方案,详见博文《无刷新微信扫码登陆解决方案》

再比如,部分页面还没有实现前后端分离,为了产品尽快上线,也需要嵌入远程URL。

直接在Electron中访问远程地址是有风险的,控制远程地址切回本地页面也是较为繁琐的。

以上需求可以使用webview标签实现。这是Electron为我们提供的一个特殊组件,类似于iframe但跟iframe不同,确保了应用的安全性。类似于AndroidioswebviewCrosswalk,Electron中的webview也支持和渲染进程相互通讯。

具体实现示例

载入父级页面的时候,将此webview悄悄放入。这样就实现在后台加载webview里面链接的内容了。

// 示意代码 (Vue)
<template v-for="(item, index) in list">
  <webview :key="index" :id="'webview' + index" :src="'https://' + item" class="hidden"></webview>
</template>
webview.hidden {
  width: 0px;
  height: 0px;
  flex: 0 1;
}

webview.show {
  display: flex !important;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 99999999999;
  right: 0;
  bottom: 0;
}

webview.backhome {
  position: fixed;
  top: 0;
  left: 0;
  width: 300px;
  height: 160px;
  z-index: 999999999999;
}

对应的显示和隐藏事件:

// 示意代码
buttomClick (id) {
  this.nowWebviewId = id
  document.getElementById(id).classList.remove('hidden');
  document.getElementById(id).classList.add('show');
}

backToHome (id) {
  document.getElementById(id).classList.remove('show');
  document.getElementById(id).classList.add('hidden');
  this.nowWebviewId = null
}

当用户点击对应按钮的时候,页面可能已经加载完毕,用户体验极佳。