-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·239 lines (209 loc) · 6.4 KB
/
script.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// We should stick to TAB being two spaces. Not actual tabs
// if your editor allows for .editorconfig please turn it on
// look up what it is. It changes the settings of an editor so they are the
// same between computers
var RedditData = {
loggedIn: false,
token: "",
refresh_token: "",
expireDate: 0,
uri: ""
};
$(document).ready(function() {
chrome.browserAction.setBadgeBackgroundColor({color: [255,0,0,255]});
const base_url = "http://www.reddit.com/r/";
var subreddit = "GlobalOffensive";
var sorting = "hot";
var subreddits = [];
var count = 1;
$("#subredditList").on("click", ".font", function(e){
subreddit = $(this).text();
emptyList();
checkForNewPosts();
});
$("#subreddit").keypress(function(e){
// NOTE: this looks ineffecient, a Reddit API must exist to check if a
// subreddit exists
if(e.which == 13){
$.ajax({
url: base_url + $("#subreddit").val() + "/" + sorting + ".json",
type: 'GET',
dataType: 'json',
beforeSend: function(xhr){
},
success: function(data, textStatus, jqXHR){
if(jqXHR.status === 200){
if(subredditExists() === false){
addSubredditToList($("#subreddit").val());
}
}
},
error: function(data, textStatus, jqXHR){
clearInput();
}
});
}
});
$("#sorting").change(function(){
sorting = $("#sorting option:selected").text();
emptyList();
checkForNewPosts();
});
$("#subredditList").change(function(){
subreddit = $("#subredditList option:selected").text();
emptyList();
checkForNewPosts();
});
$("#menu").click(function(){
this.classList.toggle("change");
if($("#menuBackground").height() === 110) {
$("input, select").animate({opacity: 0}, .4);
$("#menuBackground").animate({height: 43}, .4);
} else {
$("#menuBackground").animate({height: 110}, .4);
$("input, select").animate({opacity: 1}, .4);
}
});
function clearInput() {
$("#subreddit").val("");
}
function addSubredditToList(s) {
var item = "<option class='font' value='" + s + "'>" + s + "</option>";
$(item).appendTo("#subredditList");
subreddit = s;
clearInput();
subreddits.push(s);
chrome.storage.sync.set({'subreddits': subreddits});
}
function syncSubreddits() {
chrome.storage.sync.get('subreddits', function(data){
for(var i = 0; i < data.subreddits.length; i++) {
addSubredditToList(data.subreddits[i]);
}
});
}
function emptyList(){
$("#list li:not(:first)").remove();
titles = [];
count = 1;
}
function subredditExists(){
var r = false;
$("#subredditList li").each(function(e) {
if($(this).text() === $("#subreddit").val()) {
return r = true;
}
});
return r;
}
function handleSucess(data, jqXHR) {
var posts = data.data.children;
if(jqXHR.status == 200) {
for(var i = 0; i < posts.length; i++) {
if(posts[i].data.clicked === false) {
//dict["" + posts[i].data.title] = "" + posts[i].data.selftext;
var $item = $("#post").clone();
$item.find("#title").text("" + count++ + ") " + posts[i].data.title);
$item.find("#text").text("" + posts[i].data.selftext.substring(0, 90) + "...");
if(posts[i].data.selftext_html !== null) {
}
$item.find("#link").attr("href", "" + posts[i].data.url);
$item.appendTo("#list");
}
}
}
chrome.browserAction.setBadgeText({text: "" + (count-1)});
}
function checkWithNoAuth() {
var URL = base_url + subreddit + "/" + sorting + ".json";
$.ajax({
url: URL,
type: 'GET',
dataType: 'json',
success: function(data, textStatus, jqXHR) {
handleSucess(data, jqXHR);
}
});
}
function checkWithAuth() {
var URL = base_url + subreddit + "/" + sorting + ".json";
$.ajax({
url: URL,
type: 'GET',
dataType: 'json',
beforeSend : function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + RedditData.token);
},
success: function(data, textStatus, jqXHR) {
handleSucess(data, jqXHR);
}
});
}
function checkForNewPosts(){
if (RedditData.loggedIn) {
checkWithAuth();
} else {
checkWithNoAuth();
}
}
function sortingChanged() {
sorting = $("#sorting").val();
}
function newTitle(s) {
for(var i = 0; i < titles.length; i++){
if(titles[i] === s){
return false;
}
}
return true;
}
var data;
function getToken(code, uri, client) {
var data = "grant_type=authorization_code&code=" + code + "&redirect_uri=" + uri;
$.ajax({
url: URL,
type: 'POST',
data: data,
dataType: 'json',
beforeSend : function(xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa(client + ":" + ""));
},
success: function(data, textStatus, jqXHR) {
RedditData.loggedIn = true;
RedditData.token = data.access_token;
RedditData.refresh_token = data.refresh_token;
RedditData.expireDate = data.expires_in;
},
error: function(jqXHR, textStatus, errorThrown) {
// TODO: catch for errors
}
});
}
function authorize() {
const baseURL = "https://www.reddit.com/api/v1/authorize"
const clientID = "CAkDeHjpPz8ZWw";
const type = "code";
const rURI = "https://geaeacgampcjoddamdhmlmamimcknamn.chromiumapp.org/reddit";
const duration = "permanent";
const state = "1234";
const scope = "identity,history";
let URL = baseURL + "?client_id=" + clientID + "&response_type=" + type
+ "&state=" + state + "&redirect_uri=" + rURI + "&duration=" + duration
+ "&scope=" + scope;
chrome.identity.launchWebAuthFlow(
{'url': URL, 'interactive': true},
function(redirect_url) {
// Read this: https://github.com/reddit/reddit/wiki/OAuth2
// TODO: check for errors
var code = redirect_url.substring(redirect_url.indexOf("code="));
getToken(code, rURI, clientID);
});
}
//This is temporary for later when we will need to make the extension constantly run and do checks.
checkForNewPosts();
$("#subredditList").on("click", ".font", function(e) {
subreddit = $(this).text().substring(base_url.indexOf("/r/" + 3));
emptyList();
checkForNewPosts();
});
});