gpt4 book ai didi

javascript - 从 googlechrome 扩展获取页面的源代码

转载 作者:行者123 更新时间:2023-11-28 02:46:56 24 4
gpt4 key购买 nike

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

这是 list :

{
"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/"
]
}

最佳答案

都详细描述了here 。长话短说:

  1. 在 list 中声明域权限。
  2. 从后台页面发出跨域请求。
  3. 使用 message passing 将结果传输到内容脚本,如果需要的话。

更新

这是适合我的代码:

list - 与您的完全一样。

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>

关于javascript - 从 googlechrome 扩展获取页面的源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4728960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com