我有一个 chrome 扩展,其中有
我调用 jquery.js、popup.js 和 link.js 的 popup.html
...
<script src="jquery.js"></script>
<script src="popup.js"></script>
<script src="link.js"></script>
</head>
popup.js
var streamGenerator = {
/* Twitch URL to the top 20 streams */
searchOnTwitch_: 'https://api.twitch.tv/kraken/streams?limit=20',
/* XHR Request to grab those streams */
requestStreams: function() {
var req = new XMLHttpRequest();
req.open("GET", this.searchOnTwitch_, true);
req.onload = this.showStreams_;
req.send(null);
},
/* onload event inserting the streams into the DOM */
showStreams_: function(e) {
var streams = JSON.parse(e.target.responseText).streams;
for (var i=0; i < streams.length; i++) {
var stream = {
game: streams[i].game,
name: streams[i].channel.name,
viewers: streams[i].viewers,
link: streams[i].channel.url,
};
$('tbody').append('<tr>'
+ '<td>' + stream.game + '</td>'
+ '<td>' + '<a href=' + stream.link + '>' + stream.name + '</a></td>'
+ '<td>' + stream.viewers + '</td></a>'
);
}
}
};
document.addEventListener('DOMContentLoaded', function() {
streamGenerator.requestStreams();
});
然后是 link.js
$(function() {
$('a').click(function(){
chrome.tabs.create({url: $(this).attr('href')});
});
})
弹出窗口有效并显示它应该显示的内容,但链接不会打开新选项卡。
如果我去 chrome 的控制台,我不会收到任何错误,但如果我复制/粘贴 link.js jquery,链接会在之后工作;
我究竟做错了什么?!