-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
102 lines (83 loc) · 3.36 KB
/
popup.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
var newTab = function (url) {
console.error("Unexpected error!")
};
// BIG OL WORKAROUND for chrome opening tabs in non-incognito windows
// when the extension is used in an incognito window
chrome.windows.getCurrent(function (w) {
if (w.incognito) {
// http://stackoverflow.com/a/26216955/765210
window.addEventListener('click', function (e) {
if (e.target.href !== undefined && e.which < 3) {
chrome.tabs.query({
currentWindow: true,
active: true
}, function (tabs) {
var t = tabs[0];
chrome.tabs.create({
url: e.target.href,
index: t.index + 1,
active: e.which == 1,
openerTabId: t.id
});
});
e.preventDefault();
}
});
}
});
sorts = {
"comments": (a, b) => {
var x = b.num_comments - a.num_comments;
if (x !== 0)
return x;
return b.score - a.score;
},
"score": (a, b) => {
return b.score - a.score;
},
"date": (a, b) => {
return b.created - a.created;
},
};
window.onload = function () {
chrome.tabs.getSelected(null, function (currentTab) {
chrome.storage.sync.get({
sort: 'score',
dark: false
}, function (items) {
document.body.classList.toggle("dark", items.dark);
bg = chrome.extension.getBackgroundPage();
data = bg.getUrlData(currentTab.id);
if (data.length == 0) {
thisUrl = bg.getUrl(currentTab.id);
chrome.tabs.create({
url: 'https://www.reddit.com/submit?url=' + encodeURIComponent(thisUrl)
});
}
posts = []
data.forEach(function (row) {
if (row.kind != 't3') {
console.error("unexpected row kind: " + row.kind);
return;
}
posts.push(row.data);
});
posts.sort(sorts[items.sort]);
html = '';
posts.forEach(function (post) {
html += '<div class="post">\n';
html += (' <div class="score">' + post.score + '</div>\n');
html += (' <div class="title"><a target="_blank" href="https://www.reddit.com' + post.permalink + '">' + post.title + '</a></div>\n');
html += (' <div class="meta">\n');
html += (' <a target="_blank" href="https://www.reddit.com' + post.permalink + '">' + post.num_comments + ' comments</a>\n');
html += (' submitted to <a target="_blank" href="https://www.reddit.com/r/' + post.subreddit + '" class="subreddit">' + post.subreddit + '</a>\n');
html += (' by <span>' + post.author + '</span>\n');
created = new Date(post.created * 1000);
html += (' at <span class="created">' + created.toDateString() + '</span>\n');
html += (" </div>\n");
html += ("</div>\n");
});
document.getElementById('content').innerHTML = html;
});
});
}