Skip to content

Instantly share code, notes, and snippets.

@techsin
Created October 31, 2018 18:22
Show Gist options
  • Save techsin/d092a5b99ec50e6f2ecd84dbef8c4884 to your computer and use it in GitHub Desktop.
Save techsin/d092a5b99ec50e6f2ecd84dbef8c4884 to your computer and use it in GitHub Desktop.
Sort wish list on amazon page by reviews, simply copy paste in chrome dev console
function sortBooks() {
let books = [...document.querySelectorAll('ul#g-items li h3> a.a-link-normal')].map(el => findAncestor(el, 'g-item-sortable'));
let sorted = bubbleSort(books);
let frag = document.createDocumentFragment();
sorted.forEach(x => frag.appendChild(x));
let ul = document.querySelector("ul#g-items");
ul.innerHTML = '';
ul.append(frag);
alert('done');
return frag
}
function bubbleSort(array) {
for(let i = 0; i < array.length; i++) {
for(let j = 1; j < array.length; j++) {
let a = eltoRating(array[j-1]),
b = eltoRating(array[j]);
if(a < b) {
swap(array, j - 1, j);
}
}
}
return array;
}
function swap(array, i, j) {
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
function eltoRating(el) {
if (!el) return;
return Number(+el.querySelector("a[id^='review']").innerText.replace(/[^\d]/ig, ''))
}
function findAncestor (el, cls) {
while ((el = el.parentElement) && !el.classList.contains(cls));
return el;
}
function scrollToBottom() {
let found = false;
let timer = setInterval(function(){
let ele = document.querySelector('#endOfListMarker');
if (ele !== null ) found = true;
if (found) {
console.log('done scrolling');
clearInterval(timer);
sortBooks();
} else {
window.document.documentElement.scrollTop = window.document.documentElement.scrollHeight
}
},800);
}
scrollToBottom();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment