-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
70 lines (61 loc) · 1.96 KB
/
popup.js
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
70
document.getElementById('saveButton').addEventListener('click', async () => {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab) {
console.error('No active tab found');
return;
}
console.log('Executing content script...');
const result = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
// Get the div
const targetDiv = document.querySelector('div[class="flex-1 flex flex-col gap-3 px-4 max-w-3xl mx-auto w-full pt-1"]');
if (!targetDiv) {
console.error('Div not found');
return { success: false, error: 'Div not found' };
}
// Get page title and clean it for filename
const pageTitle = document.title
.replace(/[^a-z0-9]/gi, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '')
.toLowerCase()
.slice(0, 100);
// Create HTML content
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${document.title}</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
${targetDiv.outerHTML}
</body>
</html>`;
return {
success: true,
content: htmlContent,
filename: `${pageTitle}.html`
};
}
});
console.log('Script execution result:', result);
if (result && result[0] && result[0].result.success) {
const { content, filename } = result[0].result;
// Send to background script for download
chrome.runtime.sendMessage({
action: 'download',
payload: { content, filename }
}, response => {
console.log('Background script response:', response);
});
} else {
console.error('Script execution failed:', result);
}
} catch (err) {
console.error('Error in popup script:', err);
}
});