index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const { app: electronApp } = require('electron');
  2. const Log = require('ee-core/log');
  3. const Conf = require('ee-core/config');
  4. /**
  5. * 唤醒插件
  6. * @class
  7. */
  8. class AwakenAddon {
  9. constructor() {
  10. this.protocol = '';
  11. }
  12. /**
  13. * 创建
  14. */
  15. create () {
  16. Log.info('[addon:awaken] load');
  17. const cfg = Conf.getValue('addons.awaken');
  18. this.protocol = cfg.protocol;
  19. electronApp.setAsDefaultProtocolClient(this.protocol);
  20. this.handleArgv(process.argv);
  21. electronApp.on('second-instance', (event, argv) => {
  22. if (process.platform === 'win32') {
  23. this.handleArgv(argv)
  24. }
  25. })
  26. // 仅用于macOS
  27. electronApp.on('open-url', (event, urlStr) => {
  28. this.handleUrl(urlStr)
  29. })
  30. }
  31. /**
  32. * 参数处理
  33. */
  34. handleArgv(argv) {
  35. const offset = electronApp.isPackaged ? 1 : 2;
  36. const url = argv.find((arg, i) => i >= offset && arg.startsWith(this.protocol));
  37. this.handleUrl(url)
  38. }
  39. /**
  40. * url解析
  41. */
  42. handleUrl(awakeUrlStr) {
  43. if (!awakeUrlStr || awakeUrlStr.length === 0) {
  44. return
  45. }
  46. const {hostname, pathname, search} = new URL(awakeUrlStr);
  47. let awakeUrlInfo = {
  48. urlStr: awakeUrlStr,
  49. urlHost: hostname,
  50. urlPath: pathname,
  51. urlParams: search && search.slice(1)
  52. }
  53. Log.info('[addon:awaken] awakeUrlInfo:', awakeUrlInfo);
  54. }
  55. }
  56. AwakenAddon.toString = () => '[class AwakenAddon]';
  57. module.exports = AwakenAddon;