satyr (owner)

Revisions

gist: 29674 Download_button fork
public
Description:
[Ubiquity] Amazon.co.jp
Public Clone URL: git://gist.github.com/29674.git
jamazon.ubiq.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const Name = 'jamazon',
Jamazon = 'http://www.amazon.co.jp/',
ItemMax = 35,
Logo = <a class="logo" href={Jamazon} accesskey="0"
><img border="0" align="middle" width="126" height="28"
src={('http://g-ecx.images-amazon.com/images/G/09/gno/images/general/'+
      'navAmazonLogoFooter._V28243234_.gif')}/></a>,
Base = ''+ <div class={Name}><style><![CDATA[
  ol, ul {margin:0; padding:0; list-style:none}
  button {padding:0; border-width:1px; font:bold 96% monospace}
  .item img {float:right; border:none; margin:0 0.2em 0.2em 0}
  .num, .price {font-weight:bold}
  .num, li {margin:0 0 0.2em}
  .price {white-space:nowrap}
  .price, .author, .label, .features {font-size:88%}
  .features li:before {content: "\2022 "}
  .item, logo {clear:both}
  .logo {display:inline-block}
  .error {font-style:oblique; line-height:1.8}
  .loading + .logo {opacity:0.4}
  .error + .logo {opacity:0.7}
  ]]></style><div id={Name}
> </div></div>.appendChild(Logo),
Categories = {
  'All すべて': ['All', 'aps'],
  'Apparel アパレル&ファッション雑貨': ['Apparel', 'apparel'],
  'Baby ベビー&マタニティ': ['Baby', 'baby'],
  'Beauty コスメ': ['Beauty', 'beauty'],
  'Books 和書': ['Books', 'stripbooks'],
  'Classical クラシック音楽': ['Classical', 'classical'],
  'DVD DVD': ['DVD', 'dvd'],
  'Electronics 家電&カメラ': ['Electronics', 'electronics'],
  'ForeignBooks 洋書': ['ForeignBooks', 'english-books'],
  'Grocery 食品&飲料': ['Grocery', 'grocery'],
  'HealthPersonalCare ヘルス&ビューティー': ['HealthPersonalCare', 'hpc'],
  'Kitchen ホーム&キッチン': ['Kitchen', 'kitchen'],
  'Music ミュージック': ['Music', 'popular'],
  'Shoes&Bags シューズ&バッグ': ['Shoes', 'shoes'],
  'Software PCソフト': ['Software', 'software'],
  'SportingGoods スポーツ&アウトドア': ['SportingGoods', 'sporting'],
  'Toys&Hobbies おもちゃ&ホビー': ['Toys', 'toys'],
  'VHS ビデオ': ['VHS', 'vhs'],
  'VideoGames TVゲーム': ['VideoGames', 'videogames'],
  'Watches 時計': ['Watches', 'watch'],
};
function ng(c){
  c.textContent = Array.slice(arguments, 1).join(' ');
  c.className = 'error';
}
function joinq($_, sel, delim)(
  $_.find(sel).map(function() this.textContent).get().join(delim || ' '));
function onfocus(e){
  var b = e.target;
  if(b.nodeName !== "BUTTON") return;
  b.blur();
  b.disabled = true;
  Utils.openUrlInBrowser(b.value);
}
function percentize(c) '%'+ c.charCodeAt().toString(16);
function amazEnc(str)(
  encodeURIComponent(str).replace(/[!*\'()]/g, percentize));
function amazSig(endp, reqp, params){
  var ps = $.extend({
    Timestamp: new Date().toISOString().slice(0, -5) +'Z',
    AssociateTag: 'matyr-22',
    AWSAccessKeyId: '0SP5F78NM10ZVNWWG482',
    SignatureMethod: 'HmacSHA256',
    SignatureVersion: 2,
  }, params);
  var sps = [k +'='+ amazEnc(ps[k]) for(k in ps)].sort().join('&');
  return ('http://'+ endp + reqp +'?'+ sps +'&Signature='+
          amazEnc(Utils.signHMAC('SHA256',
                                 'DlSRkBtC3WDAMjA5fVJO4Fg04omvXYVXs49oNPlo',
                                 ['GET', endp, reqp, sps].join('\n'))));
}
CmdUtils.CreateCommand({
  name: 'jamazon',
  icon: Jamazon +'favicon.ico',
  description: ''+ Logo,
  help: ('[ Categories カテゴリリスト ]<ul>'+
         [<li>{c}</li>.toXMLString() for(c in Categories)].join('') +
         '</ul>'),
  arguments: {
    object: noun_arb_text,
    format: CmdUtils.NounType('category カテゴリ', Categories, 'All'),
  },
  execute: function jam_execute({object: {text}, format: {data}}){
    Utils.openUrlInBrowser(
      (text = Utils.trim(text))
      ? (Jamazon +'s/ref=nb_ss_?tag=matyr-22&__mk_ja_JP='+
         '%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&url=search-alias%3D'+ data[1] +
         '&field-keywords='+ encodeURIComponent(text))
      : Jamazon);
  },
  preview: function jam_preview(pb, {object: {text}, format: {data}}){
    do var cn = pb.ownerDocument.getElementById(Name);
    while(!cn && (pb.innerHTML = Base));
    if(!text) return;
    cn.className += ' loading';
    CmdUtils.previewAjax(pb, {
      url: amazSig('xml-jp.amznxslt.com', '/onca/xml', {
        Service: 'AWSECommerceService',
        Version: '2008-08-19',
        Operation: 'ItemSearch',
        Condition: 'All',
        Merchant: 'All',
        ResponseGroup: 'ItemAttributes,Images',
        SearchIndex: data[0],
        Keywords: text,
      }),
      dataType: 'text',
      success: function jam_success(xml){
        var res = XML(xml.replace(/^\s*<\?[^>]*>/, '')
                      .replace(/ xmlns=".*?"/g, '')),
        em = res..Error.Message;
        if(em[0]) return ng(cn, em);
        var ol = <ol/>, i = 1;
        for each(var it in res.Items.Item){
          var img = it..SmallImage[0],
          attr = it.ItemAttributes,
          price = attr.ListPrice,
          url = it.DetailPageURL +'',
          key = i.toString(ItemMax + 1).toUpperCase(),
          author = [x for each(x in attr.Author)].join(', '),
          label = attr.Label +'',
          features = [x for each(x in attr.Feature)],
          li = (
            <li class="item"><button value={url} accesskey={key}
            >{key}</button><a href={url}><span class="title">{attr.Title +''}
            </span></a><span class="price">{price.FormattedPrice +''}
            </span></li>);
          img && li.prependChild(
            <img src={img.URL} width={img.Width} height={img.Height}/>);
          author && li.appendChild(<div class="author">{author}</div>);
          label && li.appendChild(<div class="label">{label}</div>);
          features.length && li.appendChild(
            features.reduce(function(ul, f) ul.appendChild(<li>{f}</li>),
                            <ul class="features"/>));
          ol.appendChild(li);
          if(++i > ItemMax) break;
        }
        cn.innerHTML =
          <div class="num">Total: {res.Items.TotalResults}</div> + ol;
        cn.addEventListener('focus', onfocus, true);
        cn.className = '';
      },
      error: function jam_error(x, s){
        ng(cn, x.status, x.statusText, '('+ s +')');
      },
    });
  },
  author: 'satyr', license: 'MIT',
});