example.js 757 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. const path = require('path');
  3. const {
  4. app,
  5. webContents,
  6. shell
  7. } = require('electron');
  8. exports.getPath = function () {
  9. const dir = app.getAppPath();
  10. return dir;
  11. }
  12. exports.openDir = function (dir = '') {
  13. if (!dir) {
  14. return false;
  15. }
  16. dir = getElectronPath(dir);
  17. shell.openItem(dir);
  18. return true;
  19. }
  20. exports.executeJS = function (str) {
  21. let jscode = `(()=>{alert('${str}');return 'fromJs:${str}';})()`;
  22. console.log(jscode);
  23. return webContents.fromId(1).executeJavaScript(jscode);
  24. }
  25. function getElectronPath(filepath) {
  26. //filepath = path.resolve(filepath);
  27. filepath = filepath.replace("resources", "");
  28. filepath = filepath.replace("app.asar", "");
  29. filepath = path.normalize(filepath);
  30. return filepath;
  31. };