electron launcher
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.8 KiB

4 years ago
2 years ago
4 years ago
2 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
  1. // All of the Node.js APIs are available in the preload process.
  2. // It has the same sandbox as a Chrome extension.
  3. const {ipcRenderer, desktopCapturer} = require('electron');
  4. const remote = require('@electron/remote');
  5. window.currentFrame = 'platform';
  6. window.api = [
  7. 'showSuspensionWindow',
  8. 'resizeWindow',
  9. 'setUnReadMessage',
  10. 'getUnReadMessage',
  11. 'windowMoveHandle',
  12. 'exitSystem',
  13. 'openDevTools',
  14. 'hideMainWindow',
  15. 'showMainWindow',
  16. 'minimize',
  17. 'hideSuspensionWindow',
  18. 'closeWindowById',
  19. 'PrtSc',
  20. 'downloadImg',
  21. 'removeCanvas',
  22. 'getCookies'
  23. ].reduce(
  24. (acc, apiName) => {
  25. acc[apiName] = (...params) => {
  26. return ipcRenderer.send(apiName, ...params);
  27. };
  28. return acc;
  29. },
  30. {
  31. on: ipcRenderer.on.bind(ipcRenderer),
  32. send: ipcRenderer.send.bind(ipcRenderer),
  33. removeListener: ipcRenderer.removeListener.bind(ipcRenderer),
  34. }
  35. );
  36. ipcRenderer.on('getUnReadMessage', (e, args) => {
  37. window.getUnReadMessage(args[0]);
  38. });
  39. let ImgSrc = null;
  40. // 监听用户截屏
  41. ipcRenderer.on('PrtSc', (e, args) => {
  42. // 显示截屏区域
  43. let screenContent = document.querySelector('.screen-canvas');
  44. screenContent.style.display = 'block';
  45. // 获取当前屏幕的宽高
  46. const {width, height} = remote.getCurrentWindow().getBounds();
  47. console.log(width, height);
  48. // 获取canvas元素
  49. const canvas = document.getElementById('thumbnailCanvas');
  50. const ctx = canvas.getContext('2d');
  51. // 这里的300可根据需要自定义,显示的是截屏后图片显示的大小
  52. canvas.width = (height - 300) * 16 / 9;
  53. canvas.height = height - 300;
  54. desktopCapturer.getSources({types: ['screen'], thumbnailSize: {width: canvas.width, height: canvas.height}}).then(async sources => {
  55. try {
  56. sources.forEach(source => {
  57. const img = new Image();
  58. img.src = source.thumbnail.toDataURL();
  59. ImgSrc = img.src
  60. img.onload = () => {
  61. ctx.drawImage(img, 0, 0);
  62. };
  63. });
  64. } catch (error) {
  65. console.log(error);
  66. }
  67. });
  68. });
  69. // 监听用户下载截屏图片
  70. ipcRenderer.on('downloadImg', (e, args) => {
  71. downLoadImage(ImgSrc);
  72. });
  73. // 下载图片
  74. function downLoadImage(src) {
  75. const link = document.createElement('a');
  76. link.href = src;
  77. link.download = new Date().getTime() + '.png';
  78. document.body.appendChild(link);
  79. link.click();
  80. document.body.removeChild(link);
  81. }
  82. // 监听用户取消截屏图片显示
  83. ipcRenderer.on('removeCanvas', (e, args) => {
  84. removeCanvas()
  85. });
  86. function removeCanvas() {
  87. // 获取canvas元素
  88. const canvas = document.getElementById('thumbnailCanvas');
  89. const ctx = canvas.getContext('2d');
  90. // 清除画布
  91. ctx.clearRect(0, 0, canvas.width, canvas.height);
  92. // 隐藏截屏区域
  93. let screenContent = document.querySelector('.screen-canvas');
  94. screenContent.style.display = 'none';
  95. }