Browse Source

feat: add close window option in close tab block

Ahmad Kholid 3 years ago
parent
commit
36f4336fc3

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "automa",
-  "version": "0.13.2",
+  "version": "0.13.3",
   "description": "An extension for automating your browser by connecting blocks",
   "license": "MIT",
   "repository": {

+ 41 - 13
src/background/workflow-engine/blocks-handler/handler-close-tab.js

@@ -1,22 +1,52 @@
 import browser from 'webextension-polyfill';
 import { getBlockConnection } from '../helper';
 
-async function closeTab({ data, outputs }) {
+async function closeWindow(data, windowId) {
+  const windowIds = [];
+
+  if (data.allWindows) {
+    const windows = await browser.windows.getAll();
+
+    windows.forEach(({ id }) => {
+      windowIds.push(id);
+    });
+  } else {
+    let currentWindowId;
+
+    if (windowId && typeof windowId === 'number') {
+      currentWindowId = windowId;
+    } else {
+      currentWindowId = (await browser.windows.getCurrent()).id;
+    }
+
+    windowIds.push(currentWindowId);
+  }
+
+  await Promise.allSettled(windowIds.map((id) => browser.windows.remove(id)));
+}
+
+async function closeTab(data, tabId) {
+  let tabIds;
+
+  if (data.activeTab && tabId) {
+    tabIds = tabId;
+  } else if (data.url) {
+    tabIds = (await browser.tabs.query({ url: data.url })).map((tab) => tab.id);
+  }
+
+  if (tabIds) await browser.tabs.remove(tabIds);
+}
+
+export default async function ({ data, outputs }) {
   const nextBlockId = getBlockConnection({ outputs });
 
   try {
-    let tabIds;
-
-    if (data.activeTab && this.activeTab.id) {
-      tabIds = this.activeTab.id;
-    } else if (data.url) {
-      tabIds = (await browser.tabs.query({ url: data.url })).map(
-        (tab) => tab.id
-      );
+    if (data.closeType === 'window') {
+      await closeWindow(data, this.windowId);
+    } else {
+      await closeTab(data, this.activeTab.id);
     }
 
-    if (tabIds) await browser.tabs.remove(tabIds);
-
     return {
       nextBlockId,
       data: '',
@@ -27,5 +57,3 @@ async function closeTab({ data, outputs }) {
     throw error;
   }
 }
-
-export default closeTab;

+ 49 - 24
src/components/newtab/workflow/edit/EditCloseTab.vue

@@ -1,31 +1,55 @@
 <template>
   <div class="mb-2 mt-4">
-    <div class="mb-2">
-      <ui-checkbox
-        :model-value="data.activeTab"
-        @change="updateData({ activeTab: $event })"
-      >
-        {{ t('workflow.blocks.close-tab.activeTab') }}
-      </ui-checkbox>
-    </div>
-    <ui-input
-      v-if="!data.activeTab"
-      :model-value="data.url"
-      placeholder="http://example.com/*"
-      @change="updateData({ url: $event })"
+    <ui-select
+      :model-value="data.closeType"
+      placeholder="Close"
+      class="w-full mb-4"
+      @change="updateData({ closeType: $event })"
     >
-      <template #label>
-        {{ t('workflow.blocks.close-tab.url') }}
-        <a
-          href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns"
-          target="_blank"
-          rel="noopener"
-          title="More info"
+      <option
+        v-for="type in types"
+        :key="type"
+        :value="type"
+        class="capitalize"
+      >
+        {{ type }}
+      </option>
+    </ui-select>
+    <template v-if="data.closeType === 'tab'">
+      <div class="mb-2">
+        <ui-checkbox
+          :model-value="data.activeTab"
+          @change="updateData({ activeTab: $event })"
         >
-          &#9432;
-        </a>
-      </template>
-    </ui-input>
+          {{ t('workflow.blocks.close-tab.activeTab') }}
+        </ui-checkbox>
+      </div>
+      <ui-input
+        v-if="!data.activeTab"
+        :model-value="data.url"
+        placeholder="http://example.com/*"
+        @change="updateData({ url: $event })"
+      >
+        <template #label>
+          {{ t('workflow.blocks.close-tab.url') }}
+          <a
+            href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns"
+            target="_blank"
+            rel="noopener"
+            title="More info"
+          >
+            &#9432;
+          </a>
+        </template>
+      </ui-input>
+    </template>
+    <ui-checkbox
+      v-else
+      :model-value="data.allWindows"
+      @change="updateData({ allWindows: $event })"
+    >
+      {{ t('workflow.blocks.close-tab.allWindows') }}
+    </ui-checkbox>
   </div>
 </template>
 <script setup>
@@ -40,6 +64,7 @@ const props = defineProps({
 const emit = defineEmits(['update:data']);
 
 const { t } = useI18n();
+const types = ['tab', 'window'];
 
 function updateData(value) {
   emit('update:data', { ...props.data, ...value });

+ 4 - 3
src/locales/en/blocks.json

@@ -137,10 +137,11 @@
         "description": "Go forward to the next page"
       },
       "close-tab": {
-        "name": "Close tab",
+        "name": "Close tab/window",
         "description": "",
-        "activeTab": "Close activeTab",
-        "url": "URL or match pattern"
+        "url": "URL or match pattern",
+        "activeTab": "Close active tab",
+        "allWindows": "Close all windows"
       },
       "event-click": {
         "name": "Click element",

+ 3 - 1
src/utils/shared.js

@@ -139,7 +139,7 @@ export const tasks = {
     data: {},
   },
   'close-tab': {
-    name: 'Close tab',
+    name: 'Close tab/window',
     icon: 'riCloseCircleLine',
     component: 'BlockBasic',
     category: 'browser',
@@ -151,6 +151,8 @@ export const tasks = {
     data: {
       url: '',
       activeTab: true,
+      closeType: 'tab',
+      allWindows: false,
     },
   },
   'take-screenshot': {