-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
214 lines (175 loc) · 5.67 KB
/
background.js
File metadata and controls
214 lines (175 loc) · 5.67 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
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
/**
* Create a repeatable task to get the latest comments every 15 mins
*/
var EnvatoCommentsChecker = (function () {
var $envato_token, $items_ids, $recheck_time, $market;
function init() {
$market = 'http://themeforest.net';
// get all the envato comments only when you have a token
chrome.storage.sync.get({
envato_token: null,
items_ids: null,
recheck_time: 60
}, function (items) {
$recheck_time = items.recheck_time;
$envato_token = items.envato_token;
$items_ids = items.items_ids;
check_for_comments();
setInterval(check_for_comments, parseInt($recheck_time + '000'));
listen_for_check_now();
listen_for_mark_as_read();
});
}
var listen_for_check_now = function () {
chrome.extension.onMessage.addListener(function (msg, call) {
if (msg.type === "check_now") {
check_for_comments();
chrome.extension.sendMessage({type: 'rebuild_popup'});
}
});
};
var listen_for_mark_as_read = function () {
chrome.extension.onMessage.addListener(function (msg, call) {
if (msg.type === "mark_as_read") {
mark_comments_as_read( msg.item_id.replace('item_id_', '') );
chrome.extension.sendMessage({type: 'rebuild_popup'});
}
});
};
var mark_comments_as_read = function( item_id ) {
if ($envato_token !== null && $envato_token !== "" && $items_ids !== null) {
chrome.storage.local.get('envato_api_results', function (result) {
if (typeof result.envato_api_results === "undefined") {
envato_api_results = {};
} else {
var envato_api_results = result.envato_api_results;
envato_api_results = JSON.parse(envato_api_results);
if (envato_api_results === '' || envato_api_results === null) {
envato_api_results = {};
}
}
if ( typeof envato_api_results[item_id] !== "undefined" && typeof envato_api_results[item_id].comments !== "undefined" ) {
Object.keys(envato_api_results[item_id].comments).forEach(function(key) {
envato_api_results[item_id].comments[key].read = true;
});
save_data(envato_api_results);
}
});
} else {
updateIcon('wrong');
}
};
var check_for_comments = function () {
if ($envato_token !== null && $envato_token !== "" && $items_ids !== null) {
get_envato_comments($envato_token, $items_ids);
} else {
updateIcon('wrong');
}
};
var save_data = function (val) {
var result = {};
result['envato_api_results'] = JSON.stringify(val);
chrome.storage.local.set(result, function () {
if (chrome.extension.lastError) {
console.log('An error occurred: ' + chrome.extension.lastError.message);
}
});
};
var get_envato_comments = function (envato_token, ids) {
chrome.storage.local.get('envato_api_results', function (result) {
var items_ids = ids.split(',');
if (typeof result.envato_api_results === "undefined") {
envato_api_results = {};
} else {
var envato_api_results = result.envato_api_results;
envato_api_results = JSON.parse(envato_api_results);
if (envato_api_results === '' || envato_api_results === null) {
envato_api_results = {};
}
}
/**
* For each item, ask for data from envato API
*/
items_ids.forEach(function (item_id, index) {
check_item_comments( item_id, envato_api_results, envato_token);
});
});
};
var check_item_comments = function ( item_id, envato_api_results, envato_token ) {
var xhr = new XMLHttpRequest();
xhr.open('GET', "https://api.envato.com/v1/discovery/search/search/comment?item_id=" + item_id + "&page=1&sort_by=newest", true);
xhr.setRequestHeader('Authorization', 'Bearer ' + envato_token);
// see if I managed to get into the right json with the credentials
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 403) {
// @todo send a notifaction to popup
console.log("Oh no, it does not exist!");
}
}
};
xhr.onload = function () {
var response = JSON.parse(xhr.responseText),
new_item_json = {},
notify = false,
item_name = null;
response.matches.forEach(function (comment, comment_count) {
if (
typeof envato_api_results[item_id] !== "undefined"
&& typeof envato_api_results[item_id]['comments'] !== "undefined"
&& envato_api_results[item_id]['name'] !== null
&& typeof envato_api_results[item_id]['comments'][comment.id] !== "undefined"
&& typeof envato_api_results[item_id]['comments'][comment.id].read !== "undefined"
) {
new_item_json[comment.id] = envato_api_results[item_id]['comments'][comment.id]
} else {
notify = true;
var comment_text = comment.highlightable[0];
comment_text = comment_text.substring(9);
comment_text = strip_html(comment_text);
comment_text = comment_text.substring(0, 60);
new_item_json[comment.id] = {
comment_content: comment_text,
comment_link: $market + '/comments/' + comment.id
};
}
if (comment_count === 0) {
item_name = comment.item_name;
}
});
envato_api_results[item_id] = {
name: item_name,
comments: new_item_json
};
// save_data({});
save_data(envato_api_results);
if (notify === true) {
updateIcon('new');
} else {
updateIcon();
}
};
xhr.send();
};
var strip_html = function (html) {
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
};
// update the browser icon as the extension state is
var updateIcon = function ($icon_name) {
if (typeof $icon_name === "undefined") {
return;
}
chrome.browserAction.setIcon({
path: {
"19": "icons/icon-" + $icon_name + "-19.png",
"38": "icons/icon-" + $icon_name + "-38.png"
}
});
};
return {
init: init
}
})();
EnvatoCommentsChecker.init();