Copy Markdown-Format Address

Copy current tab title and url, and convert to markdown syntax

2018/02/12のページです。最新版はこちら。

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
// ==UserScript==
// @name         Copy Markdown-Format Address
// @namespace    undefined
// @version      0.2
// @description  Copy current tab title and url, and convert to markdown syntax
// @author       https://github.com/Dream4ever
// @require      https://code.jquery.com/jquery-latest.js
// @match        *://*/*
// @grant        none
// ==/UserScript==

// Thanks to https://stackoverflow.com/questions/400212/
// solved the problem of unable select text
// of none-displayed textarea

(function () {
  'use strict';

  window.onload = function () {
    var rdnId = Math.random().toString(36).substring(5);
    var nodeButton = document.createElement('button');
    nodeButton.setAttribute('id', rdnId);
    nodeButton.style.width = '22px';
    nodeButton.style.height = '22px';
    nodeButton.style.opacity = '.2';
    nodeButton.style.padding = '5px 10px';
    nodeButton.style.borderRadius = '5px';
    nodeButton.style.background = 'rgb(240, 240, 240)';
    nodeButton.style.boxShadow = '3px 3px 3px rgba(0, 0, 0, .1)';
    nodeButton.style.position = 'fixed';
    nodeButton.style.bottom = '20px';
    nodeButton.style.right = '100px';
    nodeButton.style.zIndex = 2;
    document.body.appendChild(nodeButton);

    var id = '#' + rdnId;
    var $btn = $(id);

    $btn.hover(function()
    {
      $btn.text('复制');
      $btn.css('width', '80px');
      $btn.css('height', '28px');
      $btn.css('opacity', '1');
      $btn.css('fontSize', '14px');
      $btn.css('fontWeight', '400');
      $btn.css('lineHeight', '1.15');
      $btn.css('color', '#000');
      },
    function()
    {
      $btn.text('');
      $btn.css('width', '22px');
      $btn.css('height', '22px');
      $btn.css('opacity', '.2');
    });

    $btn.on('click', function (event) {

      // get title and url
      var title = document.title;
      var url = document.URL;
      var address = '[' + title + '](' + url + ')';

      if (copyTextToClipboard(address)) {
          $btn.text('复制成功');
          $btn.css('color', 'green');
          setTimeout(function() {
            $btn.text('复制');
            $btn.css('color', '#000');
          }, 2000);
      } else {
          $btn.text('复制失败');
          $btn.css('color', 'red');
          setTimeout(function() {
            $btn.text('复制');
            $btn.css('color', '#000');
          }, 2000);
      }
    });
  };

})();

function copyTextToClipboard(text) {
  var textArea = document.createElement("textarea");

  //
  // *** This styling is an extra step which is likely not required. ***
  //
  // Why is it here? To ensure:
  // 1. the element is able to have focus and selection.
  // 2. if element was to flash render it has minimal visual impact.
  // 3. less flakyness with selection and copying which **might** occur if
  //    the textarea element is not visible.
  //
  // The likelihood is the element won't even render, not even a flash,
  // so some of these are just precautions. However in IE the element
  // is visible whilst the popup box asking the user for permission for
  // the web page to copy to the clipboard.
  //

  // Place in top-left corner of screen regardless of scroll position.
  textArea.style.position = 'fixed';
  textArea.style.top = 0;
  textArea.style.left = 0;

  // Ensure it has a small width and height. Setting to 1px / 1em
  // doesn't work as this gives a negative w/h on some browsers.
  textArea.style.width = '2em';
  textArea.style.height = '2em';

  // We don't need padding, reducing the size if it does flash render.
  textArea.style.padding = 0;

  // Clean up any borders.
  textArea.style.border = 'none';
  textArea.style.outline = 'none';
  textArea.style.boxShadow = 'none';

  // Avoid flash of white box if rendered for any reason.
  textArea.style.background = 'transparent';

  textArea.value = text;

  document.body.appendChild(textArea);

  textArea.select();

  var isOK = false;

  try {
    var successful = document.execCommand('copy');
    isOK = !!successful;
  } catch (err) {}

  document.body.removeChild(textArea);

  return isOK;
}