@whitesev/utils 2.11.7 → 2.11.9

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.amd.js CHANGED
@@ -239,7 +239,7 @@ define((function () { 'use strict';
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.7";
242
+ const version = "2.11.9";
243
243
 
244
244
  /* eslint-disable */
245
245
  // ==UserScript==
@@ -6129,31 +6129,35 @@ define((function () { 'use strict';
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 @@ define((function () { 'use strict';
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 @@ define((function () { 'use strict';
6179
6183
  });
6180
6184
  }
6181
6185
  }
6182
- }
6186
+ };
6183
6187
  target.querySelectorAll("*").forEach(($elItem) => {
6184
6188
  queryMaxZIndex($elItem);
6185
6189
  });
@@ -6204,33 +6208,31 @@ define((function () { 'use strict';
6204
6208
  if (typeof deviation !== "number" || Number.isNaN(deviation)) {
6205
6209
  deviation = 10;
6206
6210
  }
6207
- const leftTop = {
6208
- x: globalThis.innerWidth * (1 / 8),
6209
- y: globalThis.innerHeight * (1 / 8),
6210
- };
6211
- const leftBottom = {
6212
- x: globalThis.innerWidth * (1 / 8),
6213
- y: globalThis.innerHeight * (7 / 8),
6214
- };
6215
- const rightTop = {
6216
- x: globalThis.innerWidth * (7 / 8),
6217
- y: globalThis.innerHeight * (1 / 8),
6218
- };
6219
- const rightBottom = {
6220
- x: globalThis.innerWidth * (7 / 8),
6221
- y: globalThis.innerHeight * (7 / 8),
6222
- };
6223
- const center = {
6224
- x: globalThis.innerWidth / 2,
6225
- y: globalThis.innerHeight / 2,
6226
- };
6227
- const delayHandlerElementPostionList = [
6228
- leftTop,
6229
- leftBottom,
6230
- rightTop,
6231
- rightBottom,
6232
- center,
6233
- ];
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;
6234
6236
  if ($el) {
6235
6237
  delayHandlerElementPostionList.length = 0;
6236
6238
  if (Array.isArray($el)) {
@@ -6242,61 +6244,79 @@ define((function () { 'use strict';
6242
6244
  }
6243
6245
  const positionInfoList = delayHandlerElementPostionList
6244
6246
  .map((position) => {
6245
- let positionNode;
6247
+ let $position;
6246
6248
  let positionX;
6247
6249
  let positionY;
6248
6250
  if (position instanceof HTMLElement) {
6249
- positionNode = position;
6251
+ $position = position;
6250
6252
  const nodeRect = position.getBoundingClientRect();
6251
6253
  positionX = nodeRect.x + nodeRect.width / 2;
6252
6254
  positionY = nodeRect.y + nodeRect.height / 2;
6253
6255
  }
6254
6256
  else {
6255
- positionNode = document.elementFromPoint(position.x, position.y);
6257
+ $position = document.elementFromPoint(position.x, position.y);
6256
6258
  positionX = position.x;
6257
6259
  positionY = position.y;
6258
6260
  }
6259
- const shadowRoot = positionNode?.shadowRoot;
6261
+ const shadowRoot = $position?.shadowRoot;
6260
6262
  if (shadowRoot) {
6261
- positionNode = shadowRoot.elementFromPoint(positionX, positionY);
6263
+ // 处理ShadowRoot
6264
+ $position = shadowRoot.elementFromPoint(positionX, positionY);
6262
6265
  }
6263
- if (positionNode instanceof HTMLStyleElement)
6264
- return;
6265
- if (positionNode instanceof HTMLScriptElement)
6266
- return;
6267
- if (positionNode instanceof HTMLMetaElement)
6268
- return;
6269
- if (positionNode instanceof HTMLHeadElement)
6266
+ if (!($position instanceof HTMLElement))
6270
6267
  return;
6271
- if (!(positionNode instanceof HTMLElement))
6272
- return;
6273
- let parent = positionNode;
6268
+ let $parent = $position;
6274
6269
  let zIndex = 0;
6275
- let maxZIndexNode = null;
6276
- while (parent) {
6277
- 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);
6278
6283
  const nodeZIndex = parseInt(nodeStyle.zIndex);
6279
6284
  if (nodeStyle.position !== "static" && !isNaN(nodeZIndex)) {
6280
6285
  if (nodeZIndex > zIndex) {
6281
6286
  zIndex = nodeZIndex;
6282
- maxZIndexNode = parent;
6287
+ $maxZIndexNode = $parent;
6283
6288
  }
6284
6289
  }
6285
- 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
+ };
6286
6304
  }
6287
6305
  return {
6288
- /** 处理了偏移量后的z-index值 */
6306
+ /** 计算偏移量后的z-index值 */
6289
6307
  zIndex: zIndex + deviation,
6290
- /** 原始z-index值 */
6308
+ /** 获取到的最大的z-index值 */
6291
6309
  originZIndex: zIndex,
6292
6310
  /** 拥有最大z-index的元素 */
6293
- node: maxZIndexNode,
6311
+ node: $maxZIndexNode,
6294
6312
  /** 目标坐标元素 */
6295
- positionNode: positionNode,
6296
- /** x坐标 */
6313
+ positionNode: $position,
6314
+ /** 目标x坐标 */
6297
6315
  positionX: positionX,
6298
- /** y坐标 */
6316
+ /** 目标y坐标 */
6299
6317
  positionY: positionY,
6318
+ /** node位置信息 */
6319
+ rect,
6300
6320
  };
6301
6321
  })
6302
6322
  .filter((it) => it != null);
@@ -8321,6 +8341,20 @@ define((function () { 'use strict';
8321
8341
  * 深度获取对象的某个属性
8322
8342
  * @param target 待获取的对象
8323
8343
  * @param handler 获取属性的回调
8344
+ * @example
8345
+ * Utils.queryProperty(window,(target)=>{
8346
+ * if(target.xxx){
8347
+ * return {
8348
+ * isFind: true,
8349
+ * data: target.xxx,
8350
+ * }
8351
+ * }else{
8352
+ * return {
8353
+ * isFind: false,
8354
+ * data: target.aabbcc,
8355
+ * }
8356
+ * }
8357
+ * })
8324
8358
  */
8325
8359
  queryProperty(target, handler) {
8326
8360
  if (target == null) {
@@ -8336,10 +8370,23 @@ define((function () { 'use strict';
8336
8370
  * 异步-深度获取对象属性
8337
8371
  * @param target 待获取的对象
8338
8372
  * @param handler 获取属性的回调
8373
+ * @example
8374
+ * Utils.asyncQueryProperty(window, async (target)=>{
8375
+ * if(target.xxx){
8376
+ * return {
8377
+ * isFind: true,
8378
+ * data: target.xxx,
8379
+ * }
8380
+ * }else{
8381
+ * return {
8382
+ * isFind: false,
8383
+ * data: target.aabbcc,
8384
+ * }
8385
+ * }
8386
+ * })
8339
8387
  */
8340
8388
  async asyncQueryProperty(target, handler) {
8341
8389
  if (target == null) {
8342
- // @ts-expect-error 空返回
8343
8390
  return;
8344
8391
  }
8345
8392
  const handleResult = await handler(target);
@@ -8351,7 +8398,8 @@ define((function () { 'use strict';
8351
8398
  /**
8352
8399
  * 创建一个新的Utils实例
8353
8400
  * @param option
8354
- * @returns
8401
+ * @example
8402
+ * Utils.createUtils();
8355
8403
  */
8356
8404
  createUtils(option) {
8357
8405
  return new Utils(option);
@@ -8419,7 +8467,9 @@ define((function () { 'use strict';
8419
8467
  /**
8420
8468
  * 覆盖对象中的函数this指向
8421
8469
  * @param target 需要覆盖的对象
8422
- * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
8470
+ * @param objectThis 覆盖的this指向,如果为传入,则默认为对象本身
8471
+ * @example
8472
+ * Utils.coverObjectFunctionThis({})
8423
8473
  */
8424
8474
  coverObjectFunctionThis = commonUtil.coverObjectFunctionThis.bind(commonUtil);
8425
8475
  /**
@@ -8470,7 +8520,7 @@ define((function () { 'use strict';
8470
8520
  /**
8471
8521
  * 自动使用 Worker 执行 setTimeout
8472
8522
  * @param callback 回调函数
8473
- * @param [timeout=0] 延迟时间,默认为0
8523
+ * @param timeout 延迟时间,默认为0
8474
8524
  */
8475
8525
  workerSetTimeout(callback, timeout = 0) {
8476
8526
  try {
@@ -8541,6 +8591,10 @@ define((function () { 'use strict';
8541
8591
  /**
8542
8592
  * 判断页面中是否存在`worker-src`的CSP规则
8543
8593
  * @param timeout 超时时间,默认为`1500ms`
8594
+ * @example
8595
+ * Utils.hasWorkerCSP().then((hasCSP) => {
8596
+ * console.log(hasCSP);
8597
+ * })
8544
8598
  */
8545
8599
  hasWorkerCSP(timeout = 1500) {
8546
8600
  return new Promise((resolve) => {