-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackground.js
45 lines (40 loc) · 1.1 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
const contextMenuId = 'save-to-omnifocus';
chrome.contextMenus.create({
id: contextMenuId,
title: 'Save to OmniFocus',
contexts: ['page', 'selection', 'link']
});
function saveToOmniFocus(task) {
const url = task.name
? `omnifocus:///add?name=${encodeURIComponent(task.name)}¬e=${encodeURIComponent(task.note)}`
: `omnifocus:///add?name=${encodeURIComponent(task.note)}`;
chrome.tabs.create({
url,
active: false
});
}
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.executeScript(
tab.tabId,
{ file: 'tabinfo.js' },
function (result) {
if (result && result.length) {
saveToOmniFocus(result[0]);
} else {
saveToOmniFocus({
name: window.getSelection().toString() || document.title,
note: window.location.toString()
});
}
}
);
});
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === contextMenuId) {
const task = {
name: info.selectionText || tab.title,
note: info.linkUrl || info.pageUrl
};
saveToOmniFocus(task);
}
});