example.js 951 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. const path = require('path');
  3. const {
  4. app,
  5. webContents,
  6. shell
  7. } = require('electron');
  8. const shortcut = require('../lib/shortcut');
  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.setShortcut = function (shortcutObj) {
  27. shortcut.register(shortcutObj, true, function (){
  28. MAIN_WINDOW.hide()
  29. });
  30. return true;
  31. }
  32. function getElectronPath(filepath) {
  33. //filepath = path.resolve(filepath);
  34. filepath = filepath.replace("resources", "");
  35. filepath = filepath.replace("app.asar", "");
  36. filepath = path.normalize(filepath);
  37. return filepath;
  38. };