-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
53 lines (50 loc) · 2.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
46
47
48
49
50
51
52
53
function onClickHandler(info, tab) {
if (info.menuItemId == "sendtoicebox") {
chrome.storage.sync.get('pages', function(data) {
// add tab to storage
data.pages.push({title: tab.title, url: tab.url, icon: tab.favIconUrl});
chrome.storage.sync.set({pages: data.pages}, function() {
if (chrome.runtime.lastError) {
chrome.storage.sync.getBytesInUse('pages', function(count) {
console.log('You are using '+count+' bytes. Google allows 8192 bytes.');
});
chrome.notifications.create('icebox-saveerror', {
type: "basic",
message: "You are out of storage space.",
title: "Save Failure",
iconUrl: "/icon128.png"
}, function(id) {return id});
} else {
// close tab
chrome.tabs.remove(tab.id);
// pop a notification
chrome.notifications.create('icebox-savetab', {
type: "basic",
message: "Saved \""+tab.title+"\"",
title: "Saved Tab",
iconUrl: "/icon128.png"
}, function(id) {return id});
}
});
});
}
};
chrome.contextMenus.onClicked.addListener(onClickHandler);
chrome.runtime.onInstalled.addListener(function(details) {
var id = chrome.contextMenus.create({
"title": "Send page to Icebox",
"contexts": ["page", "selection"],
"id": "sendtoicebox"
});
// data migrations
var previousVersion = details.reason == "install" ? 0 : parseInt(details.previousVersion)
if (previousVersion < 1) {
// verify sync data is empty
chrome.storage.sync.get(null, function(data) {
if(!data['pages'] && !data['settings']) {
// no data, initialize
chrome.storage.sync.set({pages: [], settings: {storage: "chrome"}});
}
});
}
});