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

前端 未结 1 1574
情深已故
情深已故 2021-01-28 02:21

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 th

相关标签:
1条回答
  • 2021-01-28 03:00

    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);
    
    0 讨论(0)
提交回复
热议问题