main.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. const fs = require("fs");
  3. const path = require("path");
  4. const electron = require('electron')
  5. // 获得Creator主窗口
  6. function getMainWebContents(){
  7. let allwins = electron.BrowserWindow.getAllWindows();
  8. for (let i = 0; i < allwins.length; i++) {
  9. const win = allwins[i];
  10. const url = win.getURL()
  11. if (url.includes('windows/main.html') || win.title && win.title.includes('Cocos Creator')){
  12. return win.webContents;
  13. }
  14. }
  15. return;
  16. }
  17. module.exports = {
  18. load() {
  19. let webContents = getMainWebContents()
  20. try {
  21. // if (webContents.__injected_handle_prefab_tab) {
  22. // // in case plugin if reloaded
  23. // return;
  24. // }
  25. } catch (error) {
  26. // usually happen when creator is just started and main window is not created
  27. console.log(error);
  28. return
  29. }
  30. // 往web环境里写代码,添加键盘监听事件
  31. let hackCode = fs.readFileSync(path.join(__dirname, "panel", "hackCode.js")).toString();
  32. hackCode = hackCode.replace('?pos/targetPath', __dirname.replace(/\\/g,'\\\\'));
  33. // Editor.log('hackCode', hackCode);
  34. // webContents.__injected_handle_prefab_tab = true;
  35. if(webContents){
  36. webContents.executeJavaScript(
  37. hackCode,function (result) { }
  38. );
  39. }else{
  40. console.warn("插件启动失败: webContents is null");
  41. }
  42. },
  43. unload() {
  44. let webContents = getMainWebContents()
  45. if(webContents){
  46. webContents.executeJavaScript(
  47. `
  48. (()=>{
  49. let d = document.getElementById('prefab-tab');
  50. if(d){
  51. d.parentNode.removeChild(d);
  52. }
  53. })()
  54. `,function (result) { }
  55. );
  56. }
  57. },
  58. methods: {
  59. // setActive() {
  60. // Editor.Message.request("scene", "execute-scene-script", {
  61. // name: "simple-handle-node",
  62. // method: "set-active",
  63. // args: {},
  64. // });
  65. // },
  66. // setActiveRadio() {
  67. // Editor.Message.request("scene", "execute-scene-script", {
  68. // name: "simple-handle-node",
  69. // method: "set-active-radio",
  70. // args: {},
  71. // });
  72. // },
  73. },
  74. };