How to Extract/Retrieve source code given URL in Firefox extention

╄→гoц情女王★ 提交于 2019-12-02 17:28:52

问题


I'm writing a Firefox extension that will analyze / parse the linked pages while the user is still on the current page. I know 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!


回答1:


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);


来源:https://stackoverflow.com/questions/4965944/how-to-extract-retrieve-source-code-given-url-in-firefox-extention

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!