Javascript firefox扩展来获取链接周围的文本

Lil*_*ilz 1 javascript firefox onclick hyperlink firefox-addon

我是否可以等待用户点击链接,点击链接后可以获得链接的文本?

也许通过使用onClick?

小智 6

如果您的意思是处理用户正在浏览的页面中的链接的click事件,那么这是如何:

// 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);
   }   
} 
Run Code Online (Sandbox Code Playgroud)