forked from joetech/chatbot-save-file
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
48 lines (44 loc) · 1.57 KB
/
background.js
File metadata and controls
48 lines (44 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
console.log('background listening...');
// Listen for messages from the popup
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.type === "addSaveButton") {
console.log('adding save buttons...');
// Inject content script into the active tab
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
files: ["content.js"]
}, function () {
console.log('initiating addSaveButton message...');
// Send message to the content script
chrome.tabs.sendMessage(tabs[0].id, { type: "addSaveButton" });
});
});
} else if (message.type === "codeFound") {
// Initiate the code download
console.log('download message filename: ', message.filename);
const fileExtension = message.filename ? message.filename.split('.').pop() : 'txt';
const mimeType = getFileMimeType(fileExtension);
chrome.downloads.download({
url: `data:${mimeType};charset=utf-8,` + encodeURIComponent(message.code),
filename: message.filename || "code.txt",
saveAs: true
});
}
});
// Function to get the MIME type based on the file extension
function getFileMimeType(extension) {
switch (extension) {
case 'txt':
return 'text/plain';
case 'js':
return 'text/javascript';
case 'html':
return 'text/html';
case 'css':
return 'text/css';
// Add more file extensions and corresponding MIME types as needed
default:
return 'application/octet-stream';
}
}