@whitesev/utils 2.11.6 → 2.11.8

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.
package/dist/index.cjs.js CHANGED
@@ -239,7 +239,7 @@ const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
239
239
  const setInterval$1 = (...args) => loadOrReturnBroker().setInterval(...args);
240
240
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
241
241
 
242
- const version = "2.11.6";
242
+ const version = "2.11.8";
243
243
 
244
244
  /* eslint-disable */
245
245
  // ==UserScript==
@@ -6129,31 +6129,35 @@ class Utils {
6129
6129
  * 元素是否可见
6130
6130
  * @param $el
6131
6131
  * @param $css
6132
+ * @returns
6133
+ * + true 可见
6134
+ * + false 不可见
6132
6135
  */
6133
- function isVisibleNode($el, $css) {
6136
+ const isVisibleNode = function ($css, $el) {
6134
6137
  let flag = true;
6135
- if (typeof $el.checkVisibility === "function") {
6136
- flag = $el.checkVisibility();
6138
+ if (!($el instanceof HTMLElement)) {
6139
+ flag = false;
6137
6140
  }
6138
6141
  else {
6139
- flag =
6140
- $css.position !== "static" && $css.display !== "none" && $css.visibility !== "hidden" && $css.opacity !== "0";
6141
- }
6142
- if (flag) {
6143
- // css样式上可见
6144
- // 再判断宽高
6145
- const rect = $el.getBoundingClientRect();
6146
- // 确保该元素的中心点在屏幕内
6147
- flag = rect.width > 0 && rect.height > 0 && rect.x > 0 && rect.y > 0;
6142
+ if (typeof $el.checkVisibility === "function") {
6143
+ flag = $el.checkVisibility();
6144
+ }
6145
+ else {
6146
+ flag =
6147
+ $css.position !== "static" &&
6148
+ $css.display !== "none" &&
6149
+ $css.visibility !== "hidden" &&
6150
+ $css.opacity !== "0";
6151
+ }
6148
6152
  }
6149
6153
  return flag;
6150
- }
6154
+ };
6151
6155
  /**
6152
6156
  * 查询元素的z-index
6153
6157
  * 并比较值是否是已获取的最大值
6154
6158
  * @param $el
6155
6159
  */
6156
- function queryMaxZIndex($el) {
6160
+ const queryMaxZIndex = function ($el) {
6157
6161
  if (typeof ignoreCallBack === "function") {
6158
6162
  const ignoreResult = ignoreCallBack($el);
6159
6163
  if (typeof ignoreResult === "boolean" && !ignoreResult) {
@@ -6161,9 +6165,9 @@ class Utils {
6161
6165
  }
6162
6166
  }
6163
6167
  /** 元素的样式 */
6164
- const nodeStyle = that.windowApi.window.getComputedStyle($el);
6168
+ const nodeStyle = that.windowApi.globalThis.getComputedStyle($el);
6165
6169
  /* 不对position为static和display为none的元素进行获取它们的z-index */
6166
- if (isVisibleNode($el, nodeStyle)) {
6170
+ if (isVisibleNode(nodeStyle, $el)) {
6167
6171
  const nodeZIndex = parseInt(nodeStyle.zIndex);
6168
6172
  if (!isNaN(nodeZIndex)) {
6169
6173
  if (nodeZIndex > zIndex) {
@@ -6179,7 +6183,7 @@ class Utils {
6179
6183
  });
6180
6184
  }
6181
6185
  }
6182
- }
6186
+ };
6183
6187
  target.querySelectorAll("*").forEach(($elItem) => {
6184
6188
  queryMaxZIndex($elItem);
6185
6189
  });
@@ -6194,6 +6198,9 @@ class Utils {
6194
6198
  };
6195
6199
  }
6196
6200
  getMaxZIndexNodeInfoFromPoint($el, deviation) {
6201
+ if (typeof $el === "function") {
6202
+ $el = $el();
6203
+ }
6197
6204
  if (typeof $el === "number") {
6198
6205
  deviation = $el;
6199
6206
  $el = void 0;
@@ -6201,33 +6208,31 @@ class Utils {
6201
6208
  if (typeof deviation !== "number" || Number.isNaN(deviation)) {
6202
6209
  deviation = 10;
6203
6210
  }
6204
- const leftTop = {
6205
- x: globalThis.innerWidth * (1 / 8),
6206
- y: globalThis.innerHeight * (1 / 8),
6207
- };
6208
- const leftBottom = {
6209
- x: globalThis.innerWidth * (1 / 8),
6210
- y: globalThis.innerHeight * (7 / 8),
6211
- };
6212
- const rightTop = {
6213
- x: globalThis.innerWidth * (7 / 8),
6214
- y: globalThis.innerHeight * (1 / 8),
6215
- };
6216
- const rightBottom = {
6217
- x: globalThis.innerWidth * (7 / 8),
6218
- y: globalThis.innerHeight * (7 / 8),
6219
- };
6220
- const center = {
6221
- x: globalThis.innerWidth / 2,
6222
- y: globalThis.innerHeight / 2,
6223
- };
6224
- const delayHandlerElementPostionList = [
6225
- leftTop,
6226
- leftBottom,
6227
- rightTop,
6228
- rightBottom,
6229
- center,
6230
- ];
6211
+ /** 坐标偏移 */
6212
+ const positionDistance = 10;
6213
+ const defaultCalcPostion = [];
6214
+ const maxPostionX = globalThis.innerWidth;
6215
+ const maxPostionY = globalThis.innerHeight;
6216
+ const gridXCount = 8;
6217
+ const gridYCount = 8;
6218
+ for (let i = 0; i < gridXCount; i++) {
6219
+ for (let j = 0; j < gridYCount; j++) {
6220
+ const positionX = globalThis.innerWidth * (i / gridXCount) + positionDistance;
6221
+ const positionY = globalThis.innerHeight * (j / gridYCount) + positionDistance;
6222
+ const position = {
6223
+ x: positionX,
6224
+ y: positionY,
6225
+ };
6226
+ if (position.x > maxPostionX) {
6227
+ position.x = maxPostionX;
6228
+ }
6229
+ if (position.y > maxPostionY) {
6230
+ position.y = maxPostionY;
6231
+ }
6232
+ defaultCalcPostion.push(position);
6233
+ }
6234
+ }
6235
+ const delayHandlerElementPostionList = defaultCalcPostion;
6231
6236
  if ($el) {
6232
6237
  delayHandlerElementPostionList.length = 0;
6233
6238
  if (Array.isArray($el)) {
@@ -6239,61 +6244,79 @@ class Utils {
6239
6244
  }
6240
6245
  const positionInfoList = delayHandlerElementPostionList
6241
6246
  .map((position) => {
6242
- let positionNode;
6247
+ let $position;
6243
6248
  let positionX;
6244
6249
  let positionY;
6245
6250
  if (position instanceof HTMLElement) {
6246
- positionNode = position;
6251
+ $position = position;
6247
6252
  const nodeRect = position.getBoundingClientRect();
6248
6253
  positionX = nodeRect.x + nodeRect.width / 2;
6249
6254
  positionY = nodeRect.y + nodeRect.height / 2;
6250
6255
  }
6251
6256
  else {
6252
- positionNode = document.elementFromPoint(position.x, position.y);
6257
+ $position = document.elementFromPoint(position.x, position.y);
6253
6258
  positionX = position.x;
6254
6259
  positionY = position.y;
6255
6260
  }
6256
- const shadowRoot = positionNode?.shadowRoot;
6261
+ const shadowRoot = $position?.shadowRoot;
6257
6262
  if (shadowRoot) {
6258
- positionNode = shadowRoot.elementFromPoint(positionX, positionY);
6263
+ // 处理ShadowRoot
6264
+ $position = shadowRoot.elementFromPoint(positionX, positionY);
6259
6265
  }
6260
- if (positionNode instanceof HTMLStyleElement)
6266
+ if (!($position instanceof HTMLElement))
6261
6267
  return;
6262
- if (positionNode instanceof HTMLScriptElement)
6263
- return;
6264
- if (positionNode instanceof HTMLMetaElement)
6265
- return;
6266
- if (positionNode instanceof HTMLHeadElement)
6267
- return;
6268
- if (!(positionNode instanceof HTMLElement))
6269
- return;
6270
- let parent = positionNode;
6268
+ let $parent = $position;
6271
6269
  let zIndex = 0;
6272
- let maxZIndexNode = null;
6273
- while (parent) {
6274
- const nodeStyle = globalThis.getComputedStyle(parent);
6270
+ let $maxZIndexNode = null;
6271
+ let rect = {
6272
+ x: 0,
6273
+ y: 0,
6274
+ width: 0,
6275
+ height: 0,
6276
+ top: 0,
6277
+ right: 0,
6278
+ bottom: 0,
6279
+ left: 0,
6280
+ };
6281
+ while ($parent) {
6282
+ const nodeStyle = globalThis.getComputedStyle($parent);
6275
6283
  const nodeZIndex = parseInt(nodeStyle.zIndex);
6276
6284
  if (nodeStyle.position !== "static" && !isNaN(nodeZIndex)) {
6277
6285
  if (nodeZIndex > zIndex) {
6278
6286
  zIndex = nodeZIndex;
6279
- maxZIndexNode = parent;
6287
+ $maxZIndexNode = $parent;
6280
6288
  }
6281
6289
  }
6282
- parent = parent.parentElement;
6290
+ $parent = $parent.parentElement;
6291
+ }
6292
+ if ($maxZIndexNode) {
6293
+ const maxRect = $maxZIndexNode.getBoundingClientRect();
6294
+ rect = {
6295
+ x: maxRect.x,
6296
+ y: maxRect.y,
6297
+ width: maxRect.width,
6298
+ height: maxRect.height,
6299
+ top: maxRect.top,
6300
+ right: maxRect.right,
6301
+ bottom: maxRect.bottom,
6302
+ left: maxRect.left,
6303
+ };
6283
6304
  }
6284
6305
  return {
6285
- /** 处理了偏移量后的z-index值 */
6306
+ /** 计算偏移量后的z-index值 */
6286
6307
  zIndex: zIndex + deviation,
6287
- /** 原始z-index值 */
6308
+ /** 获取到的最大的z-index值 */
6288
6309
  originZIndex: zIndex,
6289
6310
  /** 拥有最大z-index的元素 */
6290
- node: maxZIndexNode,
6311
+ node: $maxZIndexNode,
6291
6312
  /** 目标坐标元素 */
6292
- positionNode: positionNode,
6293
- /** x坐标 */
6313
+ positionNode: $position,
6314
+ /** 目标x坐标 */
6294
6315
  positionX: positionX,
6295
- /** y坐标 */
6316
+ /** 目标y坐标 */
6296
6317
  positionY: positionY,
6318
+ /** node位置信息 */
6319
+ rect,
6297
6320
  };
6298
6321
  })
6299
6322
  .filter((it) => it != null);