Skip to content

Instantly share code, notes, and snippets.

@wataf1
Created May 19, 2018 21:42
Show Gist options
  • Save wataf1/871261f5719017c700634ce989e54fd7 to your computer and use it in GitHub Desktop.
Save wataf1/871261f5719017c700634ce989e54fd7 to your computer and use it in GitHub Desktop.
Closes all tabs above the belowtab in TreeStyleTabls
function getTabsToClose(tabs,args) {
var foundActiveTab = false;
var toClose = [];
function checkTab(tab) {
var add = args === "above" ? !foundActiveTab : foundActiveTab;
if (add) {
if (!toClose.includes(tab.id)) {
console.log(`Adding ${tab.id} - "${tab.title}" to toClose.`);
toClose.push(tab.id);
}
}
if (tab.states.includes("active")) {
console.log(`Found active tab: ${tab.id} - "${tab.title}"`);
foundActiveTab = true;
toClose.pop();
}
if (tab.children === undefined || tab.children.length == 0) {
return;
}
for (const child of tab.children) {
checkTab(child);
}
}
for (const tab of tabs) {
checkTab(tab);
}
console.log(`got ${toClose.length} tabs to close. ids=${toClose.join(",")}`);
executeInBackground(toClose => {
browser.tabs.remove(toClose);
}, [toClose])
.then(() => console.log("executing in bg complete"))
.catch(e => console.log(`caught exception: ${e}`));
}
console.log("running executeAsync");
const kTST_ID = 'treestyletab@piro.sakura.ne.jp';
async function executeAsync() {
console.log("starting executeAsync");
var tabs = await browser.runtime.sendMessage(kTST_ID, {
type: 'get-tree', // or 'demote'
tab: '*',
window: 0
});
console.log(`got ${tabs.length} tabs`);
getTabsToClose(tabs,"below");
return true;
}
executeAsync().then(q=>console.log(`something executeAsync: ${q}`)).catch(e=>console.log(`caught executeAsync error - ${e}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment