Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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
    }
}
share|improve this answer
    
Awesome, thanks for the script and turning me towards the chrome plugin. Works great. –  Matthew Sprankle Nov 28 '12 at 15:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.