This would probably be done best by an extension, but to see the possibilities, you can put the following code in a bookmarklet. Then on a page containing some links, select a few and click the bookmarklet. It should pop up an
alert containing the links, which you can copy-paste into a text file, changing the extension to .htm or .html. Then open the file in your browser.
An extension could put the results directly on the clipboard or save them to a file. The
alert technique would become too cumbersome if the number of links grew much.
Code: Select all
javascript:{
let linkHarvester = {
serializer: new XMLSerializer(),
handleEvent: function(event){
var oSel = window.getSelection(), hldr = document.createElement('div'), hrvst = hldr.cloneNode(false),
styl = hrvst.appendChild(document.createElement('style')); styl.setAttribute('scoped', 'scoped'); styl.appendChild(document.createTextNode('a[href]{display:block;}'));
for(let i = 0, k = oSel.rangeCount; i < k; i++){hldr.appendChild(this.extendRange(this.extendRange(oSel.getRangeAt(i), true)).cloneContents())};
var lnks=hldr.querySelectorAll('a[href]');
for(let i=0, k=lnks.length;i<k;i++){hrvst.appendChild(lnks[i].cloneNode(true))};
alert(this.serializer.serializeToString(hrvst));
},
extendRange: function(rng,bBackward){
var rc=rng[bBackward? 'startContainer' : 'endContainer']; if(rc.nodeType !=1){rc = rc.parentNode};
var x=this.seekLink(rc);
if(x && (x !== rc)){rng[bBackward? 'setStartBefore' : 'setEndAfter'](x)};
return rng;
},
seekLink:function(nd){
do{
if(nd.matches('a[href]')){return nd};
if(nd.matches('a')){return null};
}while((nd = nd.parentNode) && (nd.nodeType == 1));
}
};
linkHarvester.handleEvent(null);
}