Amazon Camel Graph

Add CamelCamelCamel graph + link to Amazon product pages.

  1. // ==UserScript==
  2. // @name Amazon Camel Graph
  3. // @version 1.0.4
  4. // @description Add CamelCamelCamel graph + link to Amazon product pages.
  5. // @namespace null
  6. // @include http://www.amazon.*/*
  7. // @include http://*.camelcamelcamel.com/*
  8. // @include http://camelcamelcamel.com/*
  9. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js
  10. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
  11. // ==/UserScript==
  12.  
  13.  
  14. var width = 400;
  15. var height = 250;
  16. var chart = "amazon-new"; //Possible other values are "amazon", "new", "used", "new-used", & "amazon-new-used"
  17.  
  18.  
  19. var arr = document.domain.split(".");
  20. var country = arr[arr.length - 1];
  21. if (country=="com") {country = "us";}
  22.  
  23. var element = $(':input[id="ASIN"]');
  24. var asin = $.trim(element.attr("value"));
  25. if (asin=="") {
  26. element = $(':input[id="ASIN"]');
  27. asin = $.trim(element.attr("value"));
  28. }
  29. var link2 = "<a target='blank' href='http://" + country + ".camelcamelcamel.com/product/" + asin + "'><img src='http://charts.camelcamelcamel.com/" + country + "/" + asin + "/" + chart + ".png?force=1&zero=0&w=" + width + "&h=" + height + "&desired=false&legend=1&ilt=1&tp=all&fo=0' /></a>";
  30. var camelurl = 'http://' + country + '.camelcamelcamel.com/product/' + asin;
  31. GM_xmlhttpRequest({
  32. method: 'GET',
  33. url: camelurl,
  34. onload: function(response) {
  35.  
  36. var parser = new DOMParser ();
  37. var responseDoc = parser.parseFromString (response.responseText, "text/html");
  38. var chartpost = 0;
  39. if (chart=="amazon"||chart=="amazon-new"||chart=="amazon-new-used"||chart=="amazon-used"){
  40. if (!responseDoc.getElementById('price_type_1').disabled) {
  41. $("#averageCustomerReviews_feature_div").append("<div id='camelcamelcamel' style='margin-top: 10px; margin-left: -10px'>" + link2 + "</div>");
  42. chartpost = 1;
  43. }
  44. }
  45.  
  46. if (chart=="new"||chart=="amazon-new"||chart=="amazon-new-used"||chart=="new-used"){
  47. if (!responseDoc.getElementById('price_type_2').disabled && chartpost==0) {
  48. $("#averageCustomerReviews_feature_div").append("<div id='camelcamelcamel' style='margin-top: 10px; margin-left: -10px'>" + link2 + "</div>");
  49. chartpost = 1;
  50. }
  51. }
  52.  
  53.  
  54. if (chart=="used"||chart=="amazon-used"||chart=="amazon-new-used"||chart=="new-used"){
  55. if (!responseDoc.getElementById('price_type_2').disabled && chartpost==0) {
  56. $("#averageCustomerReviews_feature_div").append("<div id='camelcamelcamel' style='margin-top: 10px; margin-left: -10px'>" + link2 + "</div>");
  57.  
  58. }
  59. }
  60.  
  61. }
  62. });
  63.  
  64. function thisCodeProduct () {
  65. var url = unescape(location.href);
  66. var pattern = "http://([a-z]{0,2}).?camelcamelcamel.com/(.*)/product/B([A-Z0-9]{9}).*";
  67. var exp = new RegExp(pattern, "gi");
  68. return url.replace(exp,"$3");
  69. }
  70.  
  71. var url = unescape(location.href);
  72. var pattern = "http://([a-z]{0,2}).?camelcamelcamel.com/.*";
  73. var exp = new RegExp(pattern, "gi");
  74. var code = url.replace(exp,"$1");
  75.  
  76. if (code == '')
  77. code = 'com';
  78. else if (code == 'uk')
  79. code = 'co.uk';
  80.  
  81. $(document).ready(function () {
  82. var links = document.evaluate("//a[contains(@href, 'camelcamelcamel.com')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  83. for (var i = 0; i < links.snapshotLength; i++)
  84. {
  85. var link = links.snapshotItem(i);
  86. if (link.title == 'View the product page at Amazon') {
  87. link.removeAttribute('onclick');
  88. link.href = 'http://www.amazon.'+code+'/dp/B'+thisCodeProduct()+'/';
  89. }
  90. }
  91.  
  92. });
  93.  
  94. (function(doc) {
  95. // ASIN.0 in kindle store
  96. var asin = doc.getElementById("ASIN") || doc.getElementsByName("ASIN.0")[0];
  97. if (asin) {
  98. asin = asin.value
  99. history.replaceState(null, "", "/dp/" + asin + "/");
  100. }
  101.  
  102. })(document);