-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
155 lines (125 loc) · 4.45 KB
/
background.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
let db;
const request = indexedDB.open("UrlHistoryDB", 1);
request.onupgradeneeded = function (event) {
db = event.target.result;
const objectStore = db.createObjectStore("urls", {
keyPath: "id",
autoIncrement: true,
});
objectStore.createIndex("tabId", "tabId", { unique: false });
objectStore.createIndex("url", "url", { unique: false });
objectStore.createIndex("visitedAt", "visitedAt", { unique: false });
};
request.onsuccess = function (event) {
console.log("IndexedDB initialized successfully.");
db = event.target.result;
};
request.onerror = function (event) {
console.error("Error initializing IndexedDB:", event.target.error);
};
function storeUrlIndexedDB(tabId) {
if (!db) return;
chrome.tabs.get(tabId, (tab) => {
const currentUrl = tab.url;
const timestamp = new Date().toISOString();
const transaction = db.transaction(["urls"], "readwrite");
const objectStore = transaction.objectStore("urls");
const request = objectStore.add({
tabId,
url: currentUrl,
visitedAt: timestamp,
});
request.onsuccess = function () {
console.log("URL added to the database:", currentUrl);
};
request.onerror = function () {
console.error("Error adding URL to the database:", request.error);
};
});
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
if (changeInfo.status === "complete") {
storeUrlIndexedDB(tabId);
}
});
// Modified to retain URL history even after the tab is closed
chrome.tabs.onRemoved.addListener((tabId) => {
if (!db) return;
const transaction = db.transaction(["urls"], "readwrite");
const objectStore = transaction.objectStore("urls");
const index = objectStore.index("tabId");
const request = index.openCursor(IDBKeyRange.only(tabId));
request.onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
// Do nothing to prevent deletion of the URL from history
cursor.continue();
}
};
request.onerror = function () {
console.error("Error handling tab removal:", request.error);
};
});
// Extract search query function
function getSearchQuery(url) {
try {
const urlObj = new URL(url);
const searchParams = new URLSearchParams(urlObj.search);
// Extract the 'q' parameter from the URL
const query = searchParams.get("q");
// If the query exists, return it; otherwise, return the original URL
if (query) {
return query;
} else {
return url; // If no query is found, return the original URL
}
} catch (error) {
console.error("Invalid URL:", error);
return url; // In case of an error, fallback to the full URL
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getUrlHistory") {
if (!db) {
sendResponse({ data: [] });
return true;
}
const transaction = db.transaction(["urls"], "readonly");
const objectStore = transaction.objectStore("urls");
const allRequest = objectStore.getAll();
allRequest.onsuccess = function (event) {
const results = event.target.result.map((entry) => {
return {
...entry,
query: getSearchQuery(entry.url), // Extract and display the search query
};
});
console.log("URL history with queries retrieved:", results);
sendResponse({ data: results });
};
allRequest.onerror = function () {
sendResponse({ data: [] });
};
return true; // Keeps the messaging channel open for async response
}
// Handle clearing of the URL history
else if (request.action === "clearUrlHistory") {
if (!db) {
sendResponse({ success: false, message: "Database not initialized" });
return true;
}
const transaction = db.transaction(["urls"], "readwrite");
const objectStore = transaction.objectStore("urls");
// Clear all data from the object store
const clearRequest = objectStore.clear();
clearRequest.onsuccess = function () {
console.log("URL history cleared successfully");
sendResponse({ success: true });
};
clearRequest.onerror = function () {
console.error("Error clearing URL history");
sendResponse({ success: false, message: "Error clearing URL history" });
};
return true; // Keeps the messaging channel open for async response
}
});