Browse Source

fix: take screenshot block

Ahmad Kholid 2 years ago
parent
commit
862d17a482

+ 0 - 20
src/background/WorkflowLogger.js

@@ -1,20 +0,0 @@
-import dbLogs, { defaultLogItem } from '@/db/logs';
-/* eslint-disable class-methods-use-this */
-class WorkflowLogger {
-  constructor({ key = 'logs' }) {
-    this.key = key;
-  }
-
-  async add({ detail, history, ctxData, data }) {
-    const logDetail = { ...defaultLogItem, ...detail };
-
-    await Promise.all([
-      dbLogs.logsData.add(data),
-      dbLogs.ctxData.add(ctxData),
-      dbLogs.items.add(logDetail),
-      dbLogs.histories.add(history),
-    ]);
-  }
-}
-
-export default WorkflowLogger;

+ 0 - 91
src/background/WorkflowState.js

@@ -1,91 +0,0 @@
-/* eslint-disable  no-param-reassign */
-
-class WorkflowState {
-  constructor({ storage, key = 'workflowState' }) {
-    this.key = key;
-    this.storage = storage;
-
-    this.states = new Map();
-    this.eventListeners = {};
-  }
-
-  _saveToStorage() {
-    const states = Object.fromEntries(this.states);
-    return this.storage.set(this.key, states);
-  }
-
-  dispatchEvent(name, params) {
-    const listeners = this.eventListeners[name];
-
-    if (!listeners) return;
-
-    listeners.forEach((callback) => {
-      callback(params);
-    });
-  }
-
-  on(name, listener) {
-    (this.eventListeners[name] = this.eventListeners[name] || []).push(
-      listener
-    );
-  }
-
-  off(name, listener) {
-    const listeners = this.eventListeners[name];
-    if (!listeners) return;
-
-    const index = listeners.indexOf(listener);
-    if (index !== -1) listeners.splice(index, 1);
-  }
-
-  get getAll() {
-    return this.states;
-  }
-
-  async get(stateId) {
-    let { states } = this;
-
-    if (typeof stateId === 'function') {
-      states = Array.from(states.entries()).find(({ 1: state }) =>
-        stateId(state)
-      );
-    } else if (stateId) {
-      states = this.states.get(stateId);
-    }
-
-    return states;
-  }
-
-  async add(id, data = {}) {
-    this.states.set(id, data);
-    await this._saveToStorage(this.key);
-  }
-
-  async stop(id) {
-    const isStateExist = await this.get(id);
-    if (!isStateExist) {
-      await this.delete(id);
-      this.dispatchEvent('stop', id);
-      return id;
-    }
-
-    await this.update(id, { isDestroyed: true });
-    this.dispatchEvent('stop', id);
-    return id;
-  }
-
-  async update(id, data = {}) {
-    const state = this.states.get(id);
-    this.states.set(id, { ...state, ...data });
-    this.dispatchEvent('update', { id, data });
-    await this._saveToStorage();
-  }
-
-  async delete(id) {
-    this.states.delete(id);
-    this.dispatchEvent('delete', id);
-    await this._saveToStorage();
-  }
-}
-
-export default WorkflowState;

+ 2 - 2
src/background/index.js

@@ -92,8 +92,8 @@ message.on('debugger:type', ({ tabId, commands, delay }) => {
 
 message.on('get:sender', (_, sender) => sender);
 message.on('get:file', (path) => getFile(path));
-message.on('get:tab-screenshot', (options) =>
-  browser.tabs.captureVisibleTab(options)
+message.on('get:tab-screenshot', (options, sender) =>
+  browser.tabs.captureVisibleTab(sender.tab.windowId, options)
 );
 
 message.on('dashboard:refresh-packages', async () => {

+ 1 - 1
src/content/blocksHandler/handlerTakeScreenshot.js

@@ -122,6 +122,7 @@ export default async function ({ tabId, options, data: { type, selector } }) {
 
   document.body.classList.add('is-screenshotting');
 
+  const style = injectStyle();
   const canvas = document.createElement('canvas');
   const context = canvas.getContext('2d');
   const maxCanvasSize = BROWSER_TYPE === 'firefox' ? 32767 : 65035;
@@ -137,7 +138,6 @@ export default async function ({ tabId, options, data: { type, selector } }) {
 
   scrollableElement.classList?.add('automa-scrollable-el');
 
-  const style = injectStyle();
   const originalYPosition = window.scrollY;
   let originalScrollHeight = scrollableElement.scrollHeight;
 

+ 6 - 0
src/newtab/utils/workflowEngine/blocksHandler/handlerActiveTab.js

@@ -1,4 +1,5 @@
 import browser from 'webextension-polyfill';
+import { sleep } from '@/utils/helper';
 import { attachDebugger } from '../helper';
 
 async function activeTab(block) {
@@ -46,6 +47,11 @@ async function activeTab(block) {
       await Promise.allSettled(preloadScripts);
     }
 
+    await browser.tabs.update(tab.id, { active: true });
+    await browser.windows.update(tab.windowId, { focused: true });
+
+    await sleep(200);
+
     return data;
   } catch (error) {
     console.error(error);

+ 11 - 4
src/newtab/utils/workflowEngine/blocksHandler/handlerTakeScreenshot.js

@@ -61,16 +61,23 @@ async function takeScreenshot({ data, id, label }) {
 
       let tab = null;
       const isChrome = BROWSER_TYPE === 'chrome';
-      const captureTab = () => {
-        if (isChrome) return browser.tabs.captureVisibleTab(options);
+      const captureTab = async () => {
+        let result = null;
+
+        if (isChrome) {
+          console.log(tab);
+          result = await browser.tabs.captureVisibleTab(tab.windowId, options);
+        } else {
+          result = await browser.tabs.captureTab(this.activeTab.id, options);
+        }
 
-        return browser.tabs.captureTab(this.activeTab.id, options);
+        return result;
       };
 
       if (isChrome) {
         [tab] = await browser.tabs.query({
           active: true,
-          currentWindow: true,
+          url: '*://*/*',
         });
 
         if (this.windowId) {