263 lines
7.8 KiB
JavaScript
263 lines
7.8 KiB
JavaScript
// Background service worker for Chrome extension
|
|
console.log('Quality Recticel Print Helper - Background script loaded');
|
|
|
|
// Listen for messages from content script
|
|
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
|
console.log('Background received message:', request);
|
|
|
|
if (request.action === 'print_label') {
|
|
handlePrintLabel(request.data, sendResponse);
|
|
return true; // Keep message channel open for async response
|
|
}
|
|
|
|
if (request.action === 'fallback_print') {
|
|
handleFallbackPrint(request.data, sendResponse);
|
|
return true;
|
|
}
|
|
|
|
if (request.action === 'get_printers') {
|
|
getPrinters(sendResponse);
|
|
return true;
|
|
}
|
|
|
|
if (request.action === 'check_extension') {
|
|
sendResponse({status: 'installed', version: chrome.runtime.getManifest().version});
|
|
return true;
|
|
}
|
|
});
|
|
|
|
// Function to handle label printing
|
|
async function handlePrintLabel(printData, sendResponse) {
|
|
try {
|
|
console.log('Attempting to print label with data:', printData);
|
|
|
|
// Check if printing API is available
|
|
if (!chrome.printing || !chrome.printing.getPrinters) {
|
|
console.error('Chrome printing API not available - browser may be Brave, Edge, or older Chrome');
|
|
sendResponse({
|
|
success: false,
|
|
error: 'Direct printing API not available in this browser. The extension works best in Google Chrome 85+. Using fallback method instead.'
|
|
});
|
|
return;
|
|
}
|
|
|
|
// Get available printers
|
|
const printers = await chrome.printing.getPrinters();
|
|
console.log('Available printers:', printers);
|
|
|
|
if (printers.length === 0) {
|
|
sendResponse({success: false, error: 'No printers found. Please ensure a printer is installed and set as default.'});
|
|
return;
|
|
}
|
|
|
|
// Find selected printer or use default/first available
|
|
let selectedPrinter;
|
|
if (printData.printerId && printData.printerId !== 'default') {
|
|
selectedPrinter = printers.find(p => p.id === printData.printerId);
|
|
}
|
|
|
|
if (!selectedPrinter) {
|
|
selectedPrinter = printers.find(p => p.isDefault) || printers[0];
|
|
}
|
|
|
|
console.log('Using printer:', selectedPrinter);
|
|
|
|
// Create print job
|
|
const printJob = {
|
|
printerId: selectedPrinter.id,
|
|
ticket: {
|
|
version: '1.0',
|
|
print: {
|
|
color: {
|
|
type: 'STANDARD_MONOCHROME'
|
|
},
|
|
duplex: {
|
|
type: 'NO_DUPLEX'
|
|
},
|
|
page_orientation: {
|
|
type: 'PORTRAIT'
|
|
},
|
|
copies: {
|
|
copies: 1
|
|
},
|
|
dpi: {
|
|
horizontal_dpi: 300,
|
|
vertical_dpi: 300
|
|
},
|
|
media_size: {
|
|
width_microns: 210000, // A4 width
|
|
height_microns: 297000 // A4 height
|
|
},
|
|
collate: {
|
|
collate: false
|
|
}
|
|
}
|
|
},
|
|
documentBlob: new Blob([printData.html], {type: 'text/html'})
|
|
};
|
|
|
|
// Submit print job
|
|
const result = await chrome.printing.submitJob(printJob);
|
|
console.log('Print job result:', result);
|
|
|
|
if (result.status === 'OK') {
|
|
sendResponse({success: true, jobId: result.jobId});
|
|
|
|
// Store successful print in extension storage
|
|
chrome.storage.local.set({
|
|
lastPrint: {
|
|
timestamp: Date.now(),
|
|
jobId: result.jobId,
|
|
printer: selectedPrinter.displayName
|
|
}
|
|
});
|
|
} else {
|
|
sendResponse({success: false, error: result.status});
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Print error:', error);
|
|
sendResponse({success: false, error: error.message});
|
|
}
|
|
}
|
|
|
|
// Function to get available printers
|
|
async function getPrinters(sendResponse) {
|
|
try {
|
|
// Check if printing API is available
|
|
if (!chrome.printing || !chrome.printing.getPrinters) {
|
|
sendResponse({
|
|
success: false,
|
|
error: 'Direct printing API not available in this browser (works in Chrome only)'
|
|
});
|
|
return;
|
|
}
|
|
|
|
const printers = await chrome.printing.getPrinters();
|
|
sendResponse({success: true, printers: printers});
|
|
} catch (error) {
|
|
console.error('Error getting printers:', error);
|
|
sendResponse({success: false, error: error.message});
|
|
}
|
|
}
|
|
|
|
// Extension installation/startup
|
|
chrome.runtime.onInstalled.addListener((details) => {
|
|
console.log('Quality Recticel Print Helper installed:', details);
|
|
|
|
// Set initial storage values
|
|
chrome.storage.local.set({
|
|
extensionVersion: chrome.runtime.getManifest().version,
|
|
installDate: Date.now()
|
|
});
|
|
});
|
|
|
|
// Fallback print method using tabs API (works in Brave, Edge, Chrome)
|
|
async function handleFallbackPrint(printData, sendResponse) {
|
|
try {
|
|
console.log('Using fallback print method for Brave/Edge/Chrome');
|
|
|
|
// Create enhanced HTML with better print styles
|
|
const enhancedHTML = `
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Quality Recticel Label</title>
|
|
<style>
|
|
@page {
|
|
margin: 10mm;
|
|
size: A4;
|
|
}
|
|
body {
|
|
margin: 0;
|
|
padding: 0;
|
|
font-family: Arial, sans-serif;
|
|
background: white;
|
|
}
|
|
.print-container {
|
|
width: 100%;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
min-height: 100vh;
|
|
}
|
|
.label-wrapper {
|
|
transform: scale(1.5);
|
|
transform-origin: center;
|
|
page-break-inside: avoid;
|
|
}
|
|
@media print {
|
|
body { padding: 0; margin: 0; }
|
|
.print-container { min-height: auto; padding: 20px 0; }
|
|
.no-print { display: none !important; }
|
|
}
|
|
.print-instructions {
|
|
position: fixed;
|
|
top: 10px;
|
|
right: 10px;
|
|
background: #007bff;
|
|
color: white;
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
font-size: 14px;
|
|
z-index: 1000;
|
|
}
|
|
</style>
|
|
<script>
|
|
window.onload = function() {
|
|
// Auto-print after 1 second
|
|
setTimeout(() => {
|
|
window.print();
|
|
// Auto-close after printing (or after 10 seconds)
|
|
setTimeout(() => {
|
|
window.close();
|
|
}, 2000);
|
|
}, 1000);
|
|
};
|
|
|
|
// Handle print events
|
|
window.onbeforeprint = function() {
|
|
console.log('Print dialog opened');
|
|
};
|
|
|
|
window.onafterprint = function() {
|
|
console.log('Print dialog closed');
|
|
setTimeout(() => {
|
|
window.close();
|
|
}, 500);
|
|
};
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<div class="print-instructions no-print">
|
|
🖨️ Printing will start automatically...<br>
|
|
<small>If dialog doesn't appear, press Ctrl+P</small>
|
|
</div>
|
|
<div class="print-container">
|
|
<div class="label-wrapper">
|
|
${printData.html.replace(/<!DOCTYPE html>|<html>|<\/html>|<head>.*?<\/head>|<body>|<\/body>/gi, '')}
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
`;
|
|
|
|
// Create a new tab with the enhanced print content
|
|
const tab = await chrome.tabs.create({
|
|
url: 'data:text/html;charset=utf-8,' + encodeURIComponent(enhancedHTML),
|
|
active: true // Make active so user can see the print dialog
|
|
});
|
|
|
|
console.log('Created print tab:', tab.id);
|
|
sendResponse({success: true, method: 'fallback_tab', tabId: tab.id});
|
|
|
|
} catch (error) {
|
|
console.error('Fallback print error:', error);
|
|
sendResponse({success: false, error: 'Browser print fallback failed: ' + error.message});
|
|
}
|
|
}
|
|
|
|
// Keep service worker alive
|
|
chrome.runtime.onConnect.addListener((port) => {
|
|
console.log('Port connected:', port.name);
|
|
}); |