Last active
October 4, 2016 19:27
-
-
Save spudtrooper/96a9c520c9a1d205ee2160a8ea102ab6 to your computer and use it in GitHub Desktop.
Shows updated number of reviews from amazon in a big ugly div and in the page title. Go to a book page (e.g. https://www.amazon.com/100-Tricks-Appear-Smart-Meetings/dp/1449476058/) and paste this into the JS console.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Update every 3 seconds, may want to change this. | |
var updatePeriodMillis = 1000; | |
var lastRevs = 0; | |
var d = document.createElement('div'); | |
d.innerHTML = | |
'<div style="position: absolute; left: 150px; top: 250px; background-color: rgb(119,0,0); width: 1200px; height: ' | |
+ '800px; color: #fff; font-family: arial; right: 150px; padding: 20px;" id="reviews">' | |
+ ' <div id="numReviews" style="position: relative; font-size:6em; left: 10px; top: 10px;">--</div>' | |
+ ' <div style="margin-top:20px; position: relative; font-size:1em; left: 10px; top: 10px;">Last updated</div>' | |
+ ' <div id="lastUpdated" style="margin-top:10px; position: relative;font-size:2em; left: 10px;top: 10px;">not updated</div>' | |
+ ' <div style="margin-top:10px; position: relative; font-size:1em; left: 10px; top: 10px;">Last change</div>' | |
+ ' <div id="lastChange" style="margin-top:10px; position: relative; font-size:2em; left: 10px;top: 10px;">not changed</div>' | |
+ '</div>'; | |
document.body.appendChild(d); | |
var update = function() { | |
var getReviews = function(s) {return parseInt(s.match(/showViewpoints\=1[^>]+>(\d+)<\/a>/)[1]);}; | |
var asin = String(document.location).match(/dp\/(\d+)/)[1]; | |
var args = 'method=getBookData&asin=' + asin; | |
var url = 'https://www.amazon.com/gp/search-inside/service-data'; | |
var req = new XMLHttpRequest(); | |
req.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
var revs = getReviews(this.responseText); | |
console.log(revs); | |
if (revs) { | |
document.title = String(revs); | |
document.getElementById('numReviews').innerHTML = String(revs); | |
document.getElementById('lastUpdated').innerHTML = new Date().toLocaleTimeString(); | |
if (lastRevs != 0 && lastRevs != revs) { | |
document.getElementById('lastChange').innerHTML = new Date().toLocaleTimeString(); | |
} | |
lastRevs = revs; | |
} | |
} | |
}; | |
req.open('POST', url, true); | |
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); | |
req.send(args); | |
}; | |
update(); | |
setInterval(update, updatePeriodMillis); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment