-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
158 lines (130 loc) · 3.7 KB
/
Copy pathbackground.js
File metadata and controls
158 lines (130 loc) · 3.7 KB
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
156
157
158
const STORAGE_KEY = "keeperItems";
const CONTEXT_MENU_ID = "save-to-keeper";
initializeContextMenu();
chrome.runtime.onInstalled.addListener(() => {
initializeContextMenu();
});
chrome.runtime.onStartup.addListener(() => {
initializeContextMenu();
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
try {
if (info.menuItemId !== CONTEXT_MENU_ID) {
return;
}
const text = (info.selectionText || "").trim();
if (!text) {
return;
}
const pageUrl = info.pageUrl || tab?.url || "";
await saveItem({
text,
note: "",
tag: "",
pageTitle: tab?.title || "Unknown page",
pageUrl,
sourceUrl: pageUrl,
jumpUrl: buildJumpUrl(pageUrl, text)
});
} catch (error) {
console.error("Keeper failed to save from the context menu:", error);
}
});
chrome.commands.onCommand.addListener(async (command) => {
if (command !== "quick-save-selection") {
return;
}
const tab = await getActiveTab();
if (!tab?.id) {
return;
}
const text = await getSelectedText(tab.id);
if (!text) {
console.log("Keeper quick save skipped because no text was selected.");
return;
}
await saveItem({
text,
note: "",
tag: "",
pageTitle: tab.title || "Unknown page",
pageUrl: tab.url || "",
sourceUrl: tab.url || "",
jumpUrl: buildJumpUrl(tab.url || "", text)
});
});
function initializeContextMenu() {
chrome.contextMenus.removeAll(() => {
if (chrome.runtime.lastError) {
console.error("Keeper failed to clear old context menus:", chrome.runtime.lastError.message);
}
chrome.contextMenus.create({
id: CONTEXT_MENU_ID,
title: "Save to Keeper",
contexts: ["selection"]
}, () => {
if (chrome.runtime.lastError) {
console.error("Keeper failed to create the context menu:", chrome.runtime.lastError.message);
}
});
});
}
async function getActiveTab() {
const tabs = await chrome.tabs.query({ active: true, lastFocusedWindow: true });
return tabs[0];
}
async function getSelectedText(tabId) {
try {
const results = await chrome.scripting.executeScript({
target: { tabId },
func: () => window.getSelection().toString().trim()
});
return results[0]?.result || "";
} catch (error) {
console.error("Keeper could not read selected text:", error);
return "";
}
}
async function saveItem(itemData) {
const items = await getStoredItems();
const newItem = {
id: crypto.randomUUID(),
text: itemData.text,
note: itemData.note || "",
tag: itemData.tag || "",
pageTitle: itemData.pageTitle || "Unknown page",
pageUrl: itemData.pageUrl || "",
sourceUrl: itemData.sourceUrl || itemData.pageUrl || "",
jumpUrl: itemData.jumpUrl || "",
status: "saved",
createdAt: new Date().toISOString()
};
items.unshift(newItem);
await chrome.storage.local.set({ [STORAGE_KEY]: items });
}
async function getStoredItems() {
const storedData = await chrome.storage.local.get(STORAGE_KEY);
return storedData[STORAGE_KEY] || [];
}
function buildJumpUrl(pageUrl, selectedText) {
if (!pageUrl || !selectedText) {
return "";
}
try {
const url = new URL(pageUrl);
if (url.protocol !== "http:" && url.protocol !== "https:") {
return "";
}
const cleanText = selectedText.replace(/\s+/g, " ").trim();
if (!cleanText) {
return "";
}
const existingHash = url.hash.replace(/^#/, "");
const textFragment = `:~:text=${encodeURIComponent(cleanText)}`;
url.hash = existingHash ? `${existingHash}${textFragment}` : textFragment;
return url.toString();
} catch (error) {
console.error("Keeper could not build jump URL:", error);
return "";
}
}