| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 | 
							- const {
 
-   remote,
 
-   clipboard
 
- } = require('electron');
 
- const { Menu, MenuItem } = remote
 
- // var rightTemplate = [{
 
- //     label: "复制",
 
- //     accelerator: 'ctrl+c',
 
- //     click: function() {
 
- //       console.log('复制',window.getSelection().toString())
 
- //       clipboard.writeText(window.getSelection().toString(), 'selection');
 
- //     }
 
- //   },
 
- //   {
 
- //     label: "粘贴",
 
- //     accelerator: 'ctrl+v',
 
- //     click: function() {
 
- //       console.log('粘贴')
 
- //       clipboard.readText('selection');
 
- //     }
 
- //   }
 
- // ]
 
- // console.log(remote)
 
- // var m = remote.Menu.buildFromTemplate(rightTemplate);
 
- // window.addEventListener('contextmenu', function(e) {
 
- //   e.preventDefault();
 
- //   var hasSelection = window.getSelection().toString()
 
- //   if(hasSelection.length){
 
- //     m.popup({
 
- //       window: remote.getCurrentWindow()
 
- //     });
 
- //   }
 
- // })
 
- // const inputs = document.querySelectorAll("input,textarea")
 
- // console.log(inputs)
 
- // for (const inp of inputs) {
 
- //   inp.addEventListener("focus", function(e){
 
- //     console.log(e)
 
- //     var hasSelection = window.getSelection().toString()
 
- //     if(hasSelection.length){
 
- //       m.popup({
 
- //         window: remote.getCurrentWindow()
 
- //       });
 
- //     }
 
- //   });
 
- // }
 
- // 写入剪贴板方法
 
- function copyString () {
 
-   const str = getSelections() // 获取选中内容
 
-   clipboard.writeText(str) // 写入剪贴板
 
- }
 
- // 获取剪贴版内容写入当前焦点元素中
 
- function printString () {
 
-   if (document.activeElement) {
 
-     const str = clipboard.readText() // 获取剪贴板内容
 
-     document.activeElement.value = str // 写入焦点元素
 
-     var eventchange = new Event('change');
 
-     var eventinput = new Event('input');
 
-     document.activeElement.dispatchEvent(eventchange);
 
-     document.activeElement.dispatchEvent(eventinput);
 
-     clipboard.clear() // 清空剪贴板
 
-   }
 
- }
 
- function getContextMenu(){
 
-   // 监听contextmenu,实现自定义右键菜单
 
-     window.addEventListener('contextmenu', function (e) {
 
-       e.preventDefault()
 
-       let menu = new Menu()
 
-   
 
-       // ↓ 情况一:任何情况下,都显示 复制 和 粘贴 按钮
 
-       //添加菜单功能, label: 菜单名称, accelerator:快捷键,click:点击方法
 
-       // menu.append(new MenuItem({ label: '复制', accelerator: 'CommandOrControl+C', click: copyString }))
 
-       // //添加菜单分割线
 
-       // menu.append(new MenuItem({ type: 'separator' }))
 
-       // //添加菜单功能
 
-       // menu.append(new MenuItem({ label: '粘贴', accelerator: 'CommandOrControl+V', click: printString }))
 
-       // ↑ 情况一
 
-   
 
-       // ↓ 情况二:可复制时,显示复制,可粘贴时显示粘贴
 
-       let flag = false // menu中是否有菜单项,true有,false没有
 
-       const tagName = document.activeElement.tagName // 焦点元素的tagName
 
-       const str = clipboard.readText() // 剪贴板中的内容
 
-       let selectStr = getSelections()// 选中的内容
 
-       const text = e.target.innerText || '' // 目标标签的innerText
 
-       const value = e.target.value || '' // 目标标签的value
 
-   
 
-       if (selectStr) { // 如果有选中内容
 
-         flag = true
 
-         selectStr = selectStr.replace(/\r\n\t\f\v/g,"")
 
-         selectStr = selectStr.replace(/\n/g,"")
 
-         selectStr = selectStr.replace(/\t/g,"")
 
-         selectStr = selectStr.replace(/\f/g,"")
 
-         selectStr = selectStr.replace(/\v/g,"")
 
-         // 在 选中的元素或者输入框 上面点右键,这样在选中后点别处就不会出现右键复制菜单
 
-         if (text.indexOf(selectStr) !== -1 || value.indexOf(selectStr) !== -1) menu.append(new MenuItem({ label: '复制', click: copyString }))
 
-       }
 
-       if (str && (tagName === 'INPUT' || tagName === 'TEXTAREA')) { // 若为输入框 且 剪贴板中有内容,则显示粘贴菜单
 
-         flag = true
 
-         menu.append(new MenuItem({ label: '粘贴', click: printString }))
 
-       }
 
-   
 
-       // ↑ 情况二
 
-       // menu中有菜单项 且(有选中内容 或 剪贴板中有内容)
 
-       console.log(flag,getSelections(),str)
 
-       if (flag && (getSelections() || str)) {
 
-         // 将此menu菜单作为 当前窗口 remote.getCurrentWindow() 中的上下文菜单弹出。
 
-         menu.popup(remote.getCurrentWindow())
 
-       }
 
-     }, false)
 
- }
 
- // 获取选中内容
 
- function getSelections () {
 
-   var text = ''
 
-   if (window.getSelection) { // 除IE9以下 之外的浏览器
 
-     text = window.getSelection().toString()
 
-   } else if (document.selection && document.selection.type !== 'Control') { //IE9以下,可不考虑
 
-     text = document.selection.createRange().text
 
-   }
 
-   if (text) {
 
-     return text
 
-   }
 
- }
 
- getContextMenu()
 
 
  |