1

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

但是这个功能不能正常工作,有时它工作正常,有时它只打开它应该打开的所有 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 个新标签。

怎么了 ?谢谢

4

1 回答 1

3

我自己没有测试过(由于时间不够),但我相信问题是这样的:

问题
您正在从popup.js. 一旦新选项卡获得焦点,弹出窗口 ( popup.html) 就会关闭/隐藏,并且任何 JavaScript 执行都会停止。popup.js这意味着在第一个选项卡获得焦点之前,将打开许多选项卡。所以,有时会是全部,有时会是一些等等。

解决方案
解决方案是在后台页面(或更好的事件页面)中添加“标签创建”代码。例如:

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

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

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

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

背景.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);
        });
    });
});

清单.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"
    ]
}

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

于 2013-10-22T09:07:03.347 回答