-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
71 lines (57 loc) · 2.53 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
function cleanURL(url) {
try {
// Remove Scroll-to-text fragement
const index = url.indexOf("#:~:text");
if (index !== -1) {
url = url.substring(0, index);
}
// Remove query string
url = url.split('?')[0];
return url;
} catch (e) {
return url;
}
}
document.addEventListener("DOMContentLoaded", function () {
const openRequest = indexedDB.open('saveTextDb_text-saver', 1);
openRequest.onsuccess = function (event) {
const db = event.target.result;
// Transaction to read data
const transaction = db.transaction('selectedTexts', 'readonly');
const objectStore = transaction.objectStore('selectedTexts');
// Get all records from the object store
const request = objectStore.getAll();
request.onsuccess = function () {
const savedTexts = request.result;
const savedTextElement = document.getElementById("savedText");
if (savedTexts.length > 0) {
savedTexts.forEach(function (data, index) {
// Create a div for each set of data
const dataDiv = document.createElement("div");
dataDiv.classList.add("saved-text");
// Scroll-to-text-fragement only supports Chrom >= v80
let dataText = data.selectedText.split(" ");
if (dataText.length > 10) {
dataText = encodeURIComponent(dataText.slice(0, 10).join(" ")) +
" ," +
encodeURIComponent(dataText.slice(-10).join(" "));
} else {
dataText = encodeURIComponent(dataText.join(" "));
}
dataDiv.innerHTML = `<p><a href="${cleanURL(data.url)}#:~:text=${dataText}" target="_blank">${data.title}</a></p>`;
dataDiv.innerHTML += `<p>${data.selectedText}</p>`;
// Append the div to the savedTextElement
savedTextElement.appendChild(dataDiv);
});
} else {
savedTextElement.textContent = "No saved texts found.";
}
};
request.onerror = function (event) {
console.error('Error retrieving data from IndexedDB:', event.target.error);
};
};
openRequest.onerror = function (event) {
console.error('Error opening IndexedDB:', event.target.error);
};
});