Kaynağa Gözat

feat: support more XPath syntax

Ahmad Kholid 2 yıl önce
ebeveyn
işleme
c0c90cad62

+ 2 - 3
src/background/workflowEngine/blocksHandler/handlerLoopData.js

@@ -1,4 +1,4 @@
-import { parseJSON } from '@/utils/helper';
+import { parseJSON, isXPath } from '@/utils/helper';
 import { getBlockConnection } from '../helper';
 
 async function loopData({ data, id, outputs }, { refData }) {
@@ -35,7 +35,6 @@ async function loopData({ data, id, outputs }, { refData }) {
           return parseJSON(variableVal, variableVal);
         },
         elements: async () => {
-          const isXPath = data.elementSelector.startsWith('/');
           const elements = await this._sendMessageToTab({
             id,
             name: 'loop-data',
@@ -43,7 +42,7 @@ async function loopData({ data, id, outputs }, { refData }) {
               multiple: true,
               max: data.maxLoop,
               selector: data.elementSelector,
-              findBy: isXPath ? 'xpath' : 'cssSelector',
+              findBy: isXPath(data.elementSelector) ? 'xpath' : 'cssSelector',
             },
           });
 

+ 2 - 0
src/components/newtab/workflow/WorkflowBuilder.vue

@@ -271,6 +271,8 @@ export default {
         duplicateConnections(targetNode.inputs, 'inputs');
         duplicateConnections(targetNode.outputs, 'outputs');
 
+        emitter.emit('editor:data-changed');
+
         return;
       }
 

+ 2 - 1
src/content/blocksHandler/handlerPressKey.js

@@ -1,3 +1,4 @@
+import { isXPath } from '@/utils/helper';
 import { sendMessage } from '@/utils/message';
 import { keyDefinitions } from '@/utils/USKeyboardLayout';
 import { queryElements } from '../handleSelector';
@@ -126,7 +127,7 @@ async function pressKey({ data, debugMode, activeTabId }) {
   if (data.selector) {
     const customElement = await queryElements({
       selector: data.selector,
-      findBy: data.selector.startsWith('/') ? 'xpath' : 'cssSelector',
+      findBy: isXPath(data.selector) ? 'xpath' : 'cssSelector',
     });
 
     element = customElement || element;

+ 2 - 2
src/content/handleTestCondition.js

@@ -1,10 +1,10 @@
 import { nanoid } from 'nanoid/non-secure';
-import { visibleInViewport } from '@/utils/helper';
+import { visibleInViewport, isXPath } from '@/utils/helper';
 import FindElement from '@/utils/FindElement';
 import { automaRefDataStr } from './utils';
 
 function handleConditionElement({ data, type }) {
-  const selectorType = data.selector.startsWith('/') ? 'xpath' : 'cssSelector';
+  const selectorType = isXPath(data.selector) ? 'xpath' : 'cssSelector';
 
   const element = FindElement[selectorType](data);
   const { 1: actionType } = type.split('#');

+ 6 - 0
src/utils/helper.js

@@ -1,5 +1,11 @@
 import browser from 'webextension-polyfill';
 
+export function isXPath(str) {
+  const regex = /^[(/@]/;
+
+  return regex.test(str);
+}
+
 export function visibleInViewport(element) {
   const { top, left, bottom, right, height, width } =
     element.getBoundingClientRect();