-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
135 lines (110 loc) · 3.22 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
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
// Change the current badge
var setBadge = function (text)
{
// Add badge
chrome.browserAction.setBadgeText({ text: text });
chrome.browserAction.setBadgeBackgroundColor({ color: "#F00" });
};
// Local storage
var initializeDefaultValues = function ()
{
// The key has been set, which means the default state
// values is already set
if (localStorage.getItem("default_value")) {
return;
}
// Initial state
var init_state = {
App: {
first_start_time: Date.now(),
count: 15,
items: { },
comments: { },
}
};
chrome.storage.local.set(init_state, function ()
{
console.log("Initialized the default state");
// Change key value
localStorage.setItem("default_value", true);
});
};
// Depends currently on the firebase instance
var loadComments = function (itemID)
{
return hnFire.getComments(itemID);
};
var getItemByID = function (itemID)
{
return hnFire.getItemByID(itemID);
};
initializeDefaultValues();
// Create a instance
var BASE_URL = "https://hacker-news.firebaseio.com/v0/";
var hnFire = new HNFirebase(BASE_URL);
hnFire.addListeners();
var startTime = Date.now();
// TODO: The storage state property set needs refactor
hnFire.onInitLoad = function (items)
{
console.log("Items loaded", items, Date.now() - startTime);
chrome.storage.local.get(null, function (state)
{
if (!state || !state.App || !state.App.items)
{
console.error("Trying to save items, when there is no initial structure");
return;
}
// Overwrite all the items, instead use merge ?
state.App.items = items;
// Set or overwrite existing value
chrome.storage.local.set(state, function ()
{
if (chrome.runtime.lastError)
{
console.error("Failed to store state", chrome.runtime.lastError);
}
console.log("Batch save", items, state);
});
});
};
hnFire.onNewItem = function (item)
{
// Need to get the current storage value
chrome.storage.local.get(null, function (state)
{
if (!state || !state.App || !state.App.items)
{
console.error("Trying to save items, when there is no initial structure");
return;
}
state.App.items[item.id] = item;
// Set or overwrite existing value
chrome.storage.local.set(state, function ()
{
if (chrome.runtime.lastError)
{
console.error("Failed to store state", item.id, chrome.runtime.lastError);
}
// setBadge("!");
console.log("Saved item", item.id, state);
});
});
};
console.log("Background page started");
// The script context here is chrome background script
// Do not use the require or any other node scripts here
chrome.extension.onConnect.addListener(function (port)
{
console.log("Background connected");
// Get message from popup.js
port.onMessage.addListener(function (msg)
{
if (msg.event === "popup-open")
{
setBadge("");
return;
}
console.log("Unkown receive", msg);
});
});