electron基础篇(模块)

用例君
• 阅读 1472

shell(打开网页)

1. electron5.x中建议使用iframe替代webview

2. electronic的webview标签基于Chromium的webview,后者正在经历巨大的架构变化。这将影响webview的稳定性,包括呈现、导航和事件路由。我们目前建议不使用webview标签,并考虑其他替代方案,如iframe、electronic的BrowserView或完全避免嵌入内容的体系结构。
调用shell
var { shell } = require('electron')

var aDom = document.querySelector('#adom')
aDom.onclick = function(e) {
  e.preventDefault()
  var href = this.getAttribute('href')
  shell.openExternal(href)
}
主进程通知渲染进程打开

主进程


function openWebview(url) {
  var win = BrowserWindow.getFocusedWindow()

  win.webContents.send('openWebview', url)
}

渲染进程

var { ipcRenderer } = require('electron')

ipcRenderer.on('openWebview', function(event, data) {
  myWebviewDom.src = data
})

dialog(弹框)

消息提示框

var {remote}=require('electron');

var errorDom=document.querySelector('#error');
errorDom.onclick=function(){ 
    remote.dialog.showErrorBox('警告','操作有误');

}

确认框

mesageBoxDom.onclick=function(){
    remote.dialog.showMessageBox({
        type:'info',
        title:'提示信息',
        message:'内容',
        buttons:['ok','no']
    },function(index){
        console.log(index)
    })
}

打开文件/文件夹框

openDialogDom.onclick = function() {
  remote.dialog.showOpenDialog(
    {
      properties: ['openDirectory', 'openFile']
      // properties: ['openFile']
    },
    function(data) {
      console.log(data)
      //["C:\Users\Administrator\Desktop\新建文件夹\js\index.js"]
    }
  )
}

保存文件框

