それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

Amazonのマケプレの「関東への送料無料」詐欺チェッカー(暫定版

Amazonマケプレは送料無料に見せかける詐欺が横行中

関東への送料無料

→それ以外は、780円

とかで、油断して注文すると大変な目に合うことが多い

というわけで、関西への本当の送料を調べることにした。

f:id:takuya_1st:20150623040514p:plain

ソース

ブラウザのコンソールに貼り付けて実行。

何度か使ってみて、そのうちPromiseつかったり、XHR部分を書き直して拡張機能にします。

var add_xpath = function(doc){
  doc.xpath =   function(expression) {
    ret = this.evaluate(expression,this,null,XPathResult.ANY_TYPE,null);
    switch(ret.resultType){
     case 1: return ret.numberValue;
     case 2: return ret.stringValue;
     case 3: return ret.booleanValue;
     case 4: 
     case 5:
             var a=[];
             while(e=ret.iterateNext()){a.push(e);};
             if( a.length < 1 ){return null}
             if( a.length ==1 ){return a[0]}
             return a;
     default: return ret;
     }
  }
}
var shipping_price = function(doc,loc){
  add_xpath(doc)
  var a = doc.xpath("//td/strong[contains(text(),'"+loc+"')]/../../following-sibling::tr[position()<=3]/td[position() mod 3 = 0]")
  if(!a){
    console.log("取得エラー")
    console.log(doc.URL)
    return null;
  }
  var a = a.map(function(e){  return  e.textContent.replace(/[^0-9]/,"") })
  var a = a.map(function(e){  return  Number(e) })
  price =  Math.max.apply(null, a);  
  return price;
}



var add_shipping_cost = function(seller_id,asin){
  seller_shipping_page_url = "http://www.amazon.co.jp/gp/aag/details/?seller="+seller_id+"&asin="+asin+"&sshmPath=shipping-rates#aag_shipping"
  xhr_handler = function(v,asin){
   var xhr = v;
   return function(){
     if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          var loc = "関西"
          var shipping = shipping_price(xhr.responseXML,loc);
          if(shipping){
            var span = document.xpath("//li[@data-asin='"+asin+"']//span[contains(@class, 's-price')]")
            span.innerHTML=span.innerHTML+"+"+loc+"への送料"+shipping;          
          }
        } else {
          alert('There was a problem with the request.');
        }
      }  
    }
  }

  xhr = new XMLHttpRequest()
  xhr.open("GET", seller_shipping_page_url,true);
  xhr.onreadystatechange = xhr_handler(xhr,asin);
  xhr.responseType = "document";
  xhr.send()


}

var item_page = function(asin){
  var url =  "http://www.amazon.co.jp/gp/product/"+asin;
  xhr_handler = function(v,asin){
   var xhr = v;
   return function(){
   if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          var doc =  xhr.responseXML
          add_xpath(doc)
          console.log(doc)
          var ret = doc.xpath("//*[@id='merchant-info']")
          var anchor = doc.querySelector("#merchant-info a")
          if( anchor ){
            URL.prototype.get_params=function(){
              var keys = this.search.replace("?", "").split(/&/);
              keys = keys.map( function(e){ return  e.split(/=/)} )
              var params = keys.reduce( function(p, e){  name=e[0];val=e[1]; p[name]=val; return p  }, {} )
              return params;  
            }
            url = anchor.href
            u = new URL(url)
            params = u.get_params()
            seller_id = params["seller"]
            console.log(seller_id)

            add_shipping_cost(seller_id,asin)
          }
          console.log(ret)

        } else {
          alert('There was a problem with the request.');
        }
      }  
  }
  }
  xhr = new XMLHttpRequest()
  xhr.open("GET", url, true);
  xhr.onreadystatechange = xhr_handler(xhr,asin);
  xhr.responseType = "document";
  xhr.send()
}







add_xpath(document)
a = document.xpath("//li[@data-asin]/@data-asin")
a = a.map(function(e){ return e.textContent;})
a.forEach(function(e){ return item_page(e);})

/**
 GNU GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.
**/