AmazonShortUrlButton

Amazonの商品ページに短縮したURLをクリップボードにコピーするボタンを追加する。

目前為 2018-08-05 提交的版本,檢視 最新版本

// ==UserScript==
// @name         AmazonShortUrlButton
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Amazonの商品ページに短縮したURLをクリップボードにコピーするボタンを追加する。
// @author       You
// @match        https://www.amazon.co.jp/*
// @grant        none
// @require      https://code.jquery.com/jquery-3.3.1.slim.js
// ==/UserScript==

(function() {
    $("#tell-a-friend").append('<br/><input type = "button" value = "URLをコピー" name="urlCopy" style="width:100%; font-size:small;"><div id="copyMessage" style="width:100%; font-size:x-small; padding-top:5px;color:#0000ff"></div>');
})();

$("input[name='urlCopy']").click(function(){
    var url = "";
    var protocol = location.protocol;
    var host = location.host;
    var list = location.pathname.split("/");
    if(list[2] == "dp"){
         url = protocol + "//" + host + "/"+ list[2] +"/"+ list[3];
    }else if(list[1] == "gp"){
         url = protocol + "//" + host + "/"+ list[1] +"/"+ list[2] + "/" + list[3];
    }

    var copyTarget = document.createElement("textarea");
    copyTarget.textContent = url;
    var bodyElm = document.getElementsByTagName("body")[0];
    bodyElm.appendChild(copyTarget);
    copyTarget.select();
    var result = document.execCommand('copy');
    bodyElm.removeChild(copyTarget);
    $("#copyMessage").text("クリップボードにURLをコピーしました。");
    setTimeout( function() {
        $("#copyMessage").text("");
    } , 2000 );
});