0

我正在尝试创建一个 chrome 扩展程序,perkytweets该扩展程序在弹出的扩展程序中显示带有井号标签的推文,但没有显示任何内容。

这是 manifest.json 代码

    {
  "name": "My First Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

这是 popup.html 代码

<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <style>
      body {
        min-width:357px;
        overflow-x:hidden;
      }

      img {
        margin:5px;
        border:2px solid black;
        vertical-align:middle;
        width:75px;
        height:75px;
      }
    </style>


    <script src="script.js"></script>
  </head>
  <body>
  </body>
</html>

这是 script.js 代码

var req = new XMLHttpRequest();
req.open(
    "GET",
    "http://search.twitter.com/search.atom?q=perkytweets",
    true);
req.onload = showTweets;
req.send(null);


function showTweets()
{
    document.body.appendChild(req.responseXML);
}
4

1 回答 1

1

仅在加载文档时才需要触发请求。尝试将您的代码包装在onload处理程序中

function showTweets() {
    document.body.appendChild(req.responseXML);
}

var req;
window.onload = function() {
    req = new XMLHttpRequest();
    req.open("GET", "http://search.twitter.com/search.atom?q=perkytweets", true);
    req.onload = showTweets;
    req.send(null);
}

更新

您需要允许访问 twitter URL,将其添加到您的清单中:

"permissions": [ "http://search.twitter.com/*" ]

所以你的manifest.json遗嘱现在看起来像这样:

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

更新

req.responseXML还不是一个元素,因此不能直接附加到身体,console.log你的结果并从那里去。

于 2013-01-03T13:00:50.043 回答