Lilz
Lilz

Reputation: 4081

Javascript firefox extension to get the text around the link

Is it possible for me waiting for a user to click a link, and upon clicking the link the link's text would be obtained?

Maybe by using onClick?

Upvotes: 1

Views: 1317

Answers (2)

user434917
user434917

Reputation:

If you mean handling the click event for the links in the page that the user is browsing then this is how:

// handle the load event of the window  
window.addEventListener("load",Listen,false);  
function Listen()  
{   
gBrowser.addEventListener("DOMContentLoaded",DocumentLoaded,true);  
}  

// handle the document load event, this is fired whenever a document is loaded in the browser. Then attach an event listener for the click event  
function DocumentLoaded(event) {  
 var doc = event.originalTarget;
 doc.addEventListener("click",GetLinkText,true);  
}  
// handle the click event of the document and check if the clicked element is an anchor element.  
function GetLinkText(event) {  
   if (event.target instanceof HTMLAnchorElement) {  
    alert(event.target.innerHTML);
   }   
} 

Upvotes: 6

Steve Goodman
Steve Goodman

Reputation: 1196

This is very simple using jQuery:

<script>
    $(document).ready(function(){
        $("a").click(function(){alert($(this).text());});
    });
</script>

Of course, you'll probably want to do something other than alert the text.

Upvotes: 1

Related Questions