1
0
Ahmad Kholid 3 жил өмнө
parent
commit
142135946a

+ 1 - 1
package.json

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

+ 1 - 1
src/background/index.js

@@ -296,7 +296,7 @@ browser.alarms.onAlarm.addListener(async ({ name }) => {
   workflow.execute(currentWorkflow);
 
   if (data && data.type === 'specific-day') {
-    registerSpecificDay(currentWorkflow.id, triggerBlock.data);
+    registerSpecificDay(currentWorkflow.id, data);
   }
 });
 

+ 29 - 25
src/content/element-selector/App.vue

@@ -131,7 +131,9 @@
       class="h-full w-full absolute top-0 pointer-events-none left-0 z-10"
     >
       <rect
-        v-bind="hoverElementRect"
+        v-for="(item, index) in state.hoveredElements"
+        v-bind="item"
+        :key="index"
         stroke-width="2"
         stroke="#fbbf24"
         fill="rgba(251, 191, 36, 0.2)"
@@ -176,15 +178,10 @@ const state = reactive({
   isExecuting: false,
   selectElements: [],
   selectorType: 'css',
+  hoveredElements: [],
   selectedElements: [],
   hide: window.self !== window.top,
 });
-const hoverElementRect = reactive({
-  x: 0,
-  y: 0,
-  height: 0,
-  width: 0,
-});
 const cardRect = reactive({
   x: 0,
   y: 0,
@@ -193,11 +190,21 @@ const cardRect = reactive({
 });
 
 /* eslint-disable  no-use-before-define */
-const getElementSelector = (element) =>
+const getElementSelector = (element, options = {}) =>
   state.selectorType === 'css'
     ? getCssSelector(element, {
+        root: document.body,
+        blacklist: [
+          '[focused]',
+          /focus/,
+          '[data-*]',
+          '[href=*]',
+          '[src=*]',
+          '[value=*]',
+        ],
+        selectors: ['id', 'class', 'tag', 'attribute'],
         includeTag: true,
-        blacklist: ['[focused]', /focus/, /href/, /src/],
+        ...options,
       })
     : generateXPath(element);
 
@@ -302,10 +309,10 @@ function handleMouseMove({ clientX, clientY, target }) {
 
   if (state.hide || rootElement === target) return;
 
-  Object.assign(hoverElementRect, getElementRect(target));
+  state.hoveredElements = [getElementRect(target)];
 }
 function handleClick(event) {
-  const { target, path, ctrlKey, shiftKey } = event;
+  const { target, path, ctrlKey } = event;
 
   if (target === rootElement || state.hide || state.isExecuting) return;
 
@@ -325,8 +332,9 @@ function handleClick(event) {
     highlight: false,
   };
 
-  if ((state.selectorType === 'css' && ctrlKey) || shiftKey) {
+  if (state.selectorType === 'css' && ctrlKey) {
     let elementIndex = -1;
+
     const elements = state.selectedElements.map(({ element }, index) => {
       if (element === targetElement) {
         elementIndex = index;
@@ -397,14 +405,15 @@ const handleScroll = debounce(() => {
 
   const yPos = window.scrollY - lastScrollPosY;
   const xPos = window.scrollX - lastScrollPosX;
+  const updateState = (key) => {
+    state[key].forEach((_, index) => {
+      state[key][index].x -= xPos;
+      state[key][index].y -= yPos;
+    });
+  };
 
-  state.selectedElements.forEach((_, index) => {
-    state.selectedElements[index].x -= xPos;
-    state.selectedElements[index].y -= yPos;
-  });
-
-  hoverElementRect.x -= xPos;
-  hoverElementRect.y -= yPos;
+  updateState('hoveredElements');
+  updateState('selectedElements');
 
   lastScrollPosX = window.scrollX;
   lastScrollPosY = window.scrollY;
@@ -418,14 +427,9 @@ function destroy() {
     elSelector: '',
     isDragging: false,
     isExecuting: false,
+    hoveredElements: [],
     selectedElements: [],
   });
-  Object.assign(hoverElementRect, {
-    x: 0,
-    y: 0,
-    height: 0,
-    width: 0,
-  });
 
   document.documentElement.style.fontSize = originalFontSize;
 }

+ 20 - 16
src/content/index.js

@@ -1,27 +1,16 @@
 import browser from 'webextension-polyfill';
 import { nanoid } from 'nanoid';
 import { toCamelCase } from '@/utils/helper';
+import FindElement from '@/utils/find-element';
 import executedBlock from './executed-block';
 import blocksHandler from './blocks-handler';
 
-const elementActions = {
-  text: (element) => element.innerText,
-  visible: (element) => {
-    const { visibility, display } = getComputedStyle(element);
-
-    return visibility !== 'hidden' || display !== 'none';
-  },
-  invisible: (element) => !elementActions.visible(element),
-  attribute: (element, { attrName }) => {
-    if (!element.hasAttribute(attrName)) return null;
-
-    return element.getAttribute(attrName);
-  },
-};
 function handleConditionBuilder({ data, type }) {
   if (!type.startsWith('element')) return null;
 
-  const element = document.querySelector(data.selector);
+  const selectorType = data.selector.startsWith('/') ? 'xpath' : 'cssSelector';
+
+  const element = FindElement[selectorType](data);
   const { 1: actionType } = type.split('#');
 
   if (!element) {
@@ -30,7 +19,22 @@ function handleConditionBuilder({ data, type }) {
     return null;
   }
 
-  return elementActions[actionType](element, data);
+  const elementActions = {
+    text: () => element.innerText,
+    visible: () => {
+      const { visibility, display } = getComputedStyle(element);
+
+      return visibility !== 'hidden' || display !== 'none';
+    },
+    invisible: () => !elementActions.visible(element),
+    attribute: ({ attrName }) => {
+      if (!element.hasAttribute(attrName)) return null;
+
+      return element.getAttribute(attrName);
+    },
+  };
+
+  return elementActions[actionType](data);
 }
 
 (() => {

+ 1 - 1
src/locales/zh/newtab.json

@@ -180,7 +180,7 @@
       },
       "restartWorkflow": {
         "for": "重新启动",
-        "times": "次数"
+        "times": "次数",
         "description": "工作流可以重启的最大次数"
       },
       "onError": {

+ 1 - 0
src/newtab/pages/workflows/[id].vue

@@ -759,6 +759,7 @@ function editBlock(data) {
   if (workflowData.active === 'shared') return;
 
   state.isEditBlock = true;
+  state.showSidebar = true;
   state.blockData = defu(data, tasks[data.id] || {});
 }
 function handleEditorDataChanged() {

+ 1 - 2
src/utils/shared.js

@@ -1,4 +1,3 @@
-/* to-do execute multiple blocks simultaneously, keyboard shortcut */
 import { nanoid } from 'nanoid';
 
 export const tasks = {
@@ -994,7 +993,7 @@ export const conditionBuilder = {
   inputTypes: {
     selector: {
       placeholder: '.class',
-      label: 'CSS selector',
+      label: 'CSS selector or XPath',
     },
     value: {
       label: 'Value',