How to Extract/Retrieve source code given URL in Firefox extention
I'm writing a Firefox extension that will analyze / parse the linked pages while the user is still on the current page. I k开发者_JS百科now there are ways to retrieve the source code for the page currently being viewed, but what about the linked pages? If I'm able to obtain the URL of the linking page, will I be able to retrieve the source code of that URL?
My extension is mainly written in XUL and JavaScript, any code or sample add-ons will help me a LOT!
Maybe the following JavaScript snippet will help you:
var req = new XMLHttpRequest();
req.open("GET", linkedPageUrl, false);
req.send(null);
DoSomethingWithGivenSource(req.responseText);
This works synchronously. So after req.send(null)
is executed, req.responseText
contains the page source code. Generally it is recommended to avoid synchronous network actions however so a better approach would be to use an asynchronous request and add a callback for the load
event:
var req = new XMLHttpRequest();
req.open("GET", linkedPageUrl, true);
req.addEventListener("load", function()
{
DoSomethingWithGivenSource(req.responseText);
}, false);
req.send(null);
精彩评论