example.js 1.1 KB

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