example.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. 'use strict';
  2. /**
  3. * egg服务调用electron功能时,建议使用该模块
  4. */
  5. const path = require('path');
  6. const fs = require('fs');
  7. const _ = require('lodash');
  8. const {exec} = require('child_process');
  9. const {app, webContents, shell, dialog} = require('electron');
  10. const AutoLaunchManager = require('../lib/autoLaunch');
  11. const shortcut = require('../lib/shortcut');
  12. const chromeExtension = require('../lib/chromeExtension');
  13. const unzip = require("unzip-crx-3");
  14. /**
  15. * app根目录
  16. */
  17. exports.getPath = function () {
  18. const dir = app.getAppPath();
  19. return dir;
  20. }
  21. /**
  22. * 打开目录
  23. */
  24. exports.openDir = function (dir = '') {
  25. if (!dir) {
  26. return false;
  27. }
  28. dir = getElectronPath(dir);
  29. shell.openPath(dir);
  30. return true;
  31. }
  32. /**
  33. * 执行js
  34. */
  35. exports.executeJS = function (str) {
  36. let jscode = `(()=>{alert('${str}');return 'fromJs:${str}';})()`;
  37. console.log(jscode);
  38. return webContents.fromId(1).executeJavaScript(jscode);
  39. }
  40. /**
  41. * 快捷键-注册
  42. */
  43. exports.setShortcut = function (shortcutObj) {
  44. shortcut.register(shortcutObj, true, function (){
  45. MAIN_WINDOW.hide()
  46. });
  47. return true;
  48. }
  49. /**
  50. * 开机启动-开启
  51. */
  52. exports.autoLaunchEnable = function () {
  53. const autoLaunchManager = new AutoLaunchManager()
  54. const enable = autoLaunchManager.enable()
  55. return enable
  56. }
  57. /**
  58. * 开机启动-关闭
  59. */
  60. exports.autoLaunchDisable = function () {
  61. const autoLaunchManager = new AutoLaunchManager()
  62. const disable = autoLaunchManager.disable()
  63. return disable
  64. }
  65. /**
  66. * 开机启动-是否开启
  67. */
  68. exports.autoLaunchIsEnabled = function () {
  69. const autoLaunchManager = new AutoLaunchManager()
  70. const isEnable = autoLaunchManager.isEnabled()
  71. return isEnable
  72. }
  73. /**
  74. * 调用其它程序(exe、bash等可执行程序)
  75. */
  76. exports.openSoftware = function (softName = '') {
  77. if (!softName) {
  78. return false;
  79. }
  80. // 资源路径不同
  81. let softwarePath = '';
  82. if (app.isPackaged) {
  83. // 打包后
  84. softwarePath = path.join(app.getAppPath(), "..", "extraResources", softName);
  85. } else {
  86. // 打包前
  87. softwarePath = path.join(app.getAppPath(), "build", "extraResources", softName);
  88. }
  89. // 检查程序是否存在
  90. if (!fs.existsSync(softwarePath)) {
  91. return false;
  92. }
  93. // 命令行字符串 并 执行
  94. let cmdStr = 'start ' + softwarePath;
  95. exec(cmdStr);
  96. return true;
  97. }
  98. /**
  99. * 选择目录
  100. */
  101. exports.selectDir = function () {
  102. const filePaths = dialog.showOpenDialogSync({
  103. properties: ['openDirectory', 'createDirectory']
  104. });
  105. console.log('[example] [selectDir] filePaths:', filePaths);
  106. if (_.isEmpty(filePaths)) {
  107. return null
  108. }
  109. return filePaths[0];
  110. }
  111. /**
  112. * 测试用的 - 忽略
  113. */
  114. exports.testElectronApi = function () {
  115. const filePaths = dialog.showSaveDialogSync({
  116. properties: ['openFile', 'multiSelections']
  117. });
  118. console.log('[example] [testElectronApi] filePaths:', filePaths);
  119. return true;
  120. }
  121. /**
  122. * 显示消息对话框
  123. */
  124. exports.messageShow = function () {
  125. dialog.showMessageBoxSync({
  126. type: 'info', // "none", "info", "error", "question" 或者 "warning"
  127. title: '自定义标题-message',
  128. message: '自定义消息内容',
  129. detail: '其它的额外信息'
  130. })
  131. return true;
  132. }
  133. /**
  134. * 显示消息对话框和确认
  135. */
  136. exports.messageShowConfirm = function () {
  137. const res = dialog.showMessageBoxSync({
  138. type: 'info',
  139. title: '自定义标题-message',
  140. message: '自定义消息内容',
  141. detail: '其它的额外信息',
  142. cancelId: 1, // 用于取消对话框的按钮的索引
  143. defaultId: 0, // 设置默认选中的按钮
  144. buttons: ['确认', '取消'], // 按钮及索引
  145. })
  146. console.log('[example] [messageShowConfirm] 结果:', res, res === 0 ? '点击确认按钮' : '点击取消按钮');
  147. return true;
  148. }
  149. /**
  150. * 加载扩展程序
  151. */
  152. exports.loadExtension = async function (crxFile) {
  153. if (_.isEmpty(crxFile)) {
  154. return false;
  155. }
  156. const extensionId = path.basename(crxFile, '.crx');
  157. const chromeExtensionDir = chromeExtension.getDirectory();
  158. const extensionDir = path.join(chromeExtensionDir, extensionId);
  159. console.log("[api] [example] [loadExtension] extension id:", extensionId);
  160. unzip(crxFile, extensionDir).then(() => {
  161. console.log("[api] [example] [loadExtension] unzip success!");
  162. chromeExtension.load(extensionId);
  163. });
  164. return true;
  165. }
  166. function getElectronPath(filepath) {
  167. //filepath = path.resolve(filepath);
  168. filepath = filepath.replace("resources", "");
  169. filepath = filepath.replace("app.asar", "");
  170. filepath = path.normalize(filepath);
  171. return filepath;
  172. };