menus.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const {
  2. remote,
  3. clipboard
  4. } = require('electron');
  5. const { Menu, MenuItem } = remote
  6. // var rightTemplate = [{
  7. // label: "复制",
  8. // accelerator: 'ctrl+c',
  9. // click: function() {
  10. // console.log('复制',window.getSelection().toString())
  11. // clipboard.writeText(window.getSelection().toString(), 'selection');
  12. // }
  13. // },
  14. // {
  15. // label: "粘贴",
  16. // accelerator: 'ctrl+v',
  17. // click: function() {
  18. // console.log('粘贴')
  19. // clipboard.readText('selection');
  20. // }
  21. // }
  22. // ]
  23. // console.log(remote)
  24. // var m = remote.Menu.buildFromTemplate(rightTemplate);
  25. // window.addEventListener('contextmenu', function(e) {
  26. // e.preventDefault();
  27. // var hasSelection = window.getSelection().toString()
  28. // if(hasSelection.length){
  29. // m.popup({
  30. // window: remote.getCurrentWindow()
  31. // });
  32. // }
  33. // })
  34. // const inputs = document.querySelectorAll("input,textarea")
  35. // console.log(inputs)
  36. // for (const inp of inputs) {
  37. // inp.addEventListener("focus", function(e){
  38. // console.log(e)
  39. // var hasSelection = window.getSelection().toString()
  40. // if(hasSelection.length){
  41. // m.popup({
  42. // window: remote.getCurrentWindow()
  43. // });
  44. // }
  45. // });
  46. // }
  47. // 写入剪贴板方法
  48. function copyString () {
  49. const str = getSelections() // 获取选中内容
  50. clipboard.writeText(str) // 写入剪贴板
  51. }
  52. // 获取剪贴版内容写入当前焦点元素中
  53. function printString () {
  54. if (document.activeElement) {
  55. const str = clipboard.readText() // 获取剪贴板内容
  56. document.activeElement.value = str // 写入焦点元素
  57. var eventchange = new Event('change');
  58. var eventinput = new Event('input');
  59. document.activeElement.dispatchEvent(eventchange);
  60. document.activeElement.dispatchEvent(eventinput);
  61. clipboard.clear() // 清空剪贴板
  62. }
  63. }
  64. function getContextMenu(){
  65. // 监听contextmenu,实现自定义右键菜单
  66. window.addEventListener('contextmenu', function (e) {
  67. e.preventDefault()
  68. let menu = new Menu()
  69. // ↓ 情况一:任何情况下,都显示 复制 和 粘贴 按钮
  70. //添加菜单功能, label: 菜单名称, accelerator:快捷键,click:点击方法
  71. // menu.append(new MenuItem({ label: '复制', accelerator: 'CommandOrControl+C', click: copyString }))
  72. // //添加菜单分割线
  73. // menu.append(new MenuItem({ type: 'separator' }))
  74. // //添加菜单功能
  75. // menu.append(new MenuItem({ label: '粘贴', accelerator: 'CommandOrControl+V', click: printString }))
  76. // ↑ 情况一
  77. // ↓ 情况二:可复制时,显示复制,可粘贴时显示粘贴
  78. let flag = false // menu中是否有菜单项,true有,false没有
  79. const tagName = document.activeElement.tagName // 焦点元素的tagName
  80. const str = clipboard.readText() // 剪贴板中的内容
  81. let selectStr = getSelections()// 选中的内容
  82. const text = e.target.innerText || '' // 目标标签的innerText
  83. const value = e.target.value || '' // 目标标签的value
  84. if (selectStr) { // 如果有选中内容
  85. flag = true
  86. selectStr = selectStr.replace(/\r\n\t\f\v/g,"")
  87. selectStr = selectStr.replace(/\n/g,"")
  88. selectStr = selectStr.replace(/\t/g,"")
  89. selectStr = selectStr.replace(/\f/g,"")
  90. selectStr = selectStr.replace(/\v/g,"")
  91. // 在 选中的元素或者输入框 上面点右键,这样在选中后点别处就不会出现右键复制菜单
  92. if (text.indexOf(selectStr) !== -1 || value.indexOf(selectStr) !== -1) menu.append(new MenuItem({ label: '复制', click: copyString }))
  93. }
  94. if (str && (tagName === 'INPUT' || tagName === 'TEXTAREA')) { // 若为输入框 且 剪贴板中有内容,则显示粘贴菜单
  95. flag = true
  96. menu.append(new MenuItem({ label: '粘贴', click: printString }))
  97. }
  98. // ↑ 情况二
  99. // menu中有菜单项 且(有选中内容 或 剪贴板中有内容)
  100. console.log(flag,getSelections(),str)
  101. if (flag && (getSelections() || str)) {
  102. // 将此menu菜单作为 当前窗口 remote.getCurrentWindow() 中的上下文菜单弹出。
  103. menu.popup(remote.getCurrentWindow())
  104. }
  105. }, false)
  106. }
  107. // 获取选中内容
  108. function getSelections () {
  109. var text = ''
  110. if (window.getSelection) { // 除IE9以下 之外的浏览器
  111. text = window.getSelection().toString()
  112. } else if (document.selection && document.selection.type !== 'Control') { //IE9以下,可不考虑
  113. text = document.selection.createRange().text
  114. }
  115. if (text) {
  116. return text
  117. }
  118. }
  119. getContextMenu()