index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const fs = require("fire-fs");
  2. const { shell, ipcRenderer } = require("electron");
  3. const http = Editor.require("packages://im-plugin/core/http.js");
  4. const { tr } = Editor.require('packages://im-plugin/core/translate.js');
  5. const { readJson } = Editor.require('packages://im-plugin/core/utils');
  6. const pluginVersion = readJson(Editor.url("packages://im-plugin/package.json")).version;
  7. let forceClose = false;
  8. Editor.Panel.extend({
  9. listeners: {},
  10. template: fs.readFileSync(
  11. Editor.url("packages://im-plugin/panel/index.html"),
  12. "utf-8"
  13. ),
  14. messages: {
  15. "im-plugin:force-close"(event) {
  16. forceClose = true;
  17. Editor.Ipc.sendToMain("im-plugin:close");
  18. },
  19. },
  20. $: {
  21. im_webview: "#im-webview",
  22. loading: "#imPlugin-loading",
  23. },
  24. // 阻止关闭
  25. // 2.4.0增加canClose(),旧版本只能用close(),故这里代码写了两遍
  26. close(event) {
  27. if (forceClose) {
  28. return true;
  29. }
  30. Editor.Ipc.sendToMain("im-plugin:hide");
  31. return false;
  32. },
  33. canClose() {
  34. if (forceClose) {
  35. return true;
  36. }
  37. Editor.Ipc.sendToMain("im-plugin:hide");
  38. return false;
  39. },
  40. async ready() {
  41. forceClose = false;
  42. const isLogin = await Editor.User.isLoggedIn();
  43. if (!isLogin) {
  44. this.showInfoAndForceClose("info-non-logged-in");
  45. return;
  46. }
  47. !window._Scene &&
  48. (document.title = Editor.T("im-plugin.title") + ` v${pluginVersion}`);
  49. // 注入 ipcRenderer 的 sendToHost 接口,提供给 webview 内嵌页面使用
  50. this.$im_webview.setAttribute(
  51. "preload",
  52. `file://${Editor.url("packages://im-plugin/panel/preload.js")}`
  53. );
  54. // 打开 webview 内嵌页面的开发者工具
  55. // this.$im_webview.addEventListener("dom-ready", () =>
  56. // this.$im_webview.openDevTools()
  57. // );
  58. // 清空webview缓存
  59. const webContents = this.$im_webview.getWebContents();
  60. typeof webContents !== "undefined" &&
  61. webContents.session.clearStorageData([
  62. "appcache",
  63. "cookies",
  64. "filesystem",
  65. "indexdb",
  66. "localstorage",
  67. "shadercache",
  68. "websql",
  69. "serviceworkers",
  70. "cachestorage",
  71. ]);
  72. // 注册 ipc 消息
  73. this.$im_webview.addEventListener("ipc-message", (event) => {
  74. switch (event.channel) {
  75. case "new-msg":
  76. Editor.Ipc.sendToMain(
  77. "im-plugin:update-icon",
  78. event.args[0].newMsgCount
  79. );
  80. break;
  81. // 加载完毕,隐藏loading状态
  82. case "is-ready":
  83. this.$loading.style.display = "none";
  84. break;
  85. case "download":
  86. http.download(event.args[0].url, event.args[0].path, () => {});
  87. break;
  88. default:
  89. break;
  90. }
  91. });
  92. this.$im_webview.addEventListener("new-window", (event) => {
  93. shell.openExternal(
  94. `https://creator-api.cocos.com/api/account/client_signin?session_id=${
  95. http.userInfo.session_id
  96. }&redirect_url=${encodeURIComponent(event.url)}`
  97. );
  98. });
  99. // 暂时每次都reload,避免session的改变,URL只会在面板第一次打开时获取,
  100. // 之后面板会一直保持显示或者隐藏直到用户退出或者关闭Creator,
  101. // 故init的调用是不频繁的。
  102. await http.init(true);
  103. const entryUrl = ipcRenderer.sendSync(
  104. "im-plugin:get-entry-url"
  105. );
  106. if (!entryUrl) {
  107. this.showInfoAndForceClose("get-entry-url-failed");
  108. return;
  109. }
  110. this.openUrl = `https://creator-api.cocos.com/api/account/client_signin?session_id=${
  111. http.userInfo.session_id
  112. }&redirect_url=${encodeURIComponent(entryUrl)}%3flang%3d${Editor.lang}`;
  113. this.$im_webview.src = this.openUrl;
  114. // 将窗口状态IPC消息转发到 webview 中
  115. ipcRenderer.on("im-plugin:panel-win-state-changed", (event, eventName) => {
  116. // 当用户登出,面板被强制关闭的时候,webview的DOM会被销毁,此时发消息会产生异常,这里捕获并忽略该异常
  117. try {
  118. this.$im_webview.send("im-plugin:panel-win-state-changed", eventName);
  119. } catch (_) {}
  120. });
  121. Editor.Ipc.sendToMain("im-plugin:panel-ready");
  122. },
  123. showInfoAndForceClose(trMsg) {
  124. Editor.Dialog.messageBox({
  125. title: tr("info-title"),
  126. message: tr(trMsg),
  127. buttons: [tr("ok")],
  128. defaultId: 0,
  129. noLink: true,
  130. });
  131. forceClose = true;
  132. Editor.Ipc.sendToMain("im-plugin:close");
  133. },
  134. });