|
@@ -0,0 +1,124 @@
|
|
|
+const {
|
|
|
+ remote,
|
|
|
+ clipboard
|
|
|
+} = require('electron');
|
|
|
+const { Menu, MenuItem } = remote
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+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(){
|
|
|
+
|
|
|
+ window.addEventListener('contextmenu', function (e) {
|
|
|
+ e.preventDefault()
|
|
|
+ let menu = new Menu()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ let flag = false
|
|
|
+ const tagName = document.activeElement.tagName
|
|
|
+ const str = clipboard.readText()
|
|
|
+ const selectStr = getSelections()
|
|
|
+ const text = e.target.innerText || ''
|
|
|
+ const value = e.target.value || ''
|
|
|
+
|
|
|
+ if (selectStr) {
|
|
|
+ flag = true
|
|
|
+
|
|
|
+ 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 }))
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (flag && (getSelections() || str)) {
|
|
|
+
|
|
|
+ menu.popup(remote.getCurrentWindow())
|
|
|
+ }
|
|
|
+ }, false)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function getSelections () {
|
|
|
+ var text = ''
|
|
|
+ if (window.getSelection) {
|
|
|
+ text = window.getSelection().toString()
|
|
|
+ } else if (document.selection && document.selection.type !== 'Control') {
|
|
|
+ text = document.selection.createRange().text
|
|
|
+ }
|
|
|
+ if (text) {
|
|
|
+ return text
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+getContextMenu()
|