Copy Markdown-Format Address

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

As of 12. 02. 2018. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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