example.js 969 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 (shortcutObj) {
  28. shortcut.register(shortcutObj, true, function (){
  29. MAIN_WINDOW.hide()
  30. });
  31. return true;
  32. }
  33. function getElectronPath(filepath) {
  34. //filepath = path.resolve(filepath);
  35. filepath = filepath.replace("resources", "");
  36. filepath = filepath.replace("app.asar", "");
  37. filepath = path.normalize(filepath);
  38. return filepath;
  39. };