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);
});
});
})();