0

I want to make an extension on Microsoft Edge with TamperMonkey, so whenever i go to the page and click on the files it opens printing view on new tab for both files and i want it to be selected the right printer for them based on their file name. For the first file which is label i need it to be always selected Zebra printer when i click on it. For the second file to be always selected the HP printer. So i wont need to manually search and change the printers everytime.

This is my code, when you click on files it opens them in new tab but its not selecting the right printer, it always selects the last used printer for both files. Can you tell me why it wont work?

(function() { 'use strict';

// Define the printer names for each file
const printerNames = {
   'nameofthefirstfile': {
        printerName: 'printername',
        ipAddress: '182.060.200.146'
    },
    'nameofthesecondfile': {
        printerName: 'printername',
        ipAddress: '182.50.16.18'
    }
};

// Find the file links
var fileLinks = document.querySelectorAll('.docs-list li a');

// Function to print a file using a specific printer
function printFileWithPrinter(fileLink) {
    var fileName = fileLink.innerText.trim();
    var printerName = printerNames[fileName];
    if (printerName) {
        // Open the file in a new tab
        var fileWindow = window.open(fileLink.href, '_blank');

        // Wait for the new tab to fully load
        fileWindow.addEventListener('load', function() {
            // Print the file using the specified printer
            setTimeout(function() {
                fileWindow.print({ printerName: printerName });
            }, 1000); // Adjust the delay if needed
        });
    }
}

// Add a click event listener to each file link
fileLinks.forEach(function(fileLink) {
    fileLink.addEventListener('click', function(e) {
        e.preventDefault();
        // Call the function to print the file
        printFileWithPrinter(fileLink);
    });
});

})();

infl
  • 1
  • 2
  • The answer is [you can't choose a specific printer](https://stackoverflow.com/questions/17349440/selecting-specific-printer-by-javascript) with JavaScript. – double-beep Jun 24 '23 at 12:05
  • Thank you for your answer. Do you know if something like this can be done with Selenium? Or maybe some other solution for automatically choosing printers based on their file name? – infl Jun 28 '23 at 05:35
  • I think you can't choose printer for users in browser. The printers supposed to be different for different computers. – Yu Zhou Jun 30 '23 at 09:47

0 Answers0