index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const electronApp = require('electron').app;
  2. /**
  3. * 唤醒插件
  4. * @class
  5. */
  6. class AwakenAddon {
  7. constructor(app) {
  8. this.app = app;
  9. this.cfg = app.config.addons.awaken;
  10. this.protocol = '';
  11. }
  12. /**
  13. * 创建
  14. */
  15. create () {
  16. this.app.console.info('[addon:awaken] load');
  17. this.protocol = this.cfg.protocol;
  18. electronApp.setAsDefaultProtocolClient(this.protocol);
  19. const self = this;
  20. this.handleArgv(process.argv);
  21. electronApp.on('second-instance', (event, argv) => {
  22. if (process.platform === 'win32') {
  23. self.handleArgv(argv)
  24. }
  25. })
  26. // 仅用于macOS
  27. electronApp.on('open-url', (event, urlStr) => {
  28. self.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. this.app.logger.info('[addon:awaken] awakeUrlInfo:', awakeUrlInfo);
  54. }
  55. }
  56. AwakenAddon.toString = () => '[class AwakenAddon]';
  57. module.exports = AwakenAddon;