main.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. const fs = require('fs');
  3. module.exports = {
  4. load()
  5. {
  6. try {
  7. if (Editor.Window.main.nativeWin.webContents.__injected_handle_widget) {
  8. // in case plugin if reloaded
  9. return;
  10. }
  11. } catch(error) {
  12. // usually happen when creator is just started and main window is not created
  13. Editor.log(error);
  14. }
  15. // todo: 如果插件是中途加载的,判断webContents如果就绪了就注入
  16. const electron = require('electron');
  17. electron.app.on('web-contents-created', (sender, webContents) => {
  18. webContents.on('dom-ready', (e) => {
  19. this.injectFn(e.sender);
  20. });
  21. });
  22. },
  23. // 往web环境里写代码,添加键盘监听事件
  24. injectFn(webContents){
  25. if (webContents.__injected_handle_widget) {
  26. // already injected
  27. return;
  28. }
  29. webContents.__injected_handle_widget = true;
  30. let hackCode = fs.readFileSync(Editor.url('packages://simple-handle-node/panel/hackCode.js')).toString();
  31. webContents.executeJavaScript(hackCode, function(result) {
  32. });
  33. },
  34. unload() {
  35. },
  36. messages: {
  37. 'set-active'() {
  38. Editor.Scene.callSceneScript('simple-handle-node', 'set-active', {});
  39. },
  40. 'set-active-radio'() {
  41. Editor.Scene.callSceneScript('simple-handle-node', 'set-active-radio', {});
  42. },
  43. },
  44. };