Browse Source

fix: auto install

Ahmad Kholid 2 years ago
parent
commit
696200aba5
1 changed files with 32 additions and 33 deletions
  1. 32 33
      src/content/index.js

+ 32 - 33
src/content/index.js

@@ -244,38 +244,37 @@ function messageListener({ data, source }) {
   });
 })();
 
-// Auto install only works on Chrome
-async function autoInstall() {
-  const link = window.location.href;
-  if (/.+\.automa\.json$/.test(link)) {
-    const accept = window.confirm(
-      'Do you want to add this workflow into Automa?'
-    );
-    if (!accept) return;
-    const workflow = JSON.parse(document.body.innerText);
-
-    const { workflows: workflowsStorage } = await browser.storage.local.get(
-      'workflows'
-    );
-
-    const workflowId = nanoid();
-    const workflowData = {
-      ...workflow,
-      id: workflowId,
-      dataColumns: [],
-      createdAt: Date.now(),
-      table: workflow.table || workflow.dataColumns,
-    };
-
-    if (Array.isArray(workflowsStorage)) {
-      workflowsStorage.push(workflowData);
-    } else {
-      workflowsStorage[workflowId] = workflowData;
-    }
+window.addEventListener('DOMContentLoaded', async () => {
+  const link = window.location.pathname;
+  const isAutomaWorkflow = /.+\.automa\.json$/.test(link);
+  if (!isAutomaWorkflow) return;
+
+  const accept = window.confirm(
+    'Do you want to add this workflow into Automa?'
+  );
+  if (!accept) return;
+  const workflow = JSON.parse(document.documentElement.innerText);
+
+  const { workflows: workflowsStorage } = await browser.storage.local.get(
+    'workflows'
+  );
+
+  const workflowId = nanoid();
+  const workflowData = {
+    ...workflow,
+    id: workflowId,
+    dataColumns: [],
+    createdAt: Date.now(),
+    table: workflow.table || workflow.dataColumns,
+  };
+
+  if (Array.isArray(workflowsStorage)) {
+    workflowsStorage.push(workflowData);
+  } else {
+    workflowsStorage[workflowId] = workflowData;
+  }
 
-    await browser.storage.local.set({ workflows: workflowsStorage });
+  await browser.storage.local.set({ workflows: workflowsStorage });
 
-    alert('Workflow installed');
-  }
-}
-autoInstall();
+  alert('Workflow installed');
+});