-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
236 lines (208 loc) · 6.99 KB
/
background.js
File metadata and controls
236 lines (208 loc) · 6.99 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Background service worker for Ankur's Wiki Notes extension
// Open side panel when extension icon is clicked
chrome.action.onClicked.addListener((tab) => {
chrome.sidePanel.open({ windowId: tab.windowId });
});
// Create context menu items
function createContextMenus() {
// Remove existing menus first to avoid duplicates
chrome.contextMenus.removeAll(() => {
chrome.contextMenus.create({
id: 'addAsHeading',
title: "Add to Ankur's Wiki Notes as Heading",
contexts: ['selection']
});
chrome.contextMenus.create({
id: 'addAsParagraph',
title: "Add to Ankur's Wiki Notes as Paragraph",
contexts: ['selection']
});
// Highlight submenu with colors
chrome.contextMenus.create({
id: 'highlightMenu',
title: 'Highlight as...',
contexts: ['selection']
});
chrome.contextMenus.create({
id: 'highlightYellow',
parentId: 'highlightMenu',
title: '🟨 Yellow (Important)',
contexts: ['selection']
});
chrome.contextMenus.create({
id: 'highlightGreen',
parentId: 'highlightMenu',
title: '🟩 Green (Definition)',
contexts: ['selection']
});
chrome.contextMenus.create({
id: 'highlightBlue',
parentId: 'highlightMenu',
title: '🟦 Blue (Question)',
contexts: ['selection']
});
chrome.contextMenus.create({
id: 'highlightRed',
parentId: 'highlightMenu',
title: '🟥 Red (Critical)',
contexts: ['selection']
});
chrome.contextMenus.create({
id: 'highlightPurple',
parentId: 'highlightMenu',
title: '🟪 Purple (Reference)',
contexts: ['selection']
});
chrome.contextMenus.create({
id: 'removeHighlight',
title: 'Remove Highlight',
contexts: ['all']
});
console.log('Context menus created');
});
}
// Create menus on install and startup
chrome.runtime.onInstalled.addListener(() => {
console.log('Extension installed/updated');
createContextMenus();
});
chrome.runtime.onStartup.addListener(() => {
console.log('Extension started');
createContextMenus();
});
// Also create on service worker activation
createContextMenus();
// Handle context menu clicks
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
const selectedText = info.selectionText;
const pageUrl = tab.url;
console.log('Context menu clicked:', info.menuItemId, 'Text:', selectedText);
if (info.menuItemId === 'addAsHeading') {
// Open side panel first
await chrome.sidePanel.open({ windowId: tab.windowId });
// Wait a bit for side panel to load, then send message
setTimeout(() => {
chrome.runtime.sendMessage({
action: 'addToNotes',
text: selectedText,
type: 'heading',
url: pageUrl,
pageTitle: tab.title
}, (response) => {
if (chrome.runtime.lastError) {
console.log('Error sending message:', chrome.runtime.lastError);
} else {
console.log('Message sent successfully');
}
});
}, 500);
} else if (info.menuItemId === 'addAsParagraph') {
// Open side panel first
await chrome.sidePanel.open({ windowId: tab.windowId });
// Wait a bit for side panel to load, then send message
setTimeout(() => {
chrome.runtime.sendMessage({
action: 'addToNotes',
text: selectedText,
type: 'paragraph',
url: pageUrl,
pageTitle: tab.title
}, (response) => {
if (chrome.runtime.lastError) {
console.log('Error sending message:', chrome.runtime.lastError);
} else {
console.log('Message sent successfully');
}
});
}, 500);
} else if (info.menuItemId.startsWith('highlight')) {
// Determine color based on menu item
let color = 'yellow'; // default
if (info.menuItemId === 'highlightYellow') color = 'yellow';
else if (info.menuItemId === 'highlightGreen') color = 'green';
else if (info.menuItemId === 'highlightBlue') color = 'blue';
else if (info.menuItemId === 'highlightRed') color = 'red';
else if (info.menuItemId === 'highlightPurple') color = 'purple';
// Send message to content script to highlight with color
try {
const response = await chrome.tabs.sendMessage(tab.id, {
action: 'highlightText',
text: selectedText,
color: color
});
console.log('Highlight response:', response);
} catch (error) {
console.error('Error highlighting:', error);
// Try to inject content script if it's not already there
try {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
});
// Try again after injection
setTimeout(async () => {
try {
await chrome.tabs.sendMessage(tab.id, {
action: 'highlightText',
text: selectedText,
color: color
});
} catch (e) {
console.error('Still failed after injection:', e);
}
}, 100);
} catch (injectionError) {
console.error('Could not inject content script:', injectionError);
}
}
} else if (info.menuItemId === 'removeHighlight') {
// Send message to content script to remove highlight at clicked position
try {
const response = await chrome.tabs.sendMessage(tab.id, {
action: 'removeHighlightAtClick'
});
console.log('Remove highlight response:', response);
} catch (error) {
console.error('Error removing highlight:', error);
}
}
});
// Listen for keyboard commands
chrome.commands.onCommand.addListener(async (command) => {
console.log('Command received:', command);
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (command === 'open-sidepanel') {
chrome.sidePanel.open({ windowId: tab.windowId });
} else if (command === 'highlight-yellow') {
// Send message to content script to highlight with yellow
try {
await chrome.tabs.sendMessage(tab.id, {
action: 'highlightSelection',
color: 'yellow'
});
} catch (e) {
console.error('Could not highlight:', e);
}
} else if (command === 'search-notes') {
// Open side panel and trigger search
await chrome.sidePanel.open({ windowId: tab.windowId });
setTimeout(() => {
chrome.runtime.sendMessage({ action: 'focusSearch' });
}, 300);
} else if (command === 'quick-note') {
// Open side panel and focus editor
await chrome.sidePanel.open({ windowId: tab.windowId });
setTimeout(() => {
chrome.runtime.sendMessage({ action: 'focusEditor' });
}, 300);
}
});
// Listen for messages from content script or side panel
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('Message received:', request);
if (request.action === 'openSidePanel') {
chrome.sidePanel.open({ windowId: sender.tab.windowId });
sendResponse({ success: true });
}
return true; // Keep channel open for async response
});