@onekeyfe/inpage-providers-hub 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findTextByImg = exports.findIconAndNameDirectly = exports.findIconAndNameByParent = void 0;
3
+ exports.findTextByImg = exports.findIconAndNameByIcon = exports.findIconAndNameByName = void 0;
4
4
  const consts_1 = require("./consts");
5
5
  const imgUtils_1 = require("./imgUtils");
6
6
  const textUtils_1 = require("./textUtils");
@@ -11,9 +11,9 @@ const utils_1 = require("./utils");
11
11
  * don't document to querySelector because it maybe not work in shadowRoot,
12
12
  * instead of it, use the containerElement
13
13
  */
14
- function findIconAndNameByParent(containerElement, walletName, constraints = {
14
+ function findIconAndNameByName(containerElement, walletName, icon = 'auto-search-icon', constraints = {
15
15
  text: [utils_1.isClickable],
16
- icon: [imgUtils_1.isWalletIconSizeMatch, utils_1.isClickable],
16
+ icon: [imgUtils_1.isWalletIconLessEqualThan, utils_1.isClickable],
17
17
  }) {
18
18
  const textNode = (0, textUtils_1.findWalletTextByParent)(containerElement, walletName, constraints.text);
19
19
  if (!textNode || !textNode.parentElement) {
@@ -24,18 +24,26 @@ function findIconAndNameByParent(containerElement, walletName, constraints = {
24
24
  utils_1.universalLog.log(`${walletName.toString()} is in external link`);
25
25
  return null;
26
26
  }
27
- let parent = textNode.parentElement;
28
27
  let iconNode = undefined;
29
- let level = 0;
30
- while (parent && parent !== (containerElement === null || containerElement === void 0 ? void 0 : containerElement.parentElement) && level++ < consts_1.MAX_LEVELS) {
31
- const walletIcon = (0, imgUtils_1.findWalletIconByParent)(parent, constraints.icon);
32
- //TODO: unnecessary to traverse the parent node if the icon have more than one
33
- if (!walletIcon) {
34
- parent = parent.parentElement;
35
- continue;
28
+ if (typeof icon === 'function') {
29
+ iconNode = icon(textNode);
30
+ }
31
+ else if (icon === 'auto-search-icon') {
32
+ let parent = textNode.parentElement;
33
+ let level = 0;
34
+ while (parent && parent !== (containerElement === null || containerElement === void 0 ? void 0 : containerElement.parentElement) && level++ < consts_1.MAX_LEVELS) {
35
+ const walletIcon = (0, imgUtils_1.findWalletIconByParent)(parent, constraints.icon);
36
+ if (!walletIcon) {
37
+ parent = parent.parentElement;
38
+ continue;
39
+ }
40
+ iconNode = walletIcon;
41
+ break;
36
42
  }
37
- iconNode = walletIcon;
38
- break;
43
+ }
44
+ else {
45
+ utils_1.universalLog.warn('icon paramter should be a function or auto-search-icon');
46
+ return null;
39
47
  }
40
48
  if (!iconNode) {
41
49
  utils_1.universalLog.log(`no wallet ${walletName.toString()} icon node found`);
@@ -44,9 +52,8 @@ function findIconAndNameByParent(containerElement, walletName, constraints = {
44
52
  // make sure the icon and text are both existed
45
53
  return { iconNode, textNode };
46
54
  }
47
- exports.findIconAndNameByParent = findIconAndNameByParent;
48
- function findIconAndNameDirectly(iconSelector, textSelector, name, container = document, constraints = { text: [], icon: [] }, searchLevel = consts_1.MAX_SEARCH_LEVELS_By_IMG) {
49
- var _a;
55
+ exports.findIconAndNameByName = findIconAndNameByName;
56
+ function findIconAndNameByIcon(iconSelector, textSelector, name, container = document, constraints = { text: [], icon: [] }, searchLevel = consts_1.MAX_SEARCH_LEVELS_By_IMG) {
50
57
  const iconElements = typeof iconSelector === 'string'
51
58
  ? container.querySelectorAll(iconSelector)
52
59
  : (0, utils_1.arrayify)(iconSelector());
@@ -55,6 +62,7 @@ function findIconAndNameDirectly(iconSelector, textSelector, name, container = d
55
62
  return null;
56
63
  }
57
64
  const iconElement = Array.from(iconElements)[0];
65
+ //find the text node by img
58
66
  let textNode = null;
59
67
  if (textSelector === 'auto-search-text') {
60
68
  const containerEle = container instanceof HTMLElement ? container : document.body;
@@ -62,14 +70,6 @@ function findIconAndNameDirectly(iconSelector, textSelector, name, container = d
62
70
  ? findTextByImg(iconElement, name, containerEle, constraints.text, searchLevel)
63
71
  : null;
64
72
  }
65
- else if (typeof textSelector === 'string') {
66
- const textContainer = (_a = Array.from(container.querySelectorAll(textSelector))) === null || _a === void 0 ? void 0 : _a.filter(Boolean);
67
- if ((textContainer === null || textContainer === void 0 ? void 0 : textContainer.length) > 1) {
68
- utils_1.universalLog.warn('more one wallet text found ,please check the selector');
69
- return null;
70
- }
71
- textNode = (0, textUtils_1.findWalletTextByParent)(textContainer[0], name, constraints.text);
72
- }
73
73
  else if (typeof textSelector === 'function') {
74
74
  const containerEle = iconElement && textSelector(iconElement);
75
75
  textNode =
@@ -90,12 +90,12 @@ function findIconAndNameDirectly(iconSelector, textSelector, name, container = d
90
90
  textNode,
91
91
  };
92
92
  }
93
- exports.findIconAndNameDirectly = findIconAndNameDirectly;
93
+ exports.findIconAndNameByIcon = findIconAndNameByIcon;
94
94
  function findTextByImg(img, walletName, containerLimit, constraints, maxLevel = consts_1.MAX_SEARCH_LEVELS_By_IMG) {
95
95
  let text = null;
96
96
  let parent = img;
97
97
  let level = 0;
98
- while (parent && parent != containerLimit && level++ < maxLevel) {
98
+ while (parent && parent != containerLimit.parentElement && level++ < maxLevel) {
99
99
  text = (0, textUtils_1.findWalletTextByParent)(parent, walletName, constraints);
100
100
  if (text) {
101
101
  return text;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isWalletIconSizeMatch = exports.findWalletIconByParent = exports.findIconNodesByParent = exports.createImageEle = exports.replaceIcon = void 0;
3
+ exports.isWalletIconLessEqualThan = exports.isWalletIconSizeMatch = exports.findWalletIconByParent = exports.findIconNodesByParent = exports.createImageEle = exports.replaceIcon = void 0;
4
4
  const consts_1 = require("./consts");
5
5
  const utils_1 = require("./utils");
6
6
  /**
@@ -14,9 +14,6 @@ function replaceIcon(originalNode, newIconSrc) {
14
14
  if (originalNode instanceof HTMLImageElement) {
15
15
  originalNode.src = newIconSrc;
16
16
  originalNode.removeAttribute('srcset');
17
- originalNode.style.width = width;
18
- originalNode.style.height = height;
19
- originalNode.classList.add(...Array.from(originalNode.classList));
20
17
  return originalNode;
21
18
  }
22
19
  else {
@@ -60,29 +57,29 @@ exports.findIconNodesByParent = findIconNodesByParent;
60
57
  function findWalletIconByParent(parent, constraints) {
61
58
  const iconNodes = findIconNodesByParent(parent);
62
59
  if (iconNodes.length === 0) {
63
- utils_1.universalLog.warn(`no icon node found`, parent);
60
+ utils_1.universalLog.warn(`no icon node found for parent`, parent);
64
61
  return null;
65
62
  }
66
63
  if (iconNodes.length > 1) {
67
64
  utils_1.universalLog.warn(`more than one icon node found`, iconNodes.length, iconNodes);
68
- return null;
65
+ throw new Error('more than one icon node found');
69
66
  }
70
67
  const icon = iconNodes[0];
71
68
  if (constraints.some((f) => !f(icon))) {
72
- utils_1.universalLog.warn('it doesnt satisfy the constraints');
73
- return null;
69
+ throw new Error('it doesnt satisfy the constraints');
74
70
  }
75
71
  return icon;
76
72
  }
77
73
  exports.findWalletIconByParent = findWalletIconByParent;
78
- //TODO: deal with lazy loading image
79
- function isWalletIconSizeMatch(walletIcon) {
74
+ //NOTE: use function isWalletIconLessEqualThan with lazy loading image
75
+ function isWalletIconSizeMatch(walletIcon, min = consts_1.ICON_MIN_SIZE, max = consts_1.ICON_MAX_SIZE) {
80
76
  const { width, height } = walletIcon.getBoundingClientRect();
81
- const isMatch = width < consts_1.ICON_MAX_SIZE &&
82
- width > consts_1.ICON_MIN_SIZE &&
83
- height < consts_1.ICON_MAX_SIZE &&
84
- height > consts_1.ICON_MIN_SIZE;
77
+ const isMatch = width <= max && width >= min && height <= max && height >= min;
85
78
  !isMatch && utils_1.universalLog.log('wallet icon size doesnot match: ', width, height);
86
79
  return isMatch;
87
80
  }
88
81
  exports.isWalletIconSizeMatch = isWalletIconSizeMatch;
82
+ function isWalletIconLessEqualThan(walletIcon) {
83
+ return isWalletIconSizeMatch(walletIcon, 0, consts_1.ICON_MAX_SIZE);
84
+ }
85
+ exports.isWalletIconLessEqualThan = isWalletIconLessEqualThan;
@@ -49,7 +49,7 @@ function hackWalletConnectButton(sites) {
49
49
  utils_1.universalLog.warn('containerElement is null, container=', container);
50
50
  continue;
51
51
  }
52
- result = (0, findIconAndName_1.findIconAndNameByParent)(containerElement, name, constraintMap);
52
+ result = (0, findIconAndName_1.findIconAndNameByName)(containerElement, name, 'auto-search-icon', constraintMap);
53
53
  }
54
54
  if (!result) {
55
55
  utils_1.universalLog.warn('no result found');
@@ -6,7 +6,7 @@ const utils_1 = require("./utils");
6
6
  const imgUtils_1 = require("./imgUtils");
7
7
  function findIconAndNameInShadowRoot(hostSelector, containerSelector, walletName, constraints = {
8
8
  text: [utils_1.isClickable],
9
- icon: [imgUtils_1.isWalletIconSizeMatch, utils_1.isClickable],
9
+ icon: [imgUtils_1.isWalletIconLessEqualThan, utils_1.isClickable],
10
10
  }) {
11
11
  const shadowRoots = Array.from(document.querySelectorAll(hostSelector))
12
12
  .filter(Boolean)
@@ -24,6 +24,6 @@ function findIconAndNameInShadowRoot(hostSelector, containerSelector, walletName
24
24
  utils_1.universalLog.warn('findIconAndNameInShadowRoot,length=', length);
25
25
  return null;
26
26
  }
27
- return (0, findIconAndName_1.findIconAndNameByParent)(containerElements[0], walletName, constraints);
27
+ return (0, findIconAndName_1.findIconAndNameByName)(containerElements[0], walletName, 'auto-search-icon', constraints);
28
28
  }
29
29
  exports.findIconAndNameInShadowRoot = findIconAndNameInShadowRoot;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.findWalletTextByParent = exports.replaceText = exports.makeTextWrap = exports.makeTextEllipse = void 0;
6
+ exports.findWalletTextByParent = exports.replaceText = exports.makeTextAlignCenter = exports.makeTextAlignLeft = exports.makeTextWrap = exports.makeTextEllipse = void 0;
7
7
  const utilsDomNodes_1 = __importDefault(require("../utils/utilsDomNodes"));
8
8
  const utils_1 = require("./utils");
9
9
  function makeTextEllipse(textNode, option = {}) {
@@ -17,6 +17,14 @@ function makeTextWrap(textNode) {
17
17
  textNode.style.whiteSpace = 'normal';
18
18
  }
19
19
  exports.makeTextWrap = makeTextWrap;
20
+ function makeTextAlignLeft(textNode) {
21
+ textNode.style.textAlign = 'left';
22
+ }
23
+ exports.makeTextAlignLeft = makeTextAlignLeft;
24
+ function makeTextAlignCenter(textNode) {
25
+ textNode.style.textAlign = 'center';
26
+ }
27
+ exports.makeTextAlignCenter = makeTextAlignCenter;
20
28
  function replaceText(textNode, newText) {
21
29
  const newTextNode = document.createTextNode(newText);
22
30
  textNode.replaceWith(newTextNode);