11

How can I copy only a section of text from a hyper link?

As in, if there is a link like below, how can I copy only a part of the text that is linked?

Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation.

I want to copy only the "nothing particular" words from the above text.

How could I do that?

On Chrome I had an extension that let me select links like normal text by holding shift.

I hope there is a similar way for me to select the text of a link in Safari.

3 Answers 3

2

In Safari this is not directly possible and I do not know a special extension for that.

A workaround:

start selecting the text behind "circulation",
stop at "nothing", (that selects as plain text and starts your selection at the relevant bit)
now cmd+c

paste where you like

jump to beginning of paragraph jump two words forward
delete the rest (quick way for that, just not on SE: ctrl+k or cmdshift+arrows)

0
1

I started using Tampermonkey so ported my tiny extension to a user script:

// ==UserScript==
// @name         Shift to select link text
// @version      1.0
// @description  Hold down the shift key to select text from within hyperlinks
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  function bindNewAnchor(element) {
    element.addEventListener("mouseenter", function(event) {
      if (element.innerText) {
        if (event.shiftKey) setContenteditable(this);
        currentElement = this;
      }
    });
    element.addEventListener('keydown', function (event) {
      if (event.keyCode == 67 && event.metaKey) {
        document.execCommand('copy');
      }
    });
    element.addEventListener("mouseleave", function(event) {
      this.removeAttribute("contenteditable");
      currentElement = null;
    });
    element.addEventListener("keypress", blockEvent);
    element.addEventListener("paste", blockEvent);
    element.addEventListener("cut", blockEvent);
    allNodes.push(element);
  }

  function blockEvent(event) {
    event.preventDefault();
  }

  function setContenteditable(element) {
    element.setAttribute("contenteditable", "true");
    if (window.getComputedStyle(element, null).outlineWidth == "0px") {
      element.style.outlineWidth = "0px";
    }
  }

  var currentElement = null, allNodes = [];

  addEventListener("DOMNodeInserted", function(event) {
    if (allNodes.indexOf(event.target) == -1 && event.target instanceof HTMLAnchorElement) {
      bindNewAnchor(event.target);
    }
    if (event.target.querySelectorAll) {
      var element = event.target.querySelectorAll("a");
      for (var i = 0; i < element.length; i++) {
        if (0 > allNodes.indexOf(element[i])) bindNewAnchor(element[i]);
      }
    }
  });

  for (var elements = document.querySelectorAll("a"), i = 0; i < elements.length; i++) bindNewAnchor(elements[i]);

  window.addEventListener("keydown", function(event) {
    if (currentElement && event.keyCode == 16) {
      setContenteditable(currentElement);
    }
  });
})();

Hold shift and drag over links to select them.

2
  • 1
    If you used the Option ⌥ instead this would bring it up to par with Chrome and Firefox (and Alt on Windows, Super+Alt on Linux). Commented Sep 12, 2018 at 14:15
  • Just change event.keyCode == 16 to event.keyCode == 18 to use the Option key. (also, probably obvious, but you can use the Safari free Userscripts ext with his same code too)
    – Mint
    Commented Jul 2, 2023 at 0:54
-1

Easiest way is to just copy the whole thing, paste it into a TextEdit document, convert to plain text, and then copy out exactly what you want.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .