How to detect when a Printer is added or created, updated, deleted or removed from Javascript
Product JSPrintManager Published 06/26/2020 Updated 08/01/2022 Author Neodynamic
Overview
In this walkthrough, you'll learn how to detect when a new printer is added/created, when a printer is removed/deleted, and when any changes or modifications (updates) are done on any printers available at the client machine.
NOTE: The Printers Watcher is available for Windows clients only.
Try reproducing the following sample code locally...
Follow up these steps
-
JSPrintManager Client App
Be sure you install in your dev machine JSPrintManager (JSPM) (Available for Windows, Linux, Raspberry Pi & Mac)
IMPORTANT: This small app must be installed on each client that will scan from your website! -
HTML & Script References
- Download JSPrintManager.js
- Copy all of these *.js files to the same folder where the next
index.html
page sample will be located in. - By using your favorite Web Development IDE or Text Editor, create a new HTML file naming it
index.html
Copy/paste the following code inside that html file:
<!XLSTYPE html> <html> <head> <title>JSPrintManager</title> <meta charset="utf-8" /> </head> <body> <div style="text-align:center"> <h1>Printers Watcher</h1> <hr /> <p>Detect when a new printer is added, when a printer is removed/deleted, and when any changes or modifications are done on any printers available at the client machine. Windows Only</p> <fieldset> <legend><strong>Log</strong></legend> <textarea id="txtLog" readOnly rows="30" cols="200" style="font-family:Courier New, Courier, monospace"></textarea> </fieldset> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.5/bluebird.min.js"></script> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="scripts/JSPrintManager.js"></script> <script> //WebSocket settings JSPM.JSPrintManager.auto_reconnect = true; JSPM.JSPrintManager.start(); JSPM.JSPrintManager.WS.onStatusChanged = function () { if (jspmWSStatus()) { //subscribe to printers watcher events JSPM.JSPrintManager.onPrinterCreated( data => { $("#txtLog").val($("#txtLog").val() + "PRINTER CREATED! >>>" + JSON.stringify(data) + "\r\n"); console.info(data); }, err => { console.error(err); } ); JSPM.JSPrintManager.onPrinterUpdated( data => { $("#txtLog").val($("#txtLog").val() + "PRINTER UPDATED! >>>" + JSON.stringify(data) + "\r\n"); console.info(data); }, err => { console.error(err); } ); JSPM.JSPrintManager.onPrinterDeleted( data => { $("#txtLog").val($("#txtLog").val() + "PRINTER DELETED! >>>" + JSON.stringify(data) + "\r\n"); console.info(data); }, err => { console.error(err); } ); } }; //Check JSPM WebSocket status function jspmWSStatus() { if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Open) return true; else if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Closed) { console.warn('JSPrintManager (JSPM) is not installed or not running! Download JSPM Client App from https://neodynamic.com/downloads/jspm'); return false; } else if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Blocked) { alert('JSPM has blocked this website!'); return false; } } </script> </body> </html>
- That's it! Run your website and test it.