handlerActiveTab.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import browser from 'webextension-polyfill';
  2. import { sleep } from '@/utils/helper';
  3. import { attachDebugger, injectPreloadScript } from '../helper';
  4. async function activeTab(block) {
  5. try {
  6. const data = {
  7. data: '',
  8. nextBlockId: this.getBlockConnections(block.id),
  9. };
  10. if (this.activeTab.id) {
  11. await browser.tabs.update(this.activeTab.id, { active: true });
  12. return data;
  13. }
  14. const minimizeDashboard = async (currentWindow) => {
  15. if (currentWindow.type !== 'popup') return;
  16. const [tab] = currentWindow.tabs;
  17. const isDashboard = tab && tab.url.includes(browser.runtime.getURL(''));
  18. const isWindowFocus =
  19. currentWindow.focused || currentWindow.state === 'maximized';
  20. if (isWindowFocus && isDashboard) {
  21. const windowOptions = { focused: false };
  22. if (currentWindow.state === 'maximized')
  23. windowOptions.state = 'minimized';
  24. await browser.windows.update(currentWindow.id, windowOptions);
  25. }
  26. };
  27. if (this.engine.isPopup) {
  28. const currentWindow = await browser.windows.getCurrent({
  29. populate: true,
  30. });
  31. await minimizeDashboard(currentWindow);
  32. } else {
  33. const allWindows = await browser.windows.getAll({ populate: true });
  34. for (const currWindow of allWindows) {
  35. await minimizeDashboard(currWindow);
  36. }
  37. }
  38. const [tab] = await browser.tabs.query({
  39. active: true,
  40. lastFocusedWindow: true,
  41. });
  42. if (!tab || !tab?.url.startsWith('http')) {
  43. const error = new Error('invalid-active-tab');
  44. error.data = { url: tab?.url };
  45. throw error;
  46. }
  47. this.activeTab = {
  48. ...this.activeTab,
  49. frameId: 0,
  50. id: tab.id,
  51. url: tab.url,
  52. };
  53. this.windowId = tab.windowId;
  54. if (this.settings.debugMode) {
  55. await attachDebugger(tab.id, this.activeTab.id);
  56. this.debugAttached = true;
  57. }
  58. if (this.preloadScripts.length > 0) {
  59. if (this.engine.isMV2) {
  60. await this._sendMessageToTab({
  61. isPreloadScripts: true,
  62. label: 'javascript-code',
  63. data: { scripts: this.preloadScripts },
  64. });
  65. } else {
  66. await injectPreloadScript({
  67. scripts: this.preloadScripts,
  68. frameSelector: this.frameSelector,
  69. target: {
  70. tabId: this.activeTab.id,
  71. frameIds: [this.activeTab.frameId || 0],
  72. },
  73. });
  74. }
  75. }
  76. await browser.tabs.update(tab.id, { active: true });
  77. await browser.windows.update(tab.windowId, { focused: true });
  78. await sleep(200);
  79. return data;
  80. } catch (error) {
  81. console.error(error);
  82. error.data = error.data || {};
  83. throw error;
  84. }
  85. }
  86. export default activeTab;