-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
69 lines (62 loc) · 2.33 KB
/
Copy pathbackground.js
File metadata and controls
69 lines (62 loc) · 2.33 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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" });
});
});
// Mark the message as processed
// processedMessages[message.type] = true;
} 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
});
// Mark the message as processed
// processedMessages[message.type] = true;
} else if (message.type === "saveChat") {
// Save the chat contents as a markdown file
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
files: ["content.js", "turndown.js"]
}, function () {
console.log('initiating saveChat message...');
// Send message to the content script
chrome.tabs.sendMessage(tabs[0].id, { type: "saveChatFile" });
});
});
// Mark the message as processed
// processedMessages[message.type] = 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';
}
}