0

I'm writing a Chrome Extension and am having some difficulty getting more than 5 tabs open. here is the source code.

manifset.json

{
  "manifest_version": 2,

  "name": "Test",
  "description": "testing this",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "bookmarks"
  ]
}

popup.html

<!doctype html>
<html>
  <head>
    <title>Testing</title>
    <style>
      body {
        min-width: 357px;
        overflow-x: hidden;
      }

    </style>
    <script src="popup.js"></script>
  </head>
  <body>
  </body>
</html>

popup.js

document.addEventListener('DOMContentLoaded', function () {
  chrome.bookmarks.getTree(function (stuff){
    traverseBookmarks(stuff[0].children[0].children);
  });
});

  function traverseBookmarks(bookmarkTreeNodes) {
    for(var i=0;i<bookmarkTreeNodes.length;i++) {
      var bookmark = document.createElement('a');

      if(bookmarkTreeNodes[i].url){
        bookmark.href = bookmarkTreeNodes[i].url;
        bookmark.target = "_blank";
      }
      else{
        (function(num) {
          bookmark.addEventListener("click", function() {
            addChildren(bookmarkTreeNodes[num].children, false );
          })})(i);
        document.body.appendChild(document.createElement("br"));
      }
      bookmark.innerHTML = bookmarkTreeNodes[i].title;
      document.body.appendChild(bookmark);
      document.body.appendChild(document.createElement("br"));

      if(bookmarkTreeNodes[i].children) {
        traverseBookmarks(bookmarkTreeNodes[i].children);
      }
    }
  }

  function addChildren(children) {
    for(var i = 0; i < children.length; i++){
      // will open each link in the current window
      chrome.tabs.create({
        url: children[i].url
      });
    }
  }

The goal is to be able to click the folder(now just a link symbolizing the folder) and open all links inside that folder. Currently what happens in I click on the link to the folder and it opens the first 5. For the sake of getting some form of logging I added:

var bookmark = document.createElement('a');
bookmark.innerHTML = children[i].title;
document.body.appendChild(bookmark);
document.body.appendChild(document.createElement("br"));

to the addChildren() function. It prints out every child. The issue I'm running into is that when I click on the folder it only opens up the first 5 tabs then I'm guessing the focus leaves the popup so it doesn't finish. I can't really find anything else online to help

Any help is appreciated. Let me know if I need to clarify anything.

4

1 回答 1

2

我怀疑问题就像你说的那样,当你创建一个新标签时active,默认情况下它是这样的,所以弹出窗口失去焦点并卸载。您可以通过以下两种方式之一解决此问题。您可以将 url 列表发送到背景页面,然后将它们全部打开。这将起作用,因为打开选项卡时背景页面保持加载状态。

另一种可能更好的修复方法是创建active属性设置为 false 的选项卡,然后在它们全部打开后切换到组中的第一个选项卡。代码:

function addChildren(children) {
  for(var i = 0; i < children.length; i++){
    chrome.tabs.create({
      url: children[i].url,
      active: false
    });
  }
  // switch focus to first opened tab
  chrome.tabs.query({url:children[0].url},function(tabs){
    chrome.tabs.update(tabs[tabs.length-1].id,{active:true});
  });
}
于 2013-05-24T19:33:47.590 回答