何も選択していない状態でCtrl+Cでページのタイトル+改行+URL+改行をクリップボードにコピー

選択中なら通常の動作

目前為 2018-06-07 提交的版本,檢視 最新版本

// ==UserScript==
// @name        何も選択していない状態でCtrl+Cでページのタイトル+改行+URL+改行をクリップボードにコピー
// @description 選択中なら通常の動作
// @match       *://*/*
// @version     0.1
// @grant       none
// @namespace https://gf.qytechs.cn/users/181558
// ==/UserScript==

(function() {
  document.addEventListener("keydown", function(e) { //キー入力
      if (e.ctrlKey && e.which == 67) {
        if (/input|textarea/i.test(e.target.tagName)) return;
        if (window.getSelection() != "") {
          //選択文字列がある
          popup("<B>" + window.getSelection().toString().length + "文字:</B><BR><pre><code>" + window.getSelection().toString() + "</code></pre>");
          return;
        }

        var doc = location.href;
        var txt1 = doc;
        var txt2 = txt1;

        if ((txt1.indexOf("www.amazon.co.jp/") != -1) && (txt1.indexOf("/dp/") != -1))
          txt2 = "http://www.amazon.co.jp/dp/" + txt1.substr(txt1.indexOf("/dp/") + 4, 10);

        if ((txt1.indexOf("www.amazon.co.jp/") != -1) && (txt1.indexOf("/ASIN/") != -1))
          txt2 = "http://www.amazon.co.jp/dp/" + txt1.substr(txt1.indexOf("/ASIN/") + 6, 10);

        if ((txt1.indexOf("www.youtube.com/embed/") != -1) && (txt1.indexOf("/ASIN/") != -1))
          txt2 = "http://www.amazon.co.jp/dp/" + txt1.substr(txt1.indexOf("/ASIN/") + 6, 10);

        if (/seiga\.nicovideo\.jp\/comic/.test(txt1)) txt2 = txt1.replace(/\?track=.*/, ""); // 静画(マンガ)短縮
        if (/seiga\.nicovideo\.jp\/watch\/mg/.test(txt1)) txt2 = txt1.replace(/\?track=.*/, "");

        if (/www.youtube.com\/embed\//.test(txt1)) txt2 = txt1.replace(/\?.*/, "").replace(/embed/, "watch");

        var ret = (navigator.platform.indexOf("Win") != -1) ? "\r\n" : "\n";
        var title = document.title.replace(/ https?:.*/, "");
        var txt = document.title + ret + txt2 + ret;

        // 列挙するタイプのサイトなら
        var rekkyo = ['//div[@class="title"]/a', '//div[@class="entry-title"]/a'];
        if (/seiga.nicovideo.jp\/my\/manga\/favorite|webcomics.jp\/mylist|webcomics.jp\/bookmark/.test(txt1)) {
          txt = ""; //title+ret;
          txt2 = "";
          var num = 0;
          for (let target of rekkyo) {
            var ele = elegeta(target);
            for (let a of ele) {
              num++;
              txt += a.innerText + ret + a.href.replace(/\?track=.*/, "") + ret;
              txt2 += a.innerText + "<BR>" + a.href.replace(/\?track=.*/, "") + "<BR>";
            }
          }
          title += " " + num + "件";
          txt = title + ret + txt;
        }

        // クリップボードにコピー
        var a = document.createElement("textarea");
        a.value = txt;
        document.body.appendChild(a);
        a.select();
        document.execCommand("copy");
        a.parentElement.removeChild(a);

        // バルーン表示
        popup("<B>" + title + "</B><BR>" + txt2);
        //        popup("<TT><B>" + title + "</B><BR>" + txt2 +"</TT>");

        e.preventDefault();
        return;
      }
    },
    false);
  return;

  function elegeta(xpath) {
    var ele = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    var array = [];
    for (var i = 0; i < ele.snapshotLength; i++) {
      array[i] = ele.snapshotItem(i);
    }
    return array;
  }

  // バルーン表示
  function popup(txt) {
    var panel = document.createElement("div");
    panel.setAttribute("style", "max-width:95%; right:0; top:0; z-index:2147483647; opacity:0.8; text-align:left; line-height:1.1; position:fixed; font-size:15px; margin:15px;  text-decoration:none; padding:15px 15px; border-radius:7px; background-color:#000000; color:#ffffff;  box-shadow:5px 5px 8px #0004; border:2px solid #fff; font-family: 'MS UI Gothic','Meiryo UI','Yu Gothic UI','Arial',sans-serif;");
    //    panel.setAttribute("style", "max-width:95%; right:0; top:0; z-index:10000; opacity:1; text-align:left; line-height:1.1; position:fixed; font-size:15px; margin:15px;  text-decoration:none; padding:15px 15px; border-radius:7px; background-color:rgba(0,0,0,0.8); color:rgba(255,255,255,1);  box-shadow:5px 5px 8px #0004; border:2px solid rgba(255,255,255,0.9); font-family: 'MS UI Gothic','Meiryo UI','Yu Gothic UI','Arial',sans-serif;");
    //panel.setAttribute("style", "right:0; top:0; z-index:10000; opacity:1; text-align:left; line-height:1.1; position:fixed; font-size:15px; margin:15px;  text-decoration:none; padding:15px 15px; border-radius:7px; background-color:#ffffe0; color:#000000;  box-shadow:5px 5px 5px #0004; border:1.5px solid #000; font-family: 'MS UI Gothic','Meiryo UI','Yu Gothic UI','Arial',sans-serif;");
    panel.innerHTML = txt;

    document.body.appendChild(panel);
    setTimeout(function() { panel.parentElement.removeChild(panel); }, 4000);
    return;
  }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址