View Single Post
Old 09-19-2015, 01:21 PM   #31
gbm
Wizard
gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.gbm ought to be getting tired of karma fortunes by now.
 
Posts: 2,203
Karma: 8888888
Join Date: Jun 2010
Device: Kobo Clara HD,Hisence Sero 7 Pro RIP, Nook STR, jetbook lite
Spoiler:
Quote:
Originally Posted by gbm View Post
Try this change to the script.
Edit the following line in the script
Code:
      var pos0 = data.indexOf('class="priceLarge"');
change it to:
Code:
      var pos0 = data.indexOf('class="a-size-small a-color-price"');
This is the compete script with the change.
Code:
// @namespace   kakapo.public
// @description Checks price of Kindle ebook links in MobileRead forum posts. Translates amazon.com links into your local site, please install the correct version.
// @include     https://www.mobileread.com/forums/showthread.php*
// @version     3
// @grant       GM_xmlhttpRequest
// ==/UserScript==
var site_regex = /^(http|https):\/\/(www\.)?amazon\.(com)(\/.*)/;
var migrate_regex = false;
var migrate_replacement = '';
var currency_prefix = '$';
var ebook_url_regexs = [
  /^\/dp\//,
  /^\/[^\/]+\/dp\//,
  /^\/gp\/product\//,
  /^\/exec\/obidos\/ASIN\//
];
var check_price = function (elements, url) {
  var xhr = GM_xmlhttpRequest({
    method: 'GET',
    url: url,
    onload: function (response) {
      var data = response.responseText;
      var pos0 = data.indexOf('class="a-size-small a-color-price"');
      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 == currency_prefix + '0.00' || price == currency_prefix + '0,00' || price == currency_prefix + '0' ? 'red' : 'blue';
      pos0 = data.indexOf('class="iou_cust"');
      if (pos0 >= 0) {
        price += ' already own';
      }
      for (var i = 0; i < elements.length; i++) {
        elements[i].insertAdjacentHTML('afterend', ' <span style="color:' + color + '">(' + price + ')</span>');
      }
    }
  });
}
var a = document.getElementsByTagName('a');
var to_check = {
};
for (var i = 0; i < a.length; i++) {
  var url = a[i].href;
  var parts = site_regex.exec(url);
  if (parts) {
    if (migrate_regex) {
      url = url.replace(migrate_regex, migrate_replacement);
      a[i].href = url;
    }
    for (var j = 0; j < ebook_url_regexs.length; j++) {
      if (ebook_url_regexs[j].test(parts[parts.length - 1])) {
        if (!to_check[url]) {
          to_check[url] = [
          ];
        }
        to_check[url].push(a[i]);
      }
    }
  }
}
for (var k in to_check) {
  check_price(to_check[k], k);
}


bernie


After further testing and coffee to get this script to work edit the following lines:
Code:
      var pos0 = data.indexOf('class="priceLarge"');
change to:
Code:
      var pos0 = data.indexOf('class="a-color-price a-size-medium a-align-top"');
edit following line
Code:
      pos0 = data.indexOf('class="iou_cust"');
change to:
Code:
 pos0 = data.indexOf('id="ebooksInstantOrderUpdate" class="a-size-medium"');
Here is the entire script with changes:
Spoiler:
Code:
// ==UserScript==
// @name        MobileRead Kindle Price Check (amazon.com)
// @namespace   kakapo.public
// @description Checks price of Kindle ebook links in MobileRead forum posts. Translates amazon.com links into your local site, please install the correct version.
// @include     https://www.mobileread.com/forums/showthread.php*
// @version     3
// @grant       GM_xmlhttpRequest
// ==/UserScript==
var site_regex = /^(http|https):\/\/(www\.)?amazon\.(com)(\/.*)/;
var migrate_regex = false;
var migrate_replacement = '';
var currency_prefix = '$';
var ebook_url_regexs = [
  /^\/dp\//,
  /^\/[^\/]+\/dp\//,
  /^\/gp\/product\//,
  /^\/exec\/obidos\/ASIN\//
];
var check_price = function (elements, url) {
  var xhr = GM_xmlhttpRequest({
    method: 'GET',
    url: url,
    onload: function (response) {
      var data = response.responseText;
      var pos0 = data.indexOf('class="a-color-price a-size-medium a-align-top"');
      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 == currency_prefix + '0.00' || price == currency_prefix + '0,00' || price == currency_prefix + '0' ? 'red' : 'blue';
      pos0 = data.indexOf('id="ebooksInstantOrderUpdate" class="a-size-medium"');
      if (pos0 >= 0) {
        price += ' already own';
      }
      for (var i = 0; i < elements.length; i++) {
        elements[i].insertAdjacentHTML('afterend', ' <span style="color:' + color + '">(' + price + ')</span>');
      }
    }
  });
}
var a = document.getElementsByTagName('a');
var to_check = {
};
for (var i = 0; i < a.length; i++) {
  var url = a[i].href;
  var parts = site_regex.exec(url);
  if (parts) {
    if (migrate_regex) {
      url = url.replace(migrate_regex, migrate_replacement);
      a[i].href = url;
    }
    for (var j = 0; j < ebook_url_regexs.length; j++) {
      if (ebook_url_regexs[j].test(parts[parts.length - 1])) {
        if (!to_check[url]) {
          to_check[url] = [
          ];
        }
        to_check[url].push(a[i]);
      }
    }
  }
}
for (var k in to_check) {
  check_price(to_check[k], k);
}


This will check the kindle price and if already owned.

bernie

Last edited by gbm; 09-19-2015 at 05:01 PM.
gbm is offline   Reply With Quote