upfdated to ptint plugin
This commit is contained in:
185
py_app/chrome_extension/background.js
Normal file
185
py_app/chrome_extension/background.js
Normal file
@@ -0,0 +1,185 @@
|
||||
// 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');
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: 'Chrome printing API not available. Please ensure you are using Chrome 85+ and the extension has proper permissions.'
|
||||
});
|
||||
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 default printer or use first available
|
||||
const defaultPrinter = printers.find(p => p.isDefault) || printers[0];
|
||||
console.log('Using printer:', defaultPrinter);
|
||||
|
||||
// Create print job
|
||||
const printJob = {
|
||||
printerId: defaultPrinter.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: defaultPrinter.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: 'Chrome printing API not available'
|
||||
});
|
||||
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
|
||||
async function handleFallbackPrint(printData, sendResponse) {
|
||||
try {
|
||||
console.log('Using fallback print method');
|
||||
|
||||
// Create a new tab with the print content
|
||||
const tab = await chrome.tabs.create({
|
||||
url: 'data:text/html,' + encodeURIComponent(printData.html),
|
||||
active: false
|
||||
});
|
||||
|
||||
// Wait for tab to load, then print
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
await chrome.tabs.executeScript(tab.id, {
|
||||
code: 'window.print();'
|
||||
});
|
||||
|
||||
// Close the tab after printing
|
||||
setTimeout(() => {
|
||||
chrome.tabs.remove(tab.id);
|
||||
}, 1000);
|
||||
|
||||
sendResponse({success: true, method: 'fallback'});
|
||||
} catch (error) {
|
||||
sendResponse({success: false, error: 'Fallback print failed: ' + error.message});
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Fallback print error:', error);
|
||||
sendResponse({success: false, error: error.message});
|
||||
}
|
||||
}
|
||||
|
||||
// Keep service worker alive
|
||||
chrome.runtime.onConnect.addListener((port) => {
|
||||
console.log('Port connected:', port.name);
|
||||
});
|
||||
Reference in New Issue
Block a user