Skip to content

Commit bdf475c

Browse files
committed
Initial code for new api
1 parent 493661e commit bdf475c

File tree

6 files changed

+395
-247
lines changed

6 files changed

+395
-247
lines changed

src/background.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
chrome.runtime.onStartup.addListener(setBadge);
2-
chrome.runtime.onInstalled.addListener(setBadge);
3-
chrome.storage.onChanged.addListener(setBadge);
1+
readLater.setup(null, null, false);
42

5-
function setBadge() {
6-
chrome.storage.sync.get({count: 0}, function({count}) {
7-
chrome.browserAction.setBadgeText({"text": badgeText(count)});
8-
});
9-
}
3+
chrome.runtime.onStartup.addListener(readLater.setBadge);
4+
chrome.runtime.onInstalled.addListener(readLater.setBadge);
5+
chrome.storage.onChanged.addListener(readLater.setBadge);
106

11-
function badgeText(c) {
12-
if(c > 999){
13-
return c.toString()+"+";
14-
}
15-
return c.toString();
16-
}
7+
chrome.commands.onCommand.addListener(function(command) {
8+
console.log('Command:', command);
9+
if (command === "add-url") {
10+
console.log("Adding URL");
11+
readLater.addURL();
12+
}
13+
});

src/core.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
(function() {
2+
3+
window.readLater = window.readLater || {};
4+
5+
/**
6+
Functionality needed:
7+
- Setup UI
8+
- Add URL
9+
- Remove URL
10+
- Clear all URLs
11+
- Count URLs
12+
- Update UI
13+
- Listen to changes in sync storage
14+
**/
15+
16+
readLater.setup = function(msg, links, is_popup, message){
17+
readLater.msg = msg;
18+
readLater.links = links;
19+
readLater.is_popup = is_popup;
20+
readLater.storage = chrome.storage.sync;
21+
readLater.message = message;
22+
23+
};
24+
25+
readLater.createNewURLItem = function(tab){
26+
var urlData = { "title": tab.title, "timestamp": new Date().getTime() };
27+
var urlItem = {'url': tab.url, 'data': urlData};
28+
return urlItem;
29+
};
30+
31+
readLater.addURLToStorage = function(callback){
32+
return function(urlItem){
33+
readLater.storage.get(urlItem.url, function(items){
34+
/**
35+
Add link only if it is not already present in storage
36+
**/
37+
if(!Object.keys(items).length){
38+
var item = {};
39+
item[urlItem.url] = urlItem.data;
40+
readLater.storage.set(item, callback);
41+
} else {
42+
readLater.message("URL exists.");
43+
}
44+
});
45+
46+
};
47+
};
48+
49+
readLater.addURL = function(callback){
50+
// Access the active tab
51+
chrome.tabs.query({ "active": true, "currentWindow": true }, function(
52+
tabs) {
53+
54+
if (!tabs.length) // Sanity check in case no active tab was found
55+
return;
56+
var tab = tabs[0];
57+
58+
var urlItem = readLater.createNewURLItem(tab);
59+
readLater.addURLToStorage(callback)()
60+
61+
});
62+
};
63+
64+
readLater.removeURLFromStorage = function(callback){
65+
return function(url){
66+
readLater.storage.remove(url, function(){
67+
if(typeof callback == "function"){
68+
callback();
69+
}
70+
});
71+
72+
};
73+
};
74+
75+
readLater.clearAll = function(callback) {
76+
var confirmVal = confirm("Are you sure you want to delete all links ?");
77+
if (confirmVal == true) {
78+
readLater.storage.clear(function() {
79+
callback();
80+
});
81+
}
82+
};
83+
84+
85+
86+
})();

src/core_old.js

