Browse Source

feat: add `element exists` in condition builder

Ahmad Kholid 3 years ago
parent
commit
bef94d8da7
2 changed files with 14 additions and 8 deletions
  1. 7 8
      src/content/handleTestCondition.js
  2. 7 0
      src/utils/shared.js

+ 7 - 8
src/content/handleTestCondition.js

@@ -8,26 +8,25 @@ function handleConditionElement({ data, type }) {
   const element = FindElement[selectorType](data);
   const element = FindElement[selectorType](data);
   const { 1: actionType } = type.split('#');
   const { 1: actionType } = type.split('#');
 
 
-  if (!element) {
-    if (actionType === 'visible' || actionType === 'invisible') return false;
-
-    return null;
-  }
-
   const elementActions = {
   const elementActions = {
-    text: () => element.innerText,
+    exists: () => Boolean(element),
+    text: () => element?.innerText ?? null,
     visible: () => {
     visible: () => {
+      if (!element) return false;
+
       const { visibility, display } = getComputedStyle(element);
       const { visibility, display } = getComputedStyle(element);
 
 
       return visibility !== 'hidden' && display !== 'none';
       return visibility !== 'hidden' && display !== 'none';
     },
     },
     invisible: () => {
     invisible: () => {
+      if (!element) return false;
+
       const { visibility, display } = getComputedStyle(element);
       const { visibility, display } = getComputedStyle(element);
 
 
       return visibility === 'hidden' || display === 'none';
       return visibility === 'hidden' || display === 'none';
     },
     },
     attribute: ({ attrName }) => {
     attribute: ({ attrName }) => {
-      if (!element.hasAttribute(attrName)) return null;
+      if (!element || !element.hasAttribute(attrName)) return null;
 
 
       return element.getAttribute(attrName);
       return element.getAttribute(attrName);
     },
     },

+ 7 - 0
src/utils/shared.js

@@ -1078,6 +1078,13 @@ export const conditionBuilder = {
       compareable: true,
       compareable: true,
       data: { selector: '' },
       data: { selector: '' },
     },
     },
+    {
+      id: 'element#exists',
+      category: 'element',
+      name: 'Element exists',
+      compareable: false,
+      data: { selector: '' },
+    },
     {
     {
       id: 'element#visible',
       id: 'element#visible',
       category: 'element',
       category: 'element',