I'm trying to get additional information for the selected text on a webpage, which has following HTML (simplified):
<h2 class="level2"><span>part 1</span> AAAA<a name="p1"> </a></h2>
<h3><strong>N1 </strong><a name="NameX"> </a>TitelX</h3>
<p><a name="N1S1"> </a>(1) Text1.</p>
<p><a name="N1S2"> </a>(2) Text2.</p>
<p><a name="N1S3"> </a>(3) Text3.</p>
<p><a name="N1S4"> </a>(4) Text4.</p>
...
What I need, is to get (in Tampermonkey for Chrome) the name on anchor (f.e N1S1), if the user has selected some text (f.e Text1 or part of it).
So far I have got the selected text (after button is pressed):
$(document).ready(function() {
$('body').append('<input type="button" value="Button 1" id="b1">')
$('#b1').css('position', 'fixed').css('bottom', 0).css('left', 0);
$('#b1').click(function(){
var x = document.getSelection()
alert(x)
});
});
And I need to find the anchor name for the selection.