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.

106 lines
2.9 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. 'getFilePath'
  24. ].reduce(
  25. (acc, apiName) => {
  26. acc[apiName] = (...params) => {
  27. return ipcRenderer.send(apiName, ...params);
  28. };
  29. return acc;
  30. },
  31. {
  32. on: ipcRenderer.on.bind(ipcRenderer),
  33. send: ipcRenderer.send.bind(ipcRenderer),
  34. removeListener: ipcRenderer.removeListener.bind(ipcRenderer),
  35. }
  36. );
  37. ipcRenderer.on('getUnReadMessage', (e, args) => {
  38. window.getUnReadMessage(args[0]);
  39. });
  40. let ImgSrc = null;
  41. // 监听用户截屏
  42. ipcRenderer.on('PrtSc', (e, args) => {
  43. // 显示截屏区域
  44. let screenContent = document.querySelector('.screen-canvas');
  45. screenContent.style.display = 'block';
  46. // 获取当前屏幕的宽高
  47. const {width, height} = remote.getCurrentWindow().getBounds();
  48. console.log(width, height);
  49. // 获取canvas元素
  50. const canvas = document.getElementById('thumbnailCanvas');
  51. const ctx = canvas.getContext('2d');
  52. // 这里的300可根据需要自定义,显示的是截屏后图片显示的大小
  53. canvas.width = (height - 300) * 16 / 9;
  54. canvas.height = height - 300;
  55. desktopCapturer.getSources({types: ['screen'], thumbnailSize: {width: canvas.width, height: canvas.height}}).then(async sources => {
  56. try {
  57. sources.forEach(source => {
  58. const img = new Image();
  59. img.src = source.thumbnail.toDataURL();
  60. ImgSrc = img.src
  61. img.onload = () => {
  62. ctx.drawImage(img, 0, 0);
  63. };
  64. });
  65. } catch (error) {
  66. console.log(error);
  67. }
  68. });
  69. });
  70. // 监听用户下载截屏图片
  71. ipcRenderer.on('downloadImg', (e, args) => {
  72. downLoadImage(ImgSrc);
  73. });
  74. // 下载图片
  75. function downLoadImage(src) {
  76. const link = document.createElement('a');
  77. link.href = src;
  78. link.download = new Date().getTime() + '.png';
  79. document.body.appendChild(link);
  80. link.click();
  81. document.body.removeChild(link);
  82. }
  83. // 监听用户取消截屏图片显示
  84. ipcRenderer.on('removeCanvas', (e, args) => {
  85. removeCanvas()
  86. });
  87. function removeCanvas() {
  88. // 获取canvas元素
  89. const canvas = document.getElementById('thumbnailCanvas');
  90. const ctx = canvas.getContext('2d');
  91. // 清除画布
  92. ctx.clearRect(0, 0, canvas.width, canvas.height);
  93. // 隐藏截屏区域
  94. let screenContent = document.querySelector('.screen-canvas');
  95. screenContent.style.display = 'none';
  96. }
  97. // 获取文件路径
  98. ipcRenderer.on('getFilePath', (e, filePath) => {
  99. console.log(filePath);
  100. });