├── .gitignore ├── package.json ├── LICENSE ├── index.js ├── README.md └── EventEmitter.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "electron-clipboard-extended", 3 | "version": "1.1.1", 4 | "description": "Electron clipboard extended with event handler for Text and Image change", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/arjun-g/electron-clipboard-extended.git" 12 | }, 13 | "keywords": [ 14 | "electron", 15 | "clipboard" 16 | ], 17 | "author": "Arjun G", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/arjun-g/electron-clipboard-extended/issues" 21 | }, 22 | "homepage": "https://github.com/arjun-g/electron-clipboard-extended#readme", 23 | "dependencies": { 24 | "events": "^1.1.1" 25 | }, 26 | "devDependencies": { 27 | "electron": "^1.6.2" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Arjun Ganesan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const {clipboard} = require('electron') 2 | const EventEmitter = require('./EventEmitter') 3 | const clipboardEmitter = new EventEmitter() 4 | 5 | let watcherId = null, previousText = clipboard.readText(), previousImage = clipboard.readImage() 6 | 7 | clipboard.on = (event, listener) => { 8 | clipboardEmitter.on(event, listener) 9 | return clipboard 10 | } 11 | 12 | clipboard.once = (event, listener) => { 13 | clipboardEmitter.once(event, listener) 14 | return clipboard 15 | } 16 | 17 | clipboard.off = (event, listener) => { 18 | if(listener) 19 | clipboardEmitter.removeListener(event, listener) 20 | else 21 | clipboardEmitter.removeAllListeners(event) 22 | return clipboard 23 | } 24 | 25 | clipboard.startWatching = () => { 26 | if(!watcherId) 27 | watcherId = setInterval(() => { 28 | if(isDiffText(previousText, previousText = clipboard.readText())) clipboardEmitter.emit('text-changed') 29 | if(isDiffImage(previousImage, previousImage = clipboard.readImage())) clipboardEmitter.emit('image-changed') 30 | }, 500) 31 | return clipboard 32 | } 33 | 34 | clipboard.stopWatching = () => { 35 | if(watcherId) clearInterval(watcherId) 36 | watcherId = null 37 | return clipboard 38 | } 39 | 40 | function isDiffText(str1, str2){ 41 | return str2 && str1 !== str2 42 | } 43 | 44 | function isDiffImage(img1, img2){ 45 | return !img2.isEmpty() && img1.toDataURL() !== img2.toDataURL() 46 | } 47 | 48 | module.exports = clipboard -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Electron Clipboard Extended 2 | >Electron clipboard extended with event handler for Text and Image change 3 | 4 | ```bash 5 | npm install electron-clipboard-extended 6 | ``` 7 | 8 | This library has all methods of electron's clipboard api with added functionalities for listening to changes in clipboard. As of now only Text and Image changes are captured. 9 | 10 | ## Usage 11 | 12 | ```javascript 13 | const clipboard = require('electron-clipboard-extended') 14 | 15 | clipboard 16 | .on('text-changed', () => { 17 | let currentText = clipboard.readText() 18 | }) 19 | .once('text-changed', () => { 20 | console.log('TRIGGERED ONLY ONCE') 21 | }) 22 | .on('image-changed', () => { 23 | let currentIMage = clipboard.readImage() 24 | }) 25 | .startWatching(); 26 | 27 | clipboard.off('text-changed'); 28 | 29 | clipboard.stopWatching(); 30 | 31 | ``` 32 | 33 | ## Methods 34 | 35 | All methods of electron's clipboard api plus the methods listed below 36 | 37 | ### startWatching() 38 | Returns clipboard - Chainable method, Only after startWatching is called changes to clipboard will be watched. 39 | 40 | ### stopWatching() 41 | Returns clipboard - Chainable method, Changes to clipboard will not be watched after calling this 42 | 43 | ### on(event, listener), once(event, listener) 44 | * `event` String - Can be either `text-changed` or `image-changed` 45 | * `listener` Function - Callback function 46 | 47 | Returns clipboard - Chainable method 48 | 49 | ### off(event[, listener]) 50 | * `event` String - Can be either `text-changed` or `image-changed` 51 | * `listener` Function (optional) - If `listener` is not passed all listeners of the event will be removed 52 | 53 | Returns clipboard - Chainable method -------------------------------------------------------------------------------- /EventEmitter.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | 3 | class EventEmitter{ 4 | 5 | constructor(){ 6 | this.eventListeners = {} 7 | } 8 | 9 | on(event, listener){ 10 | this.eventListeners[event] = this.eventListeners[event] || [] 11 | this.eventListeners[event].push(listener) 12 | } 13 | 14 | once(event, listener){ 15 | listener.$emitOnce = true 16 | this.eventListeners[event] = this.eventListeners[event] || [] 17 | this.eventListeners[event].push(listener) 18 | } 19 | 20 | removeListener(event, listener){ 21 | if(this.eventListeners[event]){ 22 | let eventIndex = this.eventListeners[event].findIndex(eventListener => { 23 | return eventListener === listener 24 | }) 25 | if(eventIndex >= 0) this.eventListeners[event].splice(eventIndex) 26 | } 27 | } 28 | 29 | removeAllListeners(event){ 30 | if(this.eventListeners[event]) this.eventListeners[event].length = 0 31 | } 32 | 33 | emit(event, ...args){ 34 | let self = this 35 | if(self.eventListeners[event]){ 36 | self.eventListeners[event].forEach(eventListener => { 37 | eventListener.call(self, ...args) 38 | }) 39 | self.eventListeners[event].filter(eventListener => { 40 | return eventListener.$emitOnce 41 | }) 42 | .forEach(eventListener => { 43 | self.removeListener(event, eventListener) 44 | }) 45 | } 46 | } 47 | 48 | } 49 | 50 | module.exports = EventEmitter 51 | 52 | })() --------------------------------------------------------------------------------