Copy selected text with links appended in plain text?
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.
Solution 1:
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
}
}