0

这是我的 manifest.json:

    {
      "name": "Chritter",
      "version": "1.0",
      "description": "A Twitter button for your toolbar.",
      "icons": {"128": "icon.png"},
      "browser_action": {
        "default_icon": "icon.png",
        "default_title": "Chritter",
        "popup": "popup.html"
      },
     "permissions": ["http://twitter.com/*", "tabs"]
    }



This is my popup.html:

<html>
<head>
    <script>
onload = setTimeout(init, 0); // workaround for http://crbug.com/24467

// initialize timeline template
function init() {
  timeline = document.getElementById('timeline');
  template = xpath('//ol[@id="template"]/li', document);
  link = xpath('//div[@class="thumbnail"]/a', template);
  image = xpath('img', link);
  author = xpath('//div[@class="text"]/a', template);
  content = xpath('//div[@class="text"]/span', template);

  getTweets();
}
function getTweets() {
  req = new XMLHttpRequest();
  req.open('GET', 'http://twitter.com/statuses/public_timeline.json');
  req.onload = processTweets;
  req.send();
}
// process new batch of tweets
function processTweets() {
  var res = JSON.parse(req.responseText);
  tweets = res.concat(tweets);
  update();
}

for (var i in tweets) {
  user = tweets[i].user;
  url = 'http://twitter.com/' + user.screen_name;

  // thumbnail
  link.title = user.name;
  link.href = openInNewTab(url);
  image.src = user.profile_image_url;
  image.alt = user.name;

  // text
  author.href = openInNewTab(url);
  author.innerHTML = user.name;
  content.innerHTML = linkify(tweets[i].text);

  // copy node and update
  item = template.cloneNode(true);
  timeline.appendChild(item);
}


</script>
</head>
  <body>
  <div id="body">
    <div id="title">
      <h2>Chritter</h2>
    </div>
    <ol id="timeline" />
  </div>
  <ol id="template">
    <li>
      <div class="thumbnail">
        <a>
          <img />
        </a>
      </div>
      <div class="text">
        <a></a>
        <span></span>
      </div>
      <div class="clear"></div>
    </li>
  </ol>
</body>
</html>

当我点击扩展时,我没有得到任何数据。我做错什么了吗?我应该在 popup.js 而不是 popup.html 中编写 js 吗?

4

1 回答 1

1

您的脚本中有错误。tweets并且xpath没有定义。在脚本的开头添加它来修复它:

var tweets, xpath;

要在弹出窗口中调试脚本,请单击扩展图标以打开弹出窗口,然后右键单击弹出窗口并选择Inspect Element控制台选项卡将显示错误。

于 2012-09-18T08:27:36.393 回答