在我的 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 个新标签。
怎么了 ?谢谢