example.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. const path = require('path');
  3. const {
  4. app,
  5. webContents,
  6. shell,
  7. globalShortcut
  8. } = require('electron');
  9. const shortcut = require('../lib/shortcut');
  10. exports.getPath = function () {
  11. const dir = app.getAppPath();
  12. return dir;
  13. }
  14. exports.openDir = function (dir = '') {
  15. if (!dir) {
  16. return false;
  17. }
  18. dir = getElectronPath(dir);
  19. shell.openPath(dir);
  20. return true;
  21. }
  22. exports.executeJS = function (str) {
  23. let jscode = `(()=>{alert('${str}');return 'fromJs:${str}';})()`;
  24. console.log(jscode);
  25. return webContents.fromId(1).executeJavaScript(jscode);
  26. }
  27. exports.setShortcut = function (shortcutStr = "") {
  28. if (!shortcutStr || shortcutStr == "") {
  29. return false;
  30. }
  31. shortcut.register(shortcutStr, true, function (){
  32. MAIN_WINDOW.show()
  33. });
  34. return true;
  35. }
  36. function getElectronPath(filepath) {
  37. //filepath = path.resolve(filepath);
  38. filepath = filepath.replace("resources", "");
  39. filepath = filepath.replace("app.asar", "");
  40. filepath = path.normalize(filepath);
  41. return filepath;
  42. };