Google Chrome 扩展程序单击时打开多个选项卡
我的第一个问题,希望我问得对。我找遍了所有地方,却找不到任何东西。
基本上,我有以下代码,它的作用是当在 youtube.com/watch 页面上时,它在地址栏中显示一个图标,如果您单击该图标,它会将重建的高质量拇指放入本地存储中,然后打开拇指.html 只获取本地存储值并在页面上显示图像。我注意到,如果我在地址栏中按回车键加载页面,然后单击图标,它工作正常,但如果我通过页面导航到另一个视频并单击它,它会打开 2 页,如果我转到第三页,它会打开 3 页,依此类推。我只是不知道发生了什么事。
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
updatedTab = tab;
updatedTabId = tabId;
if(changeInfo.status == 'complete')
{
if(updatedTab.url.indexOf('youtube.com/watch') != -1)
{
chrome.pageAction.show(tabId);
chrome.pageAction.onClicked.addListener(function(tab)
{
if(window === top)
{
var yt = tab.url.split('v=');
var yt = yt[1].split('&');
var yURL = 'http://www.youtube.com/watch?v='+yt[0];
var yThumb = 'http://i2.ytimg.com/vi/'+yt[0]+'/hqdefault.jpg';
localStorage.setItem('ytHQthumb', yThumb);
chrome.tabs.create({'url': 'thumb.html'});
}
});
}
}
});
my first question, hopefully I'm asking it right. I searched all over and can't find anything on it.
Basically, I have the following code, what it does is when on a youtube.com/watch page it shows an icon in the address bar, if you click that icon, it put's the high quality thumb it reconstructed into localstorage and then opens thumb.html which just grabs that localstorage value and displays the image on the page. I noticed if I hit enter in the address bar to load the page, and click the icon, it works fine, but if I navigate to another video via the page and click it, it opens 2 pages, if I go to a 3rd page, it opens 3 pages, so on, and so forth. I just don't know what's going on.
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
updatedTab = tab;
updatedTabId = tabId;
if(changeInfo.status == 'complete')
{
if(updatedTab.url.indexOf('youtube.com/watch') != -1)
{
chrome.pageAction.show(tabId);
chrome.pageAction.onClicked.addListener(function(tab)
{
if(window === top)
{
var yt = tab.url.split('v=');
var yt = yt[1].split('&');
var yURL = 'http://www.youtube.com/watch?v='+yt[0];
var yThumb = 'http://i2.ytimg.com/vi/'+yt[0]+'/hqdefault.jpg';
localStorage.setItem('ytHQthumb', yThumb);
chrome.tabs.create({'url': 'thumb.html'});
}
});
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题了。我需要将 chrome.pageAction.onClicked.addListener 移到 chrome.tabs.onUpdated.addListener 之外。
Found the problem. I needed to move the
chrome.pageAction.onClicked.addListener
outside of thechrome.tabs.onUpdated.addListener
.