30 lines
991 B
JavaScript
30 lines
991 B
JavaScript
// background.js - Background script for handling downloads (Chrome-optimized)
|
|
|
|
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
|
if (request.action === "downloadSQL") {
|
|
const { sqlQuery, filename } = request;
|
|
|
|
// Create blob with SQL content
|
|
const blob = new Blob([sqlQuery], { type: 'text/sql' });
|
|
const url = URL.createObjectURL(blob);
|
|
|
|
// Generate filename if not provided
|
|
const finalFilename = filename || `sql_query_${new Date().toISOString().slice(0, 19).replace(/:/g, '-')}.sql`;
|
|
|
|
// Download the file
|
|
chrome.downloads.download({
|
|
url: url,
|
|
filename: finalFilename,
|
|
saveAs: true
|
|
}).then(() => {
|
|
URL.revokeObjectURL(url);
|
|
sendResponse({ success: true });
|
|
}).catch((error) => {
|
|
console.error('Download failed:', error);
|
|
sendResponse({ success: false, error: error.message });
|
|
});
|
|
|
|
return true; // Keep the message channel open for async response
|
|
}
|
|
});
|