Copy selected text with links appended in plain text?

5

1

Is there a way for a link to be copied with the selected text?

"Some selected text with this link somewhere in the paragraph"

To be copied as:

"Some selected text with this { link | http://example.com } somewhere in the paragraph"

If there is anything like that it would be great help.

Matthew Sprankle

Posted 2012-11-06T14:54:26.343

Reputation: 153

Answers

2

One solution might be to transform every link on the page into the desired format. This could be done using Greasemonkey in Firefox and a custom UserScript. Alternatively, Tampermonkey for Chrome might work as well but I do not use it so I cannot verify that. The downside to this solution would be that you would want to enable it only when needed because leaving it enabled for regular web browsing would distort the layout of page elements such as headers, footers, and sidebars.

Download Greasemonkey for Firefox OR Download Tampermonkey for Chrome

The following UserScript can be saved into a *.user.js file (for example: tmpscript.user.js) and then the file can be dragged into Firefox after Greasemonkey has been installed to install the UserScript.

// ==UserScript==
// @name        Transform All Links
// @namespace   myscripts
// @description Transforms <a=href>link</a> to { link | <a=href>href</a> }
// @include     http*
// @version     1
// @grant       none
// ==/UserScript==
try{
    transformAnchors();
}catch(err){
    alert("error#" + err.number + " name: " + err.name + "\n"
        + "message: " + err.message + "\n"
        + "description: " + err.description);
    var errmsg = "";
    for (atr in err) {
        errmsg = errmsg + atr + " = " + err[atr] + "<br>\n";
    }
    alert(errmsg);
}

function transformAnchors() {
    //get every anchor node
    var anchornodes = document.getElementsByTagName("A");
    //transform every anchor
    for (var i=0; i<anchornodes.length; ++i){
        //grab the current anchor node
        anchornode = anchornodes[i];
        //ignore erroneous nodes with no parent
        if (anchornode.parentNode == undefined || anchornode.parentNode == null) continue;
        //add text sibling before anchor node : { <a=href>link</a>
        anchornode.parentNode.insertBefore(document.createTextNode("{ "),anchornode);
        //create styled anchor node to preserve styling
        styledanchornode = document.createElement("A");
        styledanchornode.setAttribute("class",anchornode.getAttribute("class"));
        styledanchornode.setAttribute("style",anchornode.getAttribute("style"));
        styledanchornode.setAttribute("title",anchornode.getAttribute("title"));
        //put styled anchor node before anchor node
        anchornode.parentNode.insertBefore(styledanchornode,anchornode);
        //move all anchor node children to before anchor node inside styled anchor node : { link <a=href></a>
        while (anchornode.hasChildNodes()){
            styledanchornode.appendChild(anchornode.childNodes[0]);
        }
        //remove styling from anchor node
        anchornode.removeAttribute("style");
        anchornode.removeAttribute("class");
        anchornode.removeAttribute("title");
        //add text sibling before anchor node : { link | <a=href></a>
        anchornode.parentNode.insertBefore(document.createTextNode(" | "),anchornode);
        //add href text child inside anchor node : { link | <a=href>href</a>
        anchornode.appendChild(document.createTextNode(anchornode.href));
        //add text sibling after anchor node : { link | <a=href>href</a> }
        anchornode.parentNode.insertBefore(document.createTextNode(" }"),anchornode.nextSibling);
        //increment index to compensate for extra styled anchor node
        ++i
    }
}

BobMesh

Posted 2012-11-06T14:54:26.343

Reputation: 36

Awesome, thanks for the script and turning me towards the chrome plugin. Works great. – Matthew Sprankle – 2012-11-28T15:49:54.767

0

I was facing a similar problem. I was able to extract the full address of the hyperlinks of a large .html-file by adding an additional step, namely copying the content of the website into a .docx Document first (I guess other formats would work as well).

This way the structure of the document remained the same to some degree (sufficient for me).

Based on that, the .docx can be easily converted into plaintext by using pandoc:

pandoc -i an.docx -o an.md

Georg

Posted 2012-11-06T14:54:26.343

Reputation: 11