从googlechrome扩展中获取页面的源代码 [英] Getting the source code of a page from a googlechrome extension

查看:963
本文介绍了从googlechrome扩展中获取页面的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一份googlechrome的扩展程序,以显示网站中的项目列表。我遇到的问题是我无法获得我正在寻找的页面的源代码。当我尝试将它放入一个iframe中时,它的代码会改变窗口的位置。 XMLhttpRequest也只允许在你自己的域中。有没有什么办法可以通过JavaScript从googlechrome扩展中获取网站的源代码?



这里是清单:

  {
name:My First Extension,
version:1.0,
description: ,
browser_action:{
default_icon:icon.png,
popup:popup.html
} ,
权限:[
http://api.flickr.com/,
http://search.twitter.com/
]


解决方案

详细介绍此处。长话短说:


  1. 在清单中声明域权限。 后台页面。

  2. 使用消息传递,如果需要的话。

更新



以下代码适用于我:

清单 - 与您的完全一致。

popup.html:

 < html> 
< head>
< script>
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
alert(xhr.status);
if(xhr.status == 200){
alert(xhr.responseText);
}
}
}
var url =http://search.twitter.com/trends/current.json?exclude=hashtags;
xhr.open(GET,url,true);
xhr.send();

< / script>
< / head>
< body>< / body>
< / html>


I am writting an extension for googlechrome to display a list of items from a website. The problem I have is that I cannot get the source code of the page I am looking for. When I tried putting it in an iframe, it had code that would change the location of the window. XMLhttpRequest is also only allowed on your own domain. Is there any way I can get the source code for a site from a googlechrome extension through javascript?

here is the manifest:

{
  "name": "My First Extension",
  "version": "1.0",
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.png",
    "popup": "popup.html"
  },
  "permissions": [
    "http://api.flickr.com/",
    "http://search.twitter.com/"
  ]
 }

解决方案

It's all described in details here. Long story short:

  1. Declare domain permissions in the manifest.
  2. Make cross-domain request from a background page.
  3. Transfer results to a content script using message passing, if needed.

UPDATE

Here is code that works for me:

Manifest - exactly as yours.

popup.html:

<html>
<head>
<script>
    var xhr = new XMLHttpRequest(); 
    xhr.onreadystatechange = function() { 
        if (xhr.readyState == 4) { 
            alert(xhr.status); 
            if (xhr.status == 200) { 
                alert(xhr.responseText); 
            } 
        } 
    } 
    var url = "http://search.twitter.com/trends/current.json?exclude=hashtags"; 
    xhr.open("GET", url, true); 
    xhr.send();

</script>
</head>
<body></body>
</html>

这篇关于从googlechrome扩展中获取页面的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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