-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
75 lines (75 loc) · 1.97 KB
/
Copy pathbackground.js
File metadata and controls
75 lines (75 loc) · 1.97 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
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: 'addToTodoText',
title: 'Add to To-Do List',
contexts: ['selection']
});
chrome.contextMenus.create({
id: 'addToTodoLink',
title: 'Add Link to To-Do List',
contexts: ['link']
});
chrome.contextMenus.create({
id: 'saveToGallery',
title: 'Save to Chaos Gallery',
contexts: ['image']
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'addToTodoText') {
const todoItem = {
id: Date.now().toString(),
text: info.selectionText,
completed: false,
priority: 'medium',
createdAt: Date.now(),
sourceUrl: info.pageUrl
};
addTodoItem(todoItem);
} else if (info.menuItemId === 'addToTodoLink') {
const todoItem = {
id: Date.now().toString(),
text: info.linkUrl,
completed: false,
priority: 'medium',
createdAt: Date.now(),
isLink: true,
sourceUrl: info.pageUrl
};
addTodoItem(todoItem);
} else if (info.menuItemId === 'saveToGallery') {
saveImageToGallery(info, tab);
}
});
function addTodoItem(item) {
chrome.storage.sync.get(['chaosTodos'], (result) => {
const todos = result.chaosTodos || [];
todos.unshift(item);
chrome.storage.sync.set({ chaosTodos: todos });
});
}
function saveImageToGallery(info, tab) {
const imageData = {
url: info.srcUrl,
srcUrl: info.srcUrl,
pageUrl: info.pageUrl,
pageTitle: tab.title,
timestamp: Date.now()
};
chrome.tabs.query({ url: 'chrome-extension://*/newtab.html' }, (tabs) => {
tabs.forEach(t => {
chrome.tabs.sendMessage(t.id, {
action: 'addToGallery',
imageData: imageData
});
});
});
chrome.storage.local.get(['chaosGallery'], (result) => {
const images = result.chaosGallery || [];
images.unshift({
id: Date.now(),
...imageData
});
chrome.storage.local.set({ chaosGallery: images });
});
}