handler-new-tab.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import browser from 'webextension-polyfill';
  2. import { getBlockConnection } from '../helper';
  3. async function newTab(block) {
  4. if (this.windowId) {
  5. try {
  6. await browser.windows.get(this.windowId);
  7. } catch (error) {
  8. this.windowId = null;
  9. }
  10. }
  11. const nextBlockId = getBlockConnection(block);
  12. try {
  13. const { updatePrevTab, url, active, inGroup } = block.data;
  14. if (updatePrevTab && this.activeTab.id) {
  15. await browser.tabs.update(this.activeTab.id, { url, active });
  16. } else {
  17. const tab = await browser.tabs.create({
  18. url,
  19. active,
  20. windowId: this.windowId,
  21. });
  22. this.activeTab.id = tab.id;
  23. this.activeTab.url = url;
  24. this.windowId = tab.windowId;
  25. }
  26. if (inGroup && !updatePrevTab) {
  27. const options = {
  28. groupId: this.activeTab.groupId,
  29. tabIds: this.activeTab.id,
  30. };
  31. if (!this.activeTab.groupId) {
  32. options.createProperties = {
  33. windowId: this.windowId,
  34. };
  35. }
  36. chrome.tabs.group(options, (tabGroupId) => {
  37. this.activeTab.groupId = tabGroupId;
  38. });
  39. }
  40. this.activeTab.frameId = 0;
  41. return {
  42. data: url,
  43. nextBlockId,
  44. };
  45. } catch (error) {
  46. console.error(error);
  47. error.nextBlockId = nextBlockId;
  48. throw error;
  49. }
  50. }
  51. export default newTab;