+265
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
(function() {
2+
3+
window.readLater = window.readLater || {};
4+
5+
/**
6+
Setup variables for accessing and modifying the chrome sync storage.
7+
*/
8+
9+
readLater.storage = chrome.storage.sync;
10+
readLater.count = 0;
11+
12+
readLater.setup = function(msg, links, is_popup) {
13+
14+
console.log("Setting up readLater");
15+
readLater.msg = msg;
16+
readLater.links = links;
17+
readLater.is_popup = is_popup;
18+
19+
};
20+
21+
readLater.useCounts = function(fn) {
22+
readLater.storage.get({ count: 0 }, function({ count }) {
23+
readLater.count = count;
24+
fn({ count });
25+
});
26+
};
27+
28+
readLater.setBadge = function() {
29+
readLater.useCounts(function({ count }) {
30+
chrome.browserAction.setBadgeText({ "text": readLater.badgeText(
31+
count) });
32+
});
33+
};
34+
35+
readLater.badgeText = function(c) {
36+
if (c > 999) {
37+
return c.toString() + "+";
38+
}
39+
return c.toString();
40+
};
41+
42+
/**
43+
Create the HTML to be stored inside each list item for every link
44+
*/
45+
readLater.createLinkHTML = function(listItem, url) {
46+
var linkBtn = document.createElement("span");
47+
linkBtn.setAttribute("class", "removeBtn");
48+
linkBtn.setAttribute("name", url);
49+
var returnHTML = linkBtn.outerHTML + "<a target='_blank' href='" + url +
50+
"'>" + readLater.getIcon(url) + " " + listItem.title + "</a>";
51+
52+
return returnHTML;
53+
};
54+
55+
readLater.getIcon = function(url) {
56+
var domain = url.replace('http://', '').replace('https://', '').split(
57+
/[/?#]/)[0];
58+
var imgUrl = "http://www.google.com/s2/favicons?domain=" + domain;
59+
60+
var img = document.createElement("img");
61+
img.setAttribute('src', imgUrl);
62+
return img.outerHTML;
63+
};
64+
65+
/**
66+
Display the message given in messageStr in the message div.
67+
*/
68+
readLater.message = function(messageStr) {
69+
// Only update UI elements when running from popup.
70+
if (!readLater.is_popup) {
71+
console.log(messageStr);
72+
return;
73+
}
74+
readLater.msg.innerText = messageStr;
75+
setTimeout(function() {
76+
readLater.useCounts(function({ count }) {
77+
readLater.msg.innerText = "Total Links:" + count;
78+
});
79+
}, 1000);
80+
};
81+
82+
/**
83+
Get the child number in its parentNode
84+
*/
85+
function getChildNumber(node) {
86+
return Array.prototype.indexOf.call(node.parentNode.childNodes, node);
87+
}
88+
89+
/**
90+
Event Function to be called when the user clicks on the remove icon
91+
*/
92+
readLater.removeLink = function(e) {
93+
// body...
94+
var linkId = e.target; //Get the caller of the click event
95+
var linkDOMId = linkId.getAttribute("name"); //Get the key for the corresponding link
96+
//console.log("Removing link: "+ linkDOMId);
97+
var parentNode = linkId.parentNode.parentNode; //Get the <ul> list dom element for the current list item
98+
if (parentNode) {
99+
var i = getChildNumber(linkId.parentNode); //Get the id of the <li> item in the given parentNode
100+
101+
/**
102+
Remove the link from the sync storage
103+
*/
104+
var key = linkDOMId;
105+
readLater.storage.remove(key, function() {
106+
readLater.count--; // Reduce Count
107+
readLater.storage.set({ "count": readLater.count }); //Update count in the sync storage
108+
readLater.message("Removed Link");
109+
//console.log("Removed Link with key: "+key+"");
110+
});
111+
/**
112+
Remove the list item dom element from the UI
113+
*/
114+
parentNode.removeChild(linkId.parentNode);
115+
//console.log("Removed Child");
116+
}
117+
};
118+
119+
/**
120+
Click Event Listener for the Add button.
121+
1. Gets the title and url of the currently selected tab.
122+
2. Add the object containing the id, title for the key equal to the url of the tab.
123+
3. Increment link counter and update in sync storage
124+
4. Updated the current list to show the newly added link item.
125+
*/
126+
127+
readLater.addURL = function() {
128+
/**
129+
Access the currently selected tab of chrome browser.
130+
*/
131+
chrome.tabs.query({ "active": true, "currentWindow": true }, function(
132+
tabs) {
133+
/**
134+
Create list items and append them to the current list.
135+
*/
136+
if (!tabs.length) // Sanity check in case no active tab was found
137+
return;
138+
var tab = tabs[0];
139+
140+
var newLink = { "title": tab.title, "timestamp": new Date().getTime() };
141+
if (newLink.title.length > 50)
142+
newLink.title = newLink.title.substr(0, 50) + "...";
143+
144+
readLater.storage.get(tab.url, function(items) {
145+
//console.log(items);
146+
/**
147+
Add the link only if it is not present in the sync storage
148+
*/
149+
if (!Object.keys(items).length) {
150+
if (readLater.is_popup) {
151+
var list = document.createElement("li");
152+
list.innerHTML = readLater.createLinkHTML(newLink, tab.url);
153+
}
154+
155+
/**
156+
Update the sync storage with the list of links containing the newly added link
157+
*/
158+
var item = {};
159+
item[tab.url] = newLink;
160+
item["count"] = readLater.count + 1; //increment count in the storage
161+
readLater.storage.set(item, function() {
162+
readLater.count++;
163+
readLater.message("Saved!");
164+
readLater.links.appendChild(list);
165+
/**
166+
Attach event listeners to the newly created link for the remove button click
167+
*/
168+
if (readLater.is_popup) {
169+
list.getElementsByClassName("removeBtn")[0].addEventListener(
170+
"click", removeLink, false);
171+
}
172+
});
173+
}
174+
/**
175+
If the storage already contains the item, display message "Already Exists"
176+
*/
177+
else {
178+
readLater.message("Link Exists");
179+
}
180+
});
181+
});
182+
};
183+
184+
/**
185+
Store everything as individual items in sync storage.
186+
MAX LENGTH = 512
187+
MAX SPACE IN BYTES = 102, 400
188+
*/
189+
190+
/**
191+
Populate the extension with the list of currently stored links.
192+
Initialize the link counter.
193+
*/
194+
195+
readLater.init = function() {
196+
readLater.storage.get(function(items) {
197+
readLater.links.innerHTML = ""; // Clear links if called in an update
198+
199+
readLater.message("Loading");
200+
readLater.count = 0;
201+
console.log("Count: " + readLater.count);
202+
203+
var syncItems = new Array();
204+
205+
for (key in items) {
206+
if (key == "count") {
207+
readLater.count = items[key]; // check for count key, and if present get its value
208+
continue;
209+
}
210+
var syncItem = items[key]; // get one item from sync storage
211+
syncItem.key = key;
212+
/*console.log('Storage key "%s" equals {"%s"}.',
213+
key,
214+
syncItem.title
215+
);
216+
console.log('Check key value in syncItem: "%s" - timestamp: %d',
217+
syncItem.key,
218+
syncItem.timestamp
219+
);*/
220+
if (syncItem.title.length > 50)
221+
syncItem.title = syncItem.title.substr(0, 50) + "...";
222+
223+
syncItems.push(syncItem);
224+
}
225+
226+
syncItems.sort(function(a, b) {
227+
if (a.timestamp < b.timestamp) return -1;
228+
if (a.timestamp > b.timestamp) return 1;
229+
return 0;
230+
});
231+
//console.log('Element was sorted by timestamp');
232+
233+
for (var i = 0; i < count; i++) {
234+
var list = document.createElement("li");
235+
list.innerHTML = readLater.createLinkHTML(syncItems[i],
236+
syncItems[i].key);
237+
readLater.links.appendChild(list);
238+
//Attach event listeners to the newly created link for the remove button click
239+
240+
list.getElementsByClassName("removeBtn")[0].addEventListener(
241+
"click", readLater.removeLink, false);
242+
}
243+
readLater.message("Finished!");
244+
});
245+
};
246+
247+
/**
248+
Click Event Listener for the Clear button.
249+
1. Clears the local storage.
250+
2. Re-initialize the link counter to 0.
251+
3. Clear the current list.
252+
*/
253+
254+
readLater.clearAll = function() {
255+
var confirmVal = confirm("Are you sure you want to delete all links ?");
256+
if (confirmVal == true) {
257+
readLater.storage.clear(function() {
258+
readLater.count = 0;
259+
readLater.message("Cleared!");
260+
});
261+
readLater.links.innerHTML = "";
262+
}
263+
};
264+
265+
})();

0 commit comments

Comments
 (0)