如何使用javascript书签和关闭所有标签?

时间:2016-08-19 19:49:42

标签: javascript loops firefox-webextensions

到目前为止,我有代码保存当前标签的书签,然后在我按下WebExtension按钮时将其关闭。我希望代码保存然后关闭所有选项卡。

var currentTab;
var currentBookmark;

// gets active tabe
function callOnActiveTab(callback) {
    chrome.tabs.query({currentWindow: true}, function(tabs) {
      for (var tab of tabs) {
        if (tab.active) {
          callback(tab, tabs);
        }
      }
    });
}

/*
 * Add the bookmark on the current page.
 */
function Bookmark() {

    chrome.bookmarks.create({title: currentTab.title, url: currentTab.url}, function(bookmark) {
        currentBookmark = bookmark;
    });

    callOnActiveTab((tab) => {
        chrome.tabs.remove(tab.id);
    });

}

/*
 * Switches currentTab and currentBookmark to reflect the currently active tab
 */
function updateTab() {
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    if (tabs[0]) {
      currentTab = tabs[0];

      chrome.bookmarks.search({url: currentTab.url}, (bookmarks) => {
        currentBookmark = bookmarks[0];
      });
    }
  });
}



function listTabs() {

    Bookmark();

}


chrome.browserAction.onClicked.addListener(listTabs);


chrome.tabs.onUpdated.addListener(updateTab);
// listen to tab switching
chrome.tabs.onActivated.addListener(updateTab);

如果我将Bookmark()函数添加到updateTab()函数的末尾,则该按钮不再有效,当我更改标签时,它会保存该标签并退出所有标签。

1 个答案:

答案 0 :(得分:0)

相当一部分代码对于您尝试执行的操作显得过于复杂。您无法使用Bookmark函数来书签和删除多个标签的问题的一个重要部分是它依赖于由跟踪活动标签的异步事件处理程序更改的全局变量。该函数可以重新编码以使用传递给函数的参数。通过这种方式,它通常可以重复使用。

注意:我移除了bookmarkTab函数(代码中的Bookmark)的标签。将它放在那里,虽然只调用函数Bookmark,但这是一个坏主意。我添加了一个bookmarkAndRemoveTab()函数,它明确地命名了它正在做的事情。

browserAction相关联的部分可能是:

var currentBookmark;  
/* Add a bookmark for a tab
 *   tabsTab - The tabs.Tab object for the tab containing the page to bookmark
 *   callback - Called with the tabs.Tab object when the bookmark is complete
 */
function bookmarkTab(tabsTab, callback) {
    chrome.bookmarks.create({title: tabsTab.title, url: tabsTab.url}, function(bookmark) {
        currentBookmark = bookmark;
        if(typeof callback === 'function'){
            callback(tabsTab);
        }
    });
}
/* Remove a Tab
 *   tabsTab - The tabs.Tab object for the tab to remove
 *   callback - Called with the, now invalid, tab ID of the removed tab
 */
function removeTab(tabsTab, callback){
    let  rememberedId = tabsTab.id; //Unknown if object changes upon removal
    chrome.tabs.remove(rememberedId,function(){
        if(typeof callback === 'function'){
            callback(rememberedId);
        }
    });
}
/* Bookmark and remove a tab once the bookmark has been made
 *   tabsTab - The tabs.Tab object for the tab to remove
 */
function bookmarkAndRemoveTab(tabsTab) {
    //When we get here from the browserAction click, tabsTab is the active tab
    //  in the window where the button was clicked.  But, this function can be used
    //  anytime you have a tabs.Tab object for the tab you want to bookmark and delete.
    bookmarkTab(tabsTab,removeTab);
}
chrome.browserAction.onClicked.addListener(bookmarkAndRemoveTab);

然后你可以在每个标签上都有bookmarkAndRemoveTab()的功能:

/* Bookmark and remove all tabs
 */
function bookmarkAndRemoveAllTabs() {
    //Get all tabs in 'normal' windows:
    //  May want to test. Could want to get all tabs in all windows
    //  Of windowTypes:["normal","popup","panel","devtools", probably only
    //  want  "normal" and "popup" tabs to be bookmarked and closed.
    let queryInfos = [{windowType: 'normal'},{windowType: 'popup'}];
    queryInfos.forEach(function(queryInfo){
        chrome.tabs.query(queryInfo, function(tabs) {
            for (let tab of tabs) {
                bookmarkAndRemoveTab(tab);
            }
        });
    });
}