-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
77 lines (72 loc) · 2.51 KB
/
content.js
File metadata and controls
77 lines (72 loc) · 2.51 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
//Recieves message from components to sort
chrome.runtime.onMessage.addListener(receiver);
function receiver(request, sender, sendResponse) {
if(request.id = "sort"){
sortPage();
}
}
//gets data from settings then calls sort
var minReviews = 0;
var minRating = 0;
function sortPage(){
chrome.storage.sync.get("minReviews", function(data) {
if(data){
minReviews = data.minReviews;
chrome.storage.sync.get("minRating", function(data) {
if(data){
minRating = data.minRating;
sort();
}
});
}
});
}
//for each search result gets the score out of 5 stars and it gets the number of ratings and
//stores is in results[]
function sort(){
var results = [];
var searchList = document.getElementById("s-results-list-atf");
if(!searchList) return;
//for each search result
searchList = searchList.getElementsByTagName("li");
for(var i = 0; i < searchList.length; i++){
//move showcase items to the top
var showcase = searchList[i].getElementsByClassName("acs-showcase-result-item");
if(showcase.length > 0){
results.push({numRatings: Number.MAX_SAFE_INTEGER, score:-1, li: searchList[i]});
continue;
}
var ratings = searchList[i].getElementsByClassName("a-row");
//for each rating
for(var j = 0; j < ratings.length; j++){
//retrieves score
var score = ratings[j].getElementsByClassName("a-icon-alt");
if(!score.length) continue;
score = Number(score[0].outerText.split(" ")[0]);
//retrieves number of ratings
var numRatings = ratings[j].getElementsByClassName("a-size-small a-link-normal a-text-normal");
if(!numRatings.length) continue;
numRatings = Number(numRatings[0].outerText.replace(",",""));
//checks settings
if(!score || !numRatings) continue;
if(score < minRating || numRatings < minReviews) continue;
//add list item
results.push({numRatings,score, li: searchList[i]});
}
}
//sorts results if setting is enabled
chrome.storage.sync.get("sortReviews", function(data) {
if(data && data.sortReviews == true){
results.sort(function(a,b){return b.numRatings - a.numRatings;});
}
print(results);
});
}
//Replaces the results list on Amazon whith the one in results
function print(results){
var resultsList = document.getElementById("s-results-list-atf");
resultsList.innerHTML = '';
for(var i = 0; i < results.length; i++){
resultsList.appendChild(results[i].li);
}
}