View Single Post
Old 07-16-2012, 06:09 PM   #1
kakapo
The Night Parrot
kakapo can fool all of the people all of the time.kakapo can fool all of the people all of the time.kakapo can fool all of the people all of the time.kakapo can fool all of the people all of the time.kakapo can fool all of the people all of the time.kakapo can fool all of the people all of the time.kakapo can fool all of the people all of the time.kakapo can fool all of the people all of the time.kakapo can fool all of the people all of the time.kakapo can fool all of the people all of the time.kakapo can fool all of the people all of the time.
 
kakapo's Avatar
 
Posts: 23
Karma: 138516
Join Date: Feb 2011
Location: Codfish Island, NZ
Device: Palm Vx, Kindle Keyboard (wifi)
GreaseMonkey script to check Kindle prices in this forum

I have written a script for GreaseMonkey (a FireFox addon) to check the prices of Kindle ebooks linked in MobileRead forum posts. Once installed, any links to a Kindle ebook will have the price displayed after then. Prices are coloured red for free and blue otherwise.

Currently the script only works in FireFox with GreaseMonkey installed. Even though Google Chrome can use some GreaseMonkey scripts, it can't use this one. I should be able to make it work as a proper Chrome extension, just ask and i will.

The script is set up to check amazon.com links. It can check amazon.co.uk links, just change the appropriate line at the top. Adding of additional Amazon countries should be easy enough. It should even be possible to add other ebook stores, eg Kobo or B&N.

To install the script, go to http://userscripts.org/scripts/show/138544 or copy out the code below.

Code:
// ==UserScript==
// @name        MobileRead Kindle Price Check
// @namespace   kakapo.public
// @description Checks price of Kindle ebook links in MobileRead forum posts
// @include     https://www.mobileread.com/forums/showthread.php*
// @version     1
// ==/UserScript==

// change com in line below to co.uk for Amazon UK
var site_regex = /^(http|https):\/\/(www\.)?amazon\.(com)(\/.*)/;

var ebook_url_regexs = [
  /^\/dp\//,
  /^\/[^\/]+\/dp\//,
  /^\/gp\/product\//
];

var check_price = function(element, url) {
  var xhr = GM_xmlhttpRequest({
    method: 'GET',
    url: url,
    onload: function(response) {
      var data = response.responseText;
      var pos0 = data.indexOf('class="priceLarge"');
      var pos1 = data.indexOf('>', pos0);
      var pos2 = data.indexOf('<', pos1);
      var price = data.substring(pos1 + 1, pos2).trim();
      if (price == '') {
        price = 'N/A';
      }
      var color = price == 'Free' || price == '$0.00' || price == '£0.00' ? 'red' : 'blue';
      element.insertAdjacentHTML('afterend', ' <span style="color:'+color+'">('+price+')</span>');
    }
  });
}

var a = document.getElementsByTagName('a');
for (var i = 0; i < a.length; i++) {
  var url = a[i].href;
  var parts = site_regex.exec(url);
  if (parts) {
    for (var j = 0; j < ebook_url_regexs.length; j++)  {
      if (ebook_url_regexs[j].test(parts[parts.length-1])) {
        check_price(a[i], url);
      }
    }
  }
}
kakapo is offline   Reply With Quote