Copy link under cursor in Safari or Chrome as Markdown

Hover the cursor over a link and trigger, to copy a Markdown version of the link.

Copy webpage link under cursor as Markdown.kmmacros (24.9 KB)

Main script:

(function () {
  // MOUSE HOVERING OVER A NODE ?
  var nh = document.querySelectorAll(':hover'),
    iLast = (nh ? nh.length : null),
    nodeHover = (iLast ? nh[iLast - 1] : null),

    // LINK IN ANY NODE UNDER MOUSE ?
    oLinkNode = nodeHover ? document.evaluate(
      document.kmvar.xPath,
      nodeHover,
      null, XPathResult.FIRST_ORDERED_NODE_TYPE, 0
    ).singleNodeValue : null;

  // MARKDOWN LINK ?
  return oLinkNode ? (
    '[' + oLinkNode.text + '](' +
	(oLinkNode.getAttribute('data-href') ||
    oLinkNode.href) + ')'
  ) : ''
})();
3 Likes

Hey Rob,

This is broken in Safari 11.1 (12605.1.33.1.3) on macOS 10.12.6, although it still works well in Chrome.

It would be much appreciated if you could find the time to investigate and fix it (if possible).

-Chris

Here is a much simpler macro that works with the Front Browser.

Chris, this should work with all browsers, including Safari, that KM supports as a Frontmost Browser, as of KM9.

MACRO: Create Markdown Link from Hyperlink Under Mouse [Example]