Chrome扩展程序读取当前页面的innerHTML? [英] Chrome extension read innerHTML of the current page?

查看:518
本文介绍了Chrome扩展程序读取当前页面的innerHTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我无法在任何地方找到答案。
我正在写一个扩展名为chrome的文件,我所需要的只是读取当前页面的html,所以我可以从中提取一些数据。



这里是到目前为止:

 < script> 
window.addEventListener(load,windowLoaded,false);
function windowLoaded(){
alert(document.innerHTML)
});
}
< / script>

有人可以告诉我我做错了什么吗?
thanks,

解决方案

  function windowLoaded(){
alert ('< html> + document.documentElement.innerHTML +'< / html>');
}
addEventListener(load,windowLoaded,false);

请注意, windowLoaded 是如何创建的在它被使用之前,不会在之后,这是行不通的。



另请注意,我如何获得文档的innerHTML。 documentElement ,它是 html 标记,然后在它周围添加 html 源标记。

Hi this may be a silly question, but I can't find the answer anywhere. I'm writing a chrome extension, all I need is to read in the html of the current page so I can extract some data from it.

here's what I have so far:

<script>
    window.addEventListener("load", windowLoaded, false);
    function windowLoaded() {
        alert(document.innerHTML)
      });
    }
</script>

Can anybody tell me what I'm doing wrong? thanks,

解决方案

function windowLoaded() {
    alert('<html>' + document.documentElement.innerHTML + '</html>');
}
addEventListener("load", windowLoaded, false);

Notice how windowLoaded is created before it is used, not after, which won't work.

Also notice how I am getting the innerHTML of document.documentElement, which is the html tag, then adding the html source tags around it.

这篇关于Chrome扩展程序读取当前页面的innerHTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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