Ver Fonte

feat: executing workflow through URL(#1607)

Ahmad Kholid há 1 ano atrás
pai
commit
5f1caf1173
3 ficheiros alterados com 78 adições e 0 exclusões
  1. 10 0
      src/execute/index.html
  2. 61 0
      src/execute/index.js
  3. 7 0
      webpack.config.js

+ 10 - 0
src/execute/index.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<meta name="viewport" content="width=device-width, initial-scale=1.0">
+	<title>Execute</title>
+</head>
+<body>
+</body>
+</html>

+ 61 - 0
src/execute/index.js

@@ -0,0 +1,61 @@
+import { sendMessage } from '@/utils/message';
+import Browser from 'webextension-polyfill';
+
+function getWorkflowDetail() {
+  let hash = window.location.hash.slice(1);
+  if (!hash.startsWith('/')) hash = `/${hash}`;
+
+  const { pathname, searchParams } = new URL(window.location.origin + hash);
+
+  const variables = {};
+  const { 1: workflowId } = pathname.split('/');
+
+  searchParams.forEach((key, value) => {
+    variables[key] = decodeURIComponent(value);
+  });
+
+  return { workflowId: workflowId ?? '', variables };
+}
+
+function writeResult(text) {
+  document.body.innerText = text;
+}
+
+(async () => {
+  try {
+    const { workflowId, variables } = getWorkflowDetail();
+    if (!workflowId) {
+      writeResult('Invalid path');
+      return;
+    }
+
+    const { workflows } = await Browser.storage.local.get('workflows');
+
+    let workflow = workflows[workflowId];
+    if (!workflow && Array.isArray(workflows)) {
+      workflow = workflows.find((item) => item.id === workflowId);
+    }
+
+    if (!workflow) {
+      writeResult('Workflow not found');
+      return;
+    }
+
+    const hasVariables = Object.keys(variables).length > 0;
+
+    writeResult('Executing workflow');
+
+    sendMessage(
+      'workflow:execute',
+      {
+        ...workflow,
+        options: { checkParam: !hasVariables, data: { variables } },
+      },
+      'background'
+    ).then(() => {
+      setTimeout(window.close, 1000);
+    });
+  } catch (error) {
+    console.error(error);
+  }
+})();

+ 7 - 0
webpack.config.js

@@ -41,6 +41,7 @@ const options = {
   mode: process.env.NODE_ENV || 'development',
   entry: {
     sandbox: path.join(__dirname, 'src', 'sandbox', 'index.js'),
+    execute: path.join(__dirname, 'src', 'execute', 'index.js'),
     newtab: path.join(__dirname, 'src', 'newtab', 'index.js'),
     popup: path.join(__dirname, 'src', 'popup', 'index.js'),
     params: path.join(__dirname, 'src', 'params', 'index.js'),
@@ -200,6 +201,12 @@ const options = {
       chunks: ['sandbox'],
       cache: false,
     }),
+    new HtmlWebpackPlugin({
+      template: path.join(__dirname, 'src', 'execute', 'index.html'),
+      filename: 'execute.html',
+      chunks: ['execute'],
+      cache: false,
+    }),
     new HtmlWebpackPlugin({
       template: path.join(__dirname, 'src', 'popup', 'index.html'),
       filename: 'popup.html',