-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
126 lines (110 loc) · 4.54 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
document.getElementById("newGroupBtn").addEventListener("click", () => {
const groupName = prompt("Enter a name for the new group:");
if (groupName) {
chrome.storage.local.get("groups", (data) => {
let groups = data.groups || {};
groups[groupName] = [];
chrome.storage.local.set({ groups }, loadGroups);
});
}
});
document.getElementById("pinSidebarBtn").addEventListener("click", openDetachedSidebar);
document.getElementById("exportBtn").addEventListener("click", toggleExportMenu);
document.getElementById("exportMarkdownBtn").addEventListener("click", () => exportGroups("markdown"));
document.getElementById("exportJsonBtn").addEventListener("click", () => exportGroups("json"));
document.getElementById("extractMarkdownBtn").addEventListener("click", extractPageToMarkdown);
function openDetachedSidebar() {
const width = 400;
const height = screen.height;
chrome.windows.create({
url: chrome.runtime.getURL("popup.html"),
type: "popup",
width: width,
height: height,
left: screen.width - width,
top: 0
});
}
function toggleExportMenu() {
const exportMenu = document.getElementById("exportMenu");
exportMenu.style.display = exportMenu.style.display === "none" ? "block" : "none";
}
// Function to extract content from the current page and convert it to Markdown
function extractPageToMarkdown() {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: convertPageToMarkdown,
}, (results) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
alert("Error: Unable to extract content.");
return;
}
const markdownContent = results[0].result;
if (markdownContent) {
downloadFile(markdownContent, "PageContent.md");
} else {
alert("No content found to extract.");
}
});
});
}
// This function runs in the context of the active page to extract and convert content to Markdown
function convertPageToMarkdown() {
let mainContent = document.querySelector('.v-slot-main-content');
if (!mainContent) return 'No element with class "v-slot-main-content" found.';
let all_text = '';
let inTable = false;
let tableHeaders = [];
function traverseNodes(node) {
if (node.nodeType === Node.ELEMENT_NODE) {
let tag = node.tagName.toLowerCase();
if (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(tag)) {
let headerLevel = parseInt(tag.charAt(1));
let prefix = '#'.repeat(headerLevel);
all_text += prefix + ' ' + node.textContent.trim() + '\n\n';
} else if (tag === 'pre') {
all_text += '```\n' + node.textContent.trim() + '\n```\n\n';
} else if (tag === 'p') {
all_text += node.textContent.trim() + '\n\n';
} else if (tag === 'li') {
all_text += '- ' + node.textContent.trim() + '\n';
} else if (tag === 'table') {
inTable = true;
tableHeaders = [];
all_text += '\n';
} else if (tag === 'th' && inTable) {
tableHeaders.push(node.textContent.trim());
} else if (tag === 'tr' && inTable) {
if (tableHeaders.length > 0) {
all_text += '| ' + tableHeaders.join(' | ') + ' |\n';
all_text += '| ' + tableHeaders.map(() => '---').join(' | ') + ' |\n';
tableHeaders = [];
}
} else if (tag === 'td' && inTable) {
all_text += '| ' + node.textContent.trim() + ' ';
} else if (tag === 'tr' && inTable) {
all_text += '|\n';
} else if (tag === 'table' && inTable) {
inTable = false;
all_text += '\n';
} else if (tag === 'div' && !node.className && getComputedStyle(node).backgroundColor === 'rgb(238, 238, 238)') {
all_text += '```\n' + node.textContent.trim() + '\n```\n\n';
}
Array.from(node.childNodes).forEach(traverseNodes);
}
}
traverseNodes(mainContent);
return all_text;
}
// Function to download the Markdown file
function downloadFile(content, filename) {
const blob = new Blob([content], { type: "text/markdown" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename;
link.click();
URL.revokeObjectURL(url);
}