saveDialogDom.onclick = function() {
  remote.dialog.showSaveDialog(
    {
      title: 'save file',
      defaultPath: 'aaa.jpg',
      filters: [
        { name: 'Images', extensions: ['jpg', 'png', 'gif'] },
        { name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
        { name: 'Custom File Type', extensions: ['as'] },
        { name: 'All Files', extensions: ['*'] }
      ]
    },
    function(path) {
      console.log(path)

      // C:\Users\Administrator\Desktop\新建文件夹\js\aaa.jpg

      //保存以后会打印保存的路径  , 但是不会实现真的保存功能  (具体保存什么数据可以写在nodejs里面)
    }
  )
}

系统托盘

var {Menu,Tray,BrowserWindow,app}=require('electron');

var path=require('path');


var iconTray=new Tray(path.join(__dirname,'../static/favicon2.ico'));



//绑定右键菜单

var trayMenu=Menu.buildFromTemplate([

    {

      label:'设置',
      click:function(){

        console.log('setting')
      }
    },
    {

        label:'升级',
        click:function(){
  
          console.log('update')
        }
      },
    {

        label:'退出',
        click:function(){
            if (process.platform !== 'darwin') {
                app.quit();
              }
        }
    }
]);

iconTray.setContextMenu(trayMenu);

iconTray.setToolTip('electron应用');



//实现点击关闭按钮让应用保存在托盘里面 ,双击托盘打开应用


var win=BrowserWindow.getFocusedWindow();


win.on('close',(e)=>{


        console.log(win.isFocused());

        if(!win.isFocused()){
                win=null;
        }else{
            e.preventDefault();  //阻止窗口的关闭事件
            win.hide();

        }
    
})




//监听任务栏图标的点击事件
iconTray.on('double-click',function(){
   
    win.show();
})





//闪烁图标

var count=0;

var timer=setInterval(function(){
    count++;

    if(count%2==0){

         iconTray.setImage(path.join(__dirname,'../static/favicon2.ico'))

    }else{

        iconTray.setImage(path.join(__dirname,'../static/empty.ico'))
    }

},500)

系统消息通知

openWinMessage.onclick = function() {
  var option = {
    title: 'electron 通知',
    body: 'electron跨平台软件开发教程更新了'
  }

  var myNotification = new window.Notification(option.title, option)

  myNotification.onclick = function() {
    console.log('点击了')
  }
}

监听网络断开,连接

window.addEventListener('online', function() {
    //
})
window.addEventListener('offline', function() {
    //
})

全局快捷键

var { globalShortcut, app } = require('electron')

app.on('ready', function() {
  //注册全局快捷键

  globalShortcut.register('ctrl+e', function() {
    console.log('ctrl+e')
  })

  globalShortcut.register('ctrl+d', function() {
    console.log('ctrl+d registed')
  })

  //检测快捷键是否注册功能

  console.log(globalShortcut.isRegistered('ctrl+e'))
})

app.on('will-quit', function() {
  //注销全局快捷键的监听

  globalShortcut.unregister('ctrl+e')

  globalShortcut.unregister('ctrl+d')
})
主进程引入
require('./main/globalShortcart.js')

//监听应用准备完成
app.on('ready', function() {

})

点赞
收藏
评论区
推荐文章
20pzqm 20pzqm
3年前
【electron】ipc模块使用
electron进程模型electron使用多进程架构与chromium类似,electron使用多进程架构。单一进程架构能够节省资源,然而浏览器经常遇到恶意代码,可能导致进程崩溃,浏览器的多个页签公用一个进程必然会一损俱损,因此多进程架构更适合些()。在开发electron的过程中,我们主要接触两个进程:主进程mainprocess与渲染进程re
关于小程序打开外部链接
一.首先,如果是个人小程序,就不用考虑打开外部链接了。这里需要使用webview组件,详细的请看https://developers.weixin.qq.com/miniprogram/dev/component/webview.html
待兔 待兔
4年前
Android WebView加载优化
1.前言最近几年关于原生WebView与H5混合开发的项目越来越多,这种开发带来了很多便利,但也会有一些缺点,比如说通过WebView加载H5会有一定的卡顿现象,会影响用户体验。下面本文就此问题一一展开讨论。2.场景根据日常需求一般是通过webView.loadUrl()方法加载指定的网页,其大概流程如下:(https://i
Wesley13 Wesley13
3年前
UIWebView背景透明的方法
1.首先UIWebView背景透明//setbackgroundtransparent,alsocansetitinnibfilewebView\_.backgroundColor  \UIColor clearColor\; webView\_.opaque  NO;2.隐藏拖拽webview时上下的两个有阴影效果的su
Stella981 Stella981
3年前
Android webview使用详解
1\.打开网页时不调用系统浏览器,而是在本WebView中显示:!复制代码(http://static.oschina.net/uploads/img/201603/18112202_IqHR.gif)mWebView.setWebViewClient(new WebViewClient(){      @Override 
Stella981 Stella981
3年前
APPIUM 小程序webview问题
小程序许多界面都是hybrid,有些webview页面用uiautomatorviewer查看不到元素,这里就要获取webview 的pagesource了1.环境:  需要确定appium\_chromedriver的版本和微信的webview版本对应:获取微信的webview版本信息,打开x5调试debugx5.qq.com在信息页面
Wesley13 Wesley13
3年前
JavaFX WebView概述,很强大,内置了类似Electron的功能
来自 MuraliBilla(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblogs.oracle.com%2Fauthor%2F451096495e164353a58cb47036af7724)JavaFX技术人员的主要成员在此博客中,我们将研究JavaFX如何渲
Stella981 Stella981
3年前
Flutter
在Flutter加载网页?也是有WebView的哦,和Android一样1.添加依赖dependencies:flutter\_webview\_plugin:^0.2.122.导入库import'import'package:flutter\_webview\_plugin/flutter\_webview\_plug
Stella981 Stella981
3年前
Electron webview完全指南
感谢支持ayqy个人订阅号,每周义务推送1篇(only_unique_one)原创精品博文,话题包括但不限于前端、Node、Android、数学(WebGL)、语文(课外书读后感)、英语(文档翻译)        如果觉得弱水三千,一瓢太少,可以去http://blog.ayqy.net看个痛快  一.webview标签
Stella981 Stella981
3年前
Flutter 实现原理及在马蜂窝的跨平台开发实践
一直以来,跨平台开发都是困扰移动客户端开发的难题。在马蜂窝旅游App很多业务场景里,我们尝试过一些主流的跨平台开发解决方案, 比如WebView和ReactNative,来提升开发效率和用户体验。但这两种方式也带来了新的问题。比如使用WebView跨平台方式,优点确实非常明显。基于WebView的框架集成了当下Web开发的诸多
少湖说 少湖说
8个月前
鸿蒙Flutter实战:04-如何使用DevTools调试Webview
鸿蒙Flutter如何使用DevTools调试Webview在《鸿蒙Flutter开发中集成Webview》,介绍了如果在Flutter中集成Webview.本文则为Webview的调试方法。配置WebviewCustomView.ets文件中,在生命周期