Copy Markdown-Format Address

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

当前为 2018-02-12 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Copy Markdown-Format Address
// @namespace    undefined
// @version      0.1
// @description  Copy current tab title and url, and convert to markdown syntax
// @author       https://github.com/Dream4ever
// @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.innerHTML = 'Copy';
    nodeButton.style.fontSize = '14px';
    nodeButton.style.color = '#000';
    nodeButton.style.width = '80px';
    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 = '70px';
    nodeButton.style.zIndex = 2;
    document.body.appendChild(nodeButton);

    nodeButton.addEventListener('click', function (event) {

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

      if (copyTextToClipboard(address)) {
          nodeButton.innerHTML = 'Done';
          setTimeout(function() { nodeButton.innerHTML = 'Copy'; }, 2000);
      } else {
          nodeButton.innerHTML = 'Failed';
          setTimeout(function() { nodeButton.innerHTML = 'Copy'; }, 2000);
      }
    });
  };

})();

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

  textArea.style.position = 'fixed';
  textArea.style.top = 0;
  textArea.style.left = 0;

  textArea.style.width = '2em';
  textArea.style.height = '2em';

  textArea.style.padding = 0;

  textArea.style.border = 'none';
  textArea.style.outline = 'none';
  textArea.style.boxShadow = 'none';

  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;
}