-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.html
More file actions
61 lines (61 loc) · 1.48 KB
/
popup.html
File metadata and controls
61 lines (61 loc) · 1.48 KB
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
<html>
<style>
body {
width: 500px;
font-size: 16px;
}
ul {
margin: 20px;
padding: 0;
}
li {
margin-bottom: 10px;
}
</style>
<body>
...
<script>
var getPageUrl = function (callback) {
chrome.tabs.getSelected(null, function (tab) { callback(tab.url); });
};
var getRedditLinks = function (url, callback) {
var xhr = new XMLHttpRequest();
xhr.open(
'GET',
'http://www.reddit.com/api/info.json?url=' + encodeURIComponent(url)
);
xhr.onload = function () {
var response = JSON.parse(xhr.responseText);
callback(response.data.children);
};
xhr.send(null);
};
var displayLinks = function (links, callback) {
if (links.length == 0) {
document.body.textContent = 'nada';
return;
}
var ul = document.createElement('ul');
links.forEach(function (link) {
var a = document.createElement('a');
a.href = 'http://www.reddit.com' + link.data.permalink;
a.textContent = link.data.title;
a.onclick = function () {
callback(a.href);
return false;
};
var li = document.createElement('li');
li.appendChild(a);
ul.appendChild(li);
});
document.body.appendChild(ul);
document.body.removeChild(document.body.firstChild);
};
getPageUrl(function (url) {
getRedditLinks(url, function (links) {
displayLinks(links, function (url) { chrome.tabs.create({ url: url }); });
});
});
</script>
</body>
</html>