0
1

More than 1 year has passed since last update.

Amazonリンク短縮(クエリ削除)・タグ付与ブックマークレット

Last updated at Posted at 2021-12-24

経緯

  • 長大なAmazonリンクがLINEトークを埋めつくした

三種類

  1. Amazonの__商品ページで押した場合に__余分なクエリを削除(画面遷移)
  2. ①に加え、__他サイトで押した場合に__Amazonホームへ移動(画面遷移)
  3. ②に加え、__アソシエイトタグ添付の確認__をはさみ、ダイアログでタグ付URLを表示(OKの場合は非遷移)

コード

1. 商品ページで押した場合に余分なクエリを削除

ブクマ用

javascript:(function(m){m=location.href.match(/\/(dp|hz\/intuition|gp\/(product|aw\/[a-z]))\/(\w{8,12})(\/|\?|$)/)??false;if(m){window.open('https://amazon.co.jp/dp/'+m[3],'_self');}else{alert('Not available on this page.');}})();

全体

javascript:(
  function(m){
    // 今のURLから「/dp/」「/gp/product/」「/gp/aw/d/」「/hz/intuition/」を探し、その後に続く商品ID(10文字)を取得( m[3] )
    m=location.href.match(/\/(dp|hz\/intuition|gp\/(product|aw\/[a-z]))\/(\w{8,12})(\/|\?|$)/)??false;
    if(m){
      // 商品IDがあった場合:最短URLに結合して画面遷移
      window.open('https://amazon.co.jp/dp/'+m[3],'_self');
    }else{
      // 商品IDがなかった場合:「ここでは使えません」と通知
      alert('Not available on this page.');
    }
  }
)();

2. 他サイトで押した場合にAmazonトップへ移動

ブクマ用

javascript:(function(url,m1,m2){url=location.href;m1=url.match(/^(|w{3}\.|https:\/{2}(w{3}\.)?)amazon\.co\.jp(\/|$)/)??false;m2=url.match(/\/(dp|hz\/intuition|gp\/(product|aw\/[a-z]))\/(\w{8,12})(\/|\?|$)/)??false;if(m1===false){window.open('https://amazon.co.jp','_self');}else{if(m2){window.open('https://amazon.co.jp/dp/'+m2[3],'_self');}else{alert('Not available on this page.');}}})();

全体

javascript:(
  function(url,m1,m2){
    url=location.href;
    // m1: 現在のページがAmazonか否か
    m1=url.match(/^(|w{3}\.|https:\/{2}(w{3}\.)?)amazon\.co\.jp(\/|$)/)??false;
    if(m1===false){
      window.open('https://amazon.co.jp','_self');
    }else{
      // m2: 商品IDを探索
      m2=url.match(/\/(dp|hz\/intuition|gp\/(product|aw\/[a-z]))\/(\w{8,12})(\/|\?|$)/)??false;
      if(m2){
        window.open('https://amazon.co.jp/dp/'+m2[3],'_self');
      }else{
        alert('Not available on this page.');
      }
    }
  }
)();

3. タグ付与の確認をはさみ、OKならダイアログでタグ付URLを表示

ブクマ用  ※「hogemoge-22」(拙タグ) の部分を自分のトラッキングIDに修正して下さい

javascript:(function(yourTag,url,m1,m2){yourTag='hogemoge-22';url=location.href;m1=url.match(/^(|w{3}\.|https:\/{2}(w{3}\.)?)amazon\.co\.jp(\/|$)/)??false;if(m1===false){window.open('https://amazon.co.jp','_self');}else{m2=url.match(/\/(dp|hz\/intuition|gp\/(product|aw\/[a-z]))\/(\w{8,12})(\/|\?|$)/)??false;if(m2){if(confirm('add your tag?')){prompt('The URL is below. Please be careful not to self-purchase.','https://amazon.co.jp/dp/'+m2[3]+'?tag='+yourTag,'_self');}else{window.open('https://amazon.co.jp/dp/'+m2[3],'_self');}}else{alert('Not available on this page.');}}})();

全体

javascript:(
  function(yourTag,url,m1,m2){
    yourTag='hogemoge-22';
    url=location.href;
    m1=url.match(/^(|w{3}\.|https:\/{2}(w{3}\.)?)amazon\.co\.jp(\/|$)/)??false;
    if(m1===false){
      window.open('https://amazon.co.jp','_self');
    }else{
      m2=url.match(/\/(dp|hz\/intuition|gp\/(product|aw\/[a-z]))\/(\w{8,12})(\/|\?|$)/)??false;
      if(m2){
        if(confirm('add your tag?')){
          prompt('The URL is below. Please be careful not to self-purchase.','https://amazon.co.jp/dp/'+m2[3]+'?tag='+yourTag,'_self');
        }else{
          window.open('https://amazon.co.jp/dp/'+m2[3],'_self');
        }
      }else{
        alert('Not available on this page.');
      }
    }
  }
)();

表示されたURLが自分のIDにきちんと関連付けられているかは、Amazonアソシエイトのリンク動作確認ツールで確認してみて下さい(要ログイン)。

自分のタグを付与したAmazonURLにはアクセスしないようご注意下さい
参考:自己購入を回避する方法


ブックマークレットの利用方法

Android版Chromeはブックマークのタップでは動作せず、アドレスバーに「amatan」等の(自分がつけた)ブクマ名を入力し、候補として表示されたものをタップすると動くようです


もし誤った点や、12文字以上のID、指定したパターン以外の商品ページ等がありましたら、お手数ですがご教示頂ければ幸いです。:santa:

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1