// Content script for Quality Recticel Print Helper console.log('Quality Recticel Print Helper - Content script loaded'); // Check if we're on the print module page if (window.location.pathname.includes('print_module')) { console.log('Print module page detected, initializing extension features'); // Wait for DOM to be ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initializePrintExtension); } else { initializePrintExtension(); } } function initializePrintExtension() { console.log('Initializing print extension features'); // Add extension status indicator addExtensionStatusIndicator(); // Override the print button functionality overridePrintButton(); // Add printer selection dropdown addPrinterSelection(); // Check extension status checkExtensionStatus(); } function addExtensionStatusIndicator() { // Create status indicator element const statusIndicator = document.createElement('div'); statusIndicator.id = 'extension-status'; statusIndicator.style.cssText = ` position: fixed; top: 10px; right: 10px; background: #28a745; color: white; padding: 5px 10px; border-radius: 4px; font-size: 12px; z-index: 9999; box-shadow: 0 2px 4px rgba(0,0,0,0.2); `; statusIndicator.textContent = '🖨️ Print Extension Active'; document.body.appendChild(statusIndicator); // Hide after 3 seconds setTimeout(() => { statusIndicator.style.opacity = '0'; statusIndicator.style.transition = 'opacity 0.5s'; setTimeout(() => statusIndicator.remove(), 500); }, 3000); } function overridePrintButton() { // Find the print button const printButton = document.getElementById('print-label-btn'); if (printButton) { console.log('Found print button, overriding functionality'); // Remove existing event listeners by cloning the element const newPrintButton = printButton.cloneNode(true); printButton.parentNode.replaceChild(newPrintButton, printButton); // Add new event listener for extension printing newPrintButton.addEventListener('click', handleExtensionPrint); // Update button text to indicate direct printing newPrintButton.innerHTML = '🖨️ Print Direct'; newPrintButton.title = 'Print directly to default printer (Chrome Extension)'; console.log('Print button override complete'); } else { console.log('Print button not found, will retry...'); // Retry after 1 second if button not found yet setTimeout(overridePrintButton, 1000); } } function handleExtensionPrint(event) { event.preventDefault(); event.stopPropagation(); console.log('Extension print button clicked'); // Get the label preview element const labelPreview = document.getElementById('label-preview'); if (!labelPreview) { alert('Label preview not found. Please select an order first.'); return; } // Show printing status showPrintStatus('Preparing to print...', 'info'); // Create complete HTML for printing const printHTML = createPrintableHTML(labelPreview); // Try direct printing first, then fallback chrome.runtime.sendMessage({ action: 'print_label', data: { html: printHTML, timestamp: Date.now() } }, (response) => { if (response && response.success) { handlePrintResponse(response); } else { console.log('Direct printing failed, trying fallback method'); showPrintStatus('Direct printing unavailable, using browser print...', 'info'); // Try fallback method chrome.runtime.sendMessage({ action: 'fallback_print', data: { html: printHTML, timestamp: Date.now() } }, handlePrintResponse); } }); } function createPrintableHTML(labelElement) { // Get the computed styles and create a complete HTML document const styles = ` `; const bodyContent = `