在循环中chrome.tabs.create的bug

时间:2013-10-21 19:43:58

标签: jquery google-chrome loops google-chrome-extension tabs

在我的Chrome扩展程序Copy All Urls上,我开发了一个“粘贴”功能,可以打开剪贴板中找到的所有网址,每个网址都有一个标签。

但是这个功能无法正常工作,有时它工作正常,有时它只会打开它应该打开的所有URL的几个子集。

我怀疑Chrome API,更具体地说chrome.tabs.create要对这个错误负责,所以我写了一小段代码来测试它:

popup.html:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="vendor/jquery-1.7.2_min.js"></script>
    <script type="text/javascript" src="popup.js"></script>
</head>
<body>
    <button id="actionTest">Test</button>
</body>
</html>

popup.js:

jQuery(function($){
    $('#actionTest').click(function(e){
        var urlList = ["http://stackoverflow.com/","http://en.wikipedia.org/wiki/Main_Page","http://www.codinghorror.com/blog/","http://nodejs.org/","https://github.com/","http://wallbase.cc/","http://www.chromium.org/Home","http://www.photoshoptuto.com/?s=test+chrome+tabs","http://www.youtube.com/","https://www.tumblr.com/","http://www.imdb.com/","http://www.flickr.com/","http://kickass.to/","https://www.dropbox.com/","http://fr.slideshare.net/","http://www.deviantart.com/","http://www.livejournal.com/","http://ohnotheydidnt.livejournal.com/82624099.html","http://www.etsy.com/","http://www.mediafire.com/","http://www.foxnews.com/","http://www.foxnews.com/politics/2013/10/21/obama-addresses-problems-with-health-care-website/"];
        $.each(urlList, function(key, val){
            chrome.tabs.create({ url: val}, function(tab){
                // console.log('openned: '+tab.url);
            });
        });
    });
});

如您所见,urlList包含22个URL,但是当我按下测试按钮时,我并不总是打开22个新标签。

怎么了? 感谢

1 个答案:

答案 0 :(得分:3)

我自己没有测试过(由于时间不够),但我认为问题是:

问题
您正在从popup.js创建标签。只要新选项卡获得焦点,弹出窗口(popup.html)就会被关闭/隐藏,并且任何JavaScript执行都会停止。这意味着将会有很多标签打开,因为许多popup.js设法在第一个标签获得焦点之前创建。所以,有时它会是全部,有时甚至是等等。

解决方案
解决方案是在后台页面(或更好的事件页面)中创建“制表符”代码。 E.g:

// ...in popup.js
...
    chrome.runtime.getBackgroundPage(function(bgPage) {
        bgPage.openAllURLsInTabs();
    });
...

(当然,有许多方法可能,例如消息传递等)。

演示
出于演示目的,我已经整理了一个小扩展,打开您的22个URL是新选项卡。为简单起见,它没有弹出窗口,只有浏览器动作 文件结构如下:

TestCX
     |__background.js
     |__jquery-1.7.2_min.js
     |__manifest.json

background.js:

var urlList = [
    "http://stackoverflow.com/",
    "http://en.wikipedia.org/wiki/Main_Page",
    "http://www.codinghorror.com/blog/",
    "http://nodejs.org/",
    "https://github.com/",
    "http://wallbase.cc/",
    "http://www.chromium.org/Home",
    "http://www.photoshoptuto.com/?s=test+chrome+tabs",
    "http://www.youtube.com/",
    "https://www.tumblr.com/",
    "http://www.imdb.com/",
    "http://www.flickr.com/",
    "http://kickass.to/",
    "https://www.dropbox.com/",
    "http://fr.slideshare.net/",
    "http://www.deviantart.com/",
    "http://www.livejournal.com/",
    "http://ohnotheydidnt.livejournal.com/82624099.html",
    "http://www.etsy.com/",
    "http://www.mediafire.com/",
    "http://www.foxnews.com/",
    "http://www.foxnews.com/politics/2013/10/21/obama-addresses-problems-with-health-care-website/"
];
chrome.browserAction.onClicked.addListener(function() {
    $.each(urlList, function(key, val) {
        chrome.tabs.create({ url: val }, function(tab) {
            console.log("Opened: " + tab.url);
        });
    });
});

的manifest.json:

{
    "manifest_version": 2,

    "name": "Paste All URLs",
    "version": "2.5",

    "browser_action": {
        "default_title": "Paste many URLs"
    },
    "background": {
        "scripts": [
            "jquery-1.7.2_min.js",
            "background.js"
        ]
    },
    "permissions": [
        "tabs"
    ]
}

我希望这可以帮助您解决问题。如果不再回来了:)