index.js 1.4 KB

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