Guest User

Untitled

a guest
May 24th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. function getTabsToClose(tabs,args) {
  2. var foundActiveTab = false;
  3. var toClose = [];
  4. function checkTab(tab) {
  5. var add = args === "above" ? !foundActiveTab : foundActiveTab;
  6. if (add) {
  7. if (!toClose.includes(tab.id)) {
  8. console.log(`Adding ${tab.id} - "${tab.title}" to toClose.`);
  9. toClose.push(tab.id);
  10. }
  11. }
  12. if (tab.states.includes("active")) {
  13. console.log(`Found active tab: ${tab.id} - "${tab.title}"`);
  14. foundActiveTab = true;
  15. toClose.pop();
  16. }
  17. if (tab.children === undefined || tab.children.length == 0) {
  18. return;
  19. }
  20. for (const child of tab.children) {
  21. checkTab(child);
  22. }
  23. }
  24.  
  25. for (const tab of tabs) {
  26. checkTab(tab);
  27. }
  28. console.log(`got ${toClose.length} tabs to close. ids=${toClose.join(",")}`);
  29.  
  30. executeInBackground(toClose => {
  31. browser.tabs.remove(toClose);
  32. }, [toClose])
  33. .then(() => console.log("executing in bg complete"))
  34. .catch(e => console.log(`caught exception: ${e}`));
  35. }
  36.  
  37.  
  38. console.log("running executeAsync");
  39. const kTST_ID = 'treestyletab@piro.sakura.ne.jp';
  40. async function executeAsync() {
  41. console.log("starting executeAsync");
  42. var tabs = await browser.runtime.sendMessage(kTST_ID, {
  43. type: 'get-tree', // or 'demote'
  44. tab: '*',
  45. window: 0
  46. });
  47. console.log(`got ${tabs.length} tabs`);
  48. getTabsToClose(tabs,"above");
  49. return true;
  50. }
  51.  
  52. executeAsync().then(q=>console.log(`something executeAsync: ${q}`)).catch(e=>console.log(`caught executeAsync error - ${e}`));
Add Comment
Please, Sign In to add comment