-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
53 lines (43 loc) · 1.94 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
/* For Jenny, my Queen */
/* ============================
Background script file, handles context menu items
============================ */
browser.browserAction.setBadgeBackgroundColor({ color: '#007f00' });
// Create context menu items
browser.contextMenus.create({
id: 'clipbeardSelectionMenuItem',
title: browser.i18n.getMessage('contextMenuLabel'),
contexts: ["selection"],
}, function () {
// onCreated function.
});
browser.contextMenus.onClicked.addListener(function (info, tab) {
// Check for contextmenu item click
if (info.menuItemId === 'clipbeardSelectionMenuItem') {
var snippetText = info.selectionText;
var snippetHeader = new Date().toUTCString();
// Iterate through every storage entry and show badge, if snippet is already saved
var storageItems = browser.storage.sync.get(function (items) {
for (var element in items) {
if (items[element] === snippetText) {
// Show a star character (unicode u+2605) on an orange background
browser.browserAction.setBadgeText({ text: String.fromCharCode(9733) });
browser.browserAction.setBadgeBackgroundColor({ color: "#FFA500" });
return;
}
}
if (snippetText.length < 500) {
// Show a plus on a green background
browser.storage.sync.set({ [snippetHeader]: snippetText });
browser.browserAction.setBadgeBackgroundColor({ color: "#00CC00" });
browser.browserAction.setBadgeText({ text: '+' });
} else {
browser.browserAction.setBadgeText({ text: String.fromCharCode(9587) });
browser.browserAction.setBadgeBackgroundColor({ color: "#DC2901" });
}
});
setTimeout(function () {
browser.browserAction.setBadgeText({ text: "" });
}, 300000);
}
});