-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-scripts.js
279 lines (238 loc) · 8.91 KB
/
content-scripts.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* This script gets all the envato comments if there is a token and only for the items we have set
*/
chrome.storage.sync.get({
envato_token: null,
items_ids: null
}, function(items) {
if ( typeof items.envato_token !== "undefined" && items.envato_token !== "" && typeof items.items_ids !== "undefined" ) {
/**
* This is a helper function which saves data in `chrome.storage.local` under the `envato_api_results key`
* @param val
*/
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 mark_comments_as_read = function (result) {
var envato_api_results = result.envato_api_results;
if ( envato_api_results === null ) {
envato_api_results = {}
} else {
envato_api_results = JSON.parse(envato_api_results);
}
/**
* We need the items ids as array so we can iterate
* @type {Array}
*/
var ids = items.items_ids.split(',');
ids.forEach( function (item_id, index ) {
/**
* We will bind a scroll event only if:
* - the current page is an envato page with our item id in URL
* - the API has data for this item id
*/
if ( window.location.href.indexOf( item_id ) !== -1 && typeof envato_api_results[item_id] !== "undefined" ) {
var item_comments = envato_api_results[item_id]['comments'];
/**
* We create a scroll event which gets the comments within viewport and marks them as `read`
* in the chrome.storage.local object
*/
document.addEventListener("scroll", function ( event ) {
var comments = document.getElementsByClassName('comment__info');
for(var i = 0; i < comments.length; i++) {
var comment_in_viewport = withinviewport( comments[i].nextElementSibling );
if ( comment_in_viewport ) {
var comment_id = comments[i].getAttribute('id');
comment_id = comment_id.replace('comment_', '');
if ( typeof item_comments[comment_id] !== "undefined" ) {
item_comments[comment_id].read = true;
if ( typeof envato_api_results[item_id]['last'] === "undefined" || envato_api_results[item_id]['last'] < comment_id ) {
envato_api_results[item_id]['last'] = comment_id;
}
}
}
}
// debugger;
envato_api_results[item_id]['comments'] = item_comments;
save_data(envato_api_results);
}, false);
}
});
};
/**
* Since the `get` method is async we need to wait the results until we start processing data
*/
chrome.storage.local.get( 'envato_api_results', mark_comments_as_read );
}
});
/**
* Within Viewport
*
* @description Determines whether an element is completely within the browser viewport
* @author Craig Patik, http://patik.com/
* @version 1.0.0
* @date 2015-08-02
*/
(function (root, name, factory) {
// AMD
if (typeof define === 'function' && define.amd) {
define([], factory);
}
// Node and CommonJS-like environments
else if (typeof module !== 'undefined' && typeof exports === 'object') {
module.exports = factory();
}
// Browser global
else {
root[name] = factory();
}
}(this, 'withinviewport', function () {
var canUseWindowDimensions = window.innerHeight !== undefined; // IE 8 and lower fail this
/**
* Determines whether an element is within the viewport
* @param {Object} elem DOM Element (required)
* @param {Object} options Optional settings
* @return {Boolean} Whether the element was completely within the viewport
*/
var withinviewport = function withinviewport (elem, options) {
var result = false;
var metadata = {};
var config = {};
var settings;
var isWithin;
var elemBoundingRect;
var sideNamesPattern;
var sides;
var side;
var i;
// If invoked by the jQuery plugin, get the actual DOM element
if (typeof jQuery !== 'undefined' && elem instanceof jQuery) {
elem = elem.get(0);
}
if (typeof elem !== 'object' || elem.nodeType !== 1) {
throw new Error('First argument must be an element');
}
// Look for inline settings on the element
if (elem.getAttribute('data-withinviewport-settings') && window.JSON) {
metadata = JSON.parse(elem.getAttribute('data-withinviewport-settings'));
}
// Settings argument may be a simple string (`top`, `right`, etc)
if (typeof options === 'string') {
settings = {sides: options};
}
else {
settings = options || {};
}
// Build configuration from defaults and user-provided settings and metadata
config.container = settings.container || metadata.container || withinviewport.defaults.container || window;
config.sides = settings.sides || metadata.sides || withinviewport.defaults.sides || 'all';
config.top = settings.top || metadata.top || withinviewport.defaults.top || 0;
config.right = settings.right || metadata.right || withinviewport.defaults.right || 0;
config.bottom = settings.bottom || metadata.bottom || withinviewport.defaults.bottom || 0;
config.left = settings.left || metadata.left || withinviewport.defaults.left || 0;
// Use the window as the container if the user specified the body or a non-element
if (config.container === document.body || !config.container.nodeType === 1) {
config.container = window;
}
// Element testing methods
isWithin = {
// Element is below the top edge of the viewport
top: function _isWithin_top () {
return elemBoundingRect.top >= config.top;
},
// Element is to the left of the right edge of the viewport
right: function _isWithin_right () {
var containerWidth;
if (canUseWindowDimensions || config.container !== window) {
containerWidth = config.container.innerWidth;
}
else {
containerWidth = document.documentElement.clientWidth;
}
// Note that `elemBoundingRect.right` is the distance from the *left* of the viewport to the element's far right edge
return elemBoundingRect.right <= containerWidth - config.right;
},
// Element is above the bottom edge of the viewport
bottom: function _isWithin_bottom () {
var containerHeight;
if (canUseWindowDimensions || config.container !== window) {
containerHeight = config.container.innerHeight;
}
else {
containerHeight = document.documentElement.clientHeight;
}
// Note that `elemBoundingRect.bottom` is the distance from the *top* of the viewport to the element's bottom edge
return elemBoundingRect.bottom <= containerHeight - config.bottom;
},
// Element is to the right of the left edge of the viewport
left: function _isWithin_left () {
return elemBoundingRect.left >= config.left;
},
// Element is within all four boundaries
all: function _isWithin_all () {
// Test each boundary in order of most efficient and most likely to be false so that we can avoid running all four functions on most elements
// Top: Quickest to calculate + most likely to be false
// Bottom: Note quite as quick to calculate, but also very likely to be false
// Left and right are both equally unlikely to be false since most sites only scroll vertically, but left is faster
return (isWithin.top() && isWithin.bottom() && isWithin.left() && isWithin.right());
}
};
// Get the element's bounding rectangle with respect to the viewport
elemBoundingRect = elem.getBoundingClientRect();
// Test the element against each side of the viewport that was requested
sideNamesPattern = /^top$|^right$|^bottom$|^left$|^all$/;
// Loop through all of the sides
sides = config.sides.split(' ');
i = sides.length;
while (i--) {
side = sides[i].toLowerCase();
if (sideNamesPattern.test(side)) {
if (isWithin[side]()) {
result = true;
}
else {
result = false;
// Quit as soon as the first failure is found
break;
}
}
}
return result;
};
// Default settings
withinviewport.prototype.defaults = {
container: document.body,
sides: 'all',
top: 0,
right: 0,
bottom: 0,
left: 0
};
withinviewport.defaults = withinviewport.prototype.defaults;
/**
* Optional enhancements and shortcuts
*
* @description Uncomment or comment these pieces as they apply to your project and coding preferences
*/
// Shortcut methods for each side of the viewport
// Example: `withinviewport.top(elem)` is the same as `withinviewport(elem, 'top')`
withinviewport.prototype.top = function _withinviewport_top (element) {
return withinviewport(element, 'top');
};
withinviewport.prototype.right = function _withinviewport_right (element) {
return withinviewport(element, 'right');
};
withinviewport.prototype.bottom = function _withinviewport_bottom (element) {
return withinviewport(element, 'bottom');
};
withinviewport.prototype.left = function _withinviewport_left (element) {
return withinviewport(element, 'left');
};
return withinviewport;
}));