-
Notifications
You must be signed in to change notification settings - Fork 0
/
netflixQueueSort.js
216 lines (202 loc) · 7.04 KB
/
netflixQueueSort.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
var sortInstant = true; //Whether to push (sorted) instantWatch to bottom of list
var sortType = 'predicted'; //Sorts on Predicted or Average Ratings
var secSort = 'age'; //Secondary sort on Age or Alphabetical
var sortOrder = 'desc'; //Sort ascending or descending
var secSortOrder = 'asc'; //Secondary sort order
var maintainSeries = true; //Lump series, in order, on the list
var tempObj = {}, dvdQueue = [], seriesObject = {}, instantWatchArray = [], starClass, starText, seriesRatings = {};
var queue = document.querySelectorAll('#MainQueueForm tr') //full array (for caching)
var queueLength = queue.length //full length of array
for (var i = 1; i < queueLength; i++) { // first tr is junk data
//Identify series
if (queue[i].className.length > 0) {
tempObj.isSeries = true;
tempObj.series = queue[i].className;
} else {
tempObj.isSeries = false;
}
tempObj.currentOrder = parseInt(queue[i].querySelectorAll('.pr input.o')[0].value) // order number
tempObj.title = queue[i].querySelectorAll('.tt .title')[0].innerText //title
tempObj.discNumber = queue[i].dataset.mid; //disc number (primarily for sorting series)
//Get both predicted and average ratings
//Ratings only are listed for the first in a series
if (queue[i].querySelectorAll('.stbrMaskFg')[0]) {
//Predicted ratings
starClass = queue[i].querySelectorAll('.stbrMaskFg')[0].className.split(' ');
for (var z = 0, s = starClass.length; z < s; z++) {
if(starClass[z].indexOf('sbmf-') > -1) {
tempObj.predictedRating = parseInt(starClass[z].replace('sbmf-', ''), 10)/10;
if(tempObj.isSeries){seriesRatings[tempObj.series + '-predicted'] = tempObj.predictedRating;}
}
}
//Average ratings
starText = queue[i].querySelectorAll('.stbrMaskFg')[0].innerText.split(': ');
tempObj.rating = parseFloat(starText[1]);
if(tempObj.isSeries){seriesRatings[tempObj.series] = tempObj.rating;}
//If is not the first in a series
} else if (tempObj.isSeries && seriesRatings[tempObj.series] && seriesRatings[tempObj.series + '-predicted']) {
tempObj.predictedRating = seriesRatings[tempObj.series + '-predicted'];
tempObj.rating = seriesRatings[tempObj.series];
}
//Whether is available for instant watch
queue[i].querySelectorAll('.wi')[0].innerText.length > 0 ? tempObj.isInstantWatch = true : tempObj.isInstantWatch = false;
//Push series to new placeholder object to maintain grouping
if (tempObj.isSeries && maintainSeries) {
if (seriesObject[tempObj.series] === undefined) {seriesObject[tempObj.series] = [];}
seriesObject[tempObj.series].push(tempObj);
}
//Push instantWatch to new placeholder array to maintain grouping
if (tempObj.isInstantWatch && sortInstant) {
instantWatchArray.push(tempObj);
}
dvdQueue.push(tempObj);
tempObj = {};
}
function compareSort(a, b, sortOrder) {
if(sortOrder === 'desc') {
return b-a;
} else {
return a-b;
}
}
function sortQueue(sortType, sortOrder, queue, startsAt, lumpSeries) {
var offset = startsAt || 0;
var accountedforSeries = {};
var seriesLump = lumpSeries || false;
queue = queue.sort(function(a, b){
//Sort by Predicted Rating (Descending)
if(sortType === 'predicted') {
//if same rating, sort by secSort
if (b.predictedRating === a.predictedRating) {
if(secSort === 'age') {
if(secSortOrder === 'desc') {
return b.discNumber-a.discNumber;
} else {
return a.discNumber-b.discNumber;
}
} else {
if(secSortOrder === 'desc') {
return b.title-a.title;
} else {
return a.title-b.title;
}
}
} else {
if(sortOrder === 'desc') {
return b.predictedRating-a.predictedRating;
} else {
return a.predictedRating-b.predictedRating;
}
}
//Sort by Average Rating (Descending)
} else if(sortType === 'average') {
//if same rating, sort by age
if (b.rating === a.rating) {
if(secSort === 'age') {
if(secSortOrder === 'desc') {
return b.discNumber-a.discNumber;
} else {
return a.discNumber-b.discNumber;
}
} else {
if(secSortOrder === 'desc') {
return b.title-a.title;
} else {
return a.title-b.title;
}
}
} else {
if(sortOrder === 'desc') {
return b.rating-a.rating;
} else {
return a.rating-b.rating;
}
}
} else if(sortType === 'series') {
return a.discNumber-b.discNumber;
}
});
var numbered = 1;
for (var i = 0, ql = queue.length; i < ql; i++) {
if((sortInstant && offset === 0 && !queue[i].isInstantWatch) || offset !== 0) {
if(seriesLump && maintainSeries) {
if(!queue[i].isSeries){
queue[i].newSort = offset + numbered;
numbered++;
} else if (accountedforSeries[queue[i].series] === undefined) {
queue[i].newSort = offset + numbered;
accountedforSeries[queue[i].series] = 1;
numbered += seriesObject[queue[i].series].length;
} if (queue[i].isSeries && accountedforSeries[queue[i].series] !== undefined) {
queue[i].newSort = offset + (numbered - (seriesObject[queue[i].series].length - accountedforSeries[queue[i].series]));
accountedforSeries[queue[i].series]++;
}
} else {
queue[i].newSort = offset + numbered;
numbered++;
}
}
}
//Return to original order
queue = queue.sort(function(a, b){
return a.currentOrder-b.currentOrder
});
return queue;
}
function returnToQueue (newQueue, masterQueue) {
for(var i = 0, q = newQueue.length; i < q; i++) {
for(var x = 0, y = masterQueue.length; x < y; x++) {
if(newQueue[i].title === masterQueue[x].title) {
masterQueue[x] = newQueue[i];
break;
}
}
}
return masterQueue;
}
sortQueue(sortType, sortOrder, dvdQueue);
if (sortInstant) {
sortQueue(sortType, sortOrder, instantWatchArray, (dvdQueue.length - instantWatchArray.length), true);
returnToQueue(instantWatchArray, dvdQueue);
}
if (maintainSeries) {
for(var key in seriesObject) {
var offset = 0;
for (var i = 0, x = dvdQueue.length; i < x; i++) {
for (var y = 0, z = seriesObject[key].length; y < z; y++) {
if(dvdQueue[i].title === seriesObject[key][y].title && (offset === 0 || offset > dvdQueue[i].newSort)) {
offset = dvdQueue[i].newSort;
}
}
}
sortQueue('series', sortOrder, seriesObject[key], offset - 2);
returnToQueue(seriesObject[key], dvdQueue);
}
}
//// Inject back into page
function Save() {
for (var i = 1; i < queueLength; i++) {
queue[i].querySelectorAll('.pr input.o')[0].value = dvdQueue[i - 1].newSort; // order number
}
document.forms["MainQueueForm"].submit();
}
//Create sort button
var newSpan = document.createElement("span");
var sortButton = document.createElement('input');
sortButton.id = 'Sort';
sortButton.type = 'button';
sortButton.value = 'Sort';
sortButton.name = 'Sort';
newSpan.appendChild(sortButton);
document.getElementsByTagName('h2')[1].insertBefore(newSpan, document.getElementsByClassName('hButtonWrapper')[0]);
function OnSortClick() {
Save();
}
var button = document.getElementById('Sort');
if (button.addEventListener) { // all browsers except IE before version 9
button.addEventListener('click', OnSortClick, false);
} else {
if (button.attachEvent) { // IE before version 9
button.attachEvent('onclick', OnSortClick);
}
}