Copy Markdown-Format Address

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

目前為 2018-02-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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;
}