-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
62 lines (51 loc) · 1.88 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
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "textSelection_menu",
title: "Save selected text",
contexts: ["selection"]
});
});
// Open the popup in a new tab when the user clicks on the extension icon
chrome.action.onClicked.addListener(() => {
chrome.tabs.create({ url: "index.html" });
});
// Open the IndexedDB database
const openRequest = indexedDB.open('saveTextDb_text-saver', 1);
// Create or upgrade the database structure
openRequest.onupgradeneeded = function(event) {
const db = event.target.result;
// Create an object store with an auto-incrementing key
const objectStore = db.createObjectStore('selectedTexts', { autoIncrement: true });
objectStore.createIndex('url', 'url', { unique: false });
objectStore.createIndex('title', 'title', { unique: false });
};
// Event handler when the database is successfully opened
openRequest.onsuccess = function(event) {
const db = event.target.result;
// Function to save selected text to IndexedDB
function saveSelectedText(selectedText, url, title) {
const transaction = db.transaction('selectedTexts', 'readwrite');
const objectStore = transaction.objectStore('selectedTexts');
const data = {
selectedText: selectedText,
url: url,
title: title,
};
const request = objectStore.add(data);
request.onsuccess = function() {
console.log('Selected text saved:', selectedText);
};
request.onerror = function(event) {
console.error('Error saving selected text:', event.target.error);
};
}
// Event listener for the context menu item click
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === 'textSelection_menu') {
const selectedText = info.selectionText;
const url = tab.url;
const title = tab.title;
saveSelectedText(selectedText, url, title);
}
});
};