如何在Firefox扩展中提取/检索给定URL的源代码 [英] How to Extract/Retrieve source code given URL in Firefox extention

查看:135
本文介绍了如何在Firefox扩展中提取/检索给定URL的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Firefox扩展,它将在用户仍然在当前页面上时分析/解析链接的页面。我知道有办法检索当前正在查看的页面的源代码,但链接的页面呢?如果我能够获取链接页面的URL,我将能够检索该URL的源代码?

我的扩展主要是用XUL编写的, JavaScript,任何代码或示例插件都将帮助我很多!

解决方案

也许以下JavaScript代码段将帮助您: / p>

  var req = new XMLHttpRequest(); 
req.open(GET,linkedPageUrl,false);
req.send(null);
DoSomethingWithGivenSource(req.responseText);

这可以同步工作。因此,在执行 req.send(null)之后, req.responseText 包含页面源代码。通常建议避免同步网络操作,所以更好的方法是使用异步请求并为 load 事件添加回调:

  var req = new XMLHttpRequest(); 
req.open(GET,linkedPageUrl,true);
req.addEventListener(load,function()
{
DoSomethingWithGivenSource(req.responseText);
},false);
req.send(null);


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!

解决方案

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

这篇关于如何在Firefox扩展中提取/检索给定URL的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