简体   繁体   English

如何使用javascript标记并关闭所有标签?

[英]How to bookmark and close all tabs with javascript?

I have code so far that will save a bookmark of the current tab then close it when i push my WebExtension button. 到目前为止,我有代码可以保存当前选项卡的书签,然后在按下WebExtension按钮时将其关闭。 I want the code to save and then close all of the tabs. 我想要保存代码,然后关闭所有选项卡。

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);

If I add the Bookmark() function to the end of updateTab() function, the button no longer works and when I change tabs it saves that one and exits all tabs. 如果将Bookmark()函数添加到updateTab()函数的末尾,该按钮将不再起作用,并且当我更改选项卡时,它将保存该按钮并退出所有选项卡。

Quite a bit of your code appears overly complicated for what you appear to be attempting to do. 对于您看来要执行的操作,相当多的代码显得过于复杂。 A significant part of your problem with not being able to use the Bookmark function to bookmark and remove multiple tabs is that it is relying on a global variable that is changed by an asynchronous event handler which is tracking the active tab. 无法使用“ Bookmark功能添加书签和删除多个选项卡的一个重要问题是,它依赖于由跟踪活动选项卡的异步事件处理程序更改的全局变量。 That function can be re-coded to use an argument that is passed in to the function. 可以对该函数进行重新编码,以使用传递给该函数的参数。 In that way it can be generally re-used. 这样,它通常可以重新使用。

Note: I moved the removal of the tab out of the bookmarkTab function (what is Bookmark in your code). 注意:我将选项卡的删除移出了bookmarkTab函数(代码中的Bookmark )。 Having it in there, while only calling the function Bookmark , is a bad idea. 在其中仅调用函数Bookmark ,在其中放置它是一个坏主意。 I added a bookmarkAndRemoveTab() function which is clearly named for both things that it is doing. 我添加了一个bookmarkAndRemoveTab()函数,该函数为正在执行的两件事都明确命名。

Just the sections associated with your browserAction could be: 只是与您的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);

Then you could have a function that did bookmarkAndRemoveTab() on every tab: 然后,您可以拥有一个在每个选项卡上执行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);
            }
        });
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM