这是我的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吗?
答案 0 :(得分:1)
您的脚本中存在错误。 tweets
和xpath
未定义。在脚本的开头添加它以修复它:
var tweets, xpath;
要在弹出窗口中调试脚本,请单击扩展图标以打开弹出窗口,然后右键单击弹出窗口并选择检查元素。 控制台标签会显示错误。