@whitesev/utils 1.9.4 → 1.9.5

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.esm.js CHANGED
@@ -270,7 +270,7 @@ class UtilsGMCookie {
270
270
  if (typeof cookieName !== "string") {
271
271
  throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
272
272
  }
273
- let cookies = document.cookie.split(";");
273
+ let cookies = UtilsCore.document.cookie.split(";");
274
274
  let findValue = void 0;
275
275
  for (const cookieItem of cookies) {
276
276
  let item = cookieItem.trim();
@@ -280,7 +280,7 @@ class UtilsGMCookie {
280
280
  let itemValue = decodeURIComponent(itemSplit.join(""));
281
281
  if (itemName === cookieName) {
282
282
  findValue = {
283
- domain: globalThis.location.hostname,
283
+ domain: UtilsCore.globalThis.location.hostname,
284
284
  expirationDate: null,
285
285
  hostOnly: true,
286
286
  httpOnly: false,
@@ -310,13 +310,13 @@ class UtilsGMCookie {
310
310
  let resultData = [];
311
311
  try {
312
312
  let details = {
313
- url: globalThis.location.href,
314
- domain: globalThis.location.hostname,
313
+ url: UtilsCore.globalThis.location.href,
314
+ domain: UtilsCore.globalThis.location.hostname,
315
315
  name: "",
316
316
  path: "/",
317
317
  };
318
318
  details = utils.assign(details, paramDetails);
319
- let cookies = document.cookie.split(";");
319
+ let cookies = UtilsCore.document.cookie.split(";");
320
320
  cookies.forEach((item) => {
321
321
  item = item.trim();
322
322
  let itemSplit = item.split("=");
@@ -328,7 +328,7 @@ class UtilsGMCookie {
328
328
  : new RegExp("^" + details.name, "g");
329
329
  if (itemName.match(nameRegexp)) {
330
330
  resultData.push({
331
- domain: globalThis.location.hostname,
331
+ domain: UtilsCore.globalThis.location.hostname,
332
332
  expirationDate: null,
333
333
  hostOnly: true,
334
334
  httpOnly: false,
@@ -361,13 +361,13 @@ class UtilsGMCookie {
361
361
  }
362
362
  let resultData = [];
363
363
  let details = {
364
- url: globalThis.location.href,
365
- domain: globalThis.location.hostname,
364
+ url: UtilsCore.globalThis.location.href,
365
+ domain: UtilsCore.globalThis.location.hostname,
366
366
  name: "",
367
367
  path: "/",
368
368
  };
369
369
  details = utils.assign(details, paramDetails);
370
- let cookies = document.cookie.split(";");
370
+ let cookies = UtilsCore.document.cookie.split(";");
371
371
  cookies.forEach((item) => {
372
372
  item = item.trim();
373
373
  let itemSplit = item.split("=");
@@ -379,7 +379,7 @@ class UtilsGMCookie {
379
379
  : new RegExp("^" + details.name, "g");
380
380
  if (itemName.match(nameRegexp)) {
381
381
  resultData.push({
382
- domain: globalThis.location.hostname,
382
+ domain: UtilsCore.globalThis.location.hostname,
383
383
  expirationDate: null,
384
384
  hostOnly: true,
385
385
  httpOnly: false,
@@ -402,10 +402,10 @@ class UtilsGMCookie {
402
402
  set(paramDetails, callback = (error) => { }) {
403
403
  try {
404
404
  let details = {
405
- url: window.location.href,
405
+ url: UtilsCore.window.location.href,
406
406
  name: "",
407
407
  value: "",
408
- domain: window.location.hostname,
408
+ domain: UtilsCore.window.location.hostname,
409
409
  path: "/",
410
410
  secure: true,
411
411
  httpOnly: false,
@@ -424,7 +424,7 @@ class UtilsGMCookie {
424
424
  ";expires=" +
425
425
  new Date(life).toGMTString() +
426
426
  "; path=/";
427
- document.cookie = cookieStr;
427
+ UtilsCore.document.cookie = cookieStr;
428
428
  callback();
429
429
  }
430
430
  catch (error) {
@@ -439,14 +439,14 @@ class UtilsGMCookie {
439
439
  delete(paramDetails, callback = (error) => { }) {
440
440
  try {
441
441
  let details = {
442
- url: window.location.href,
442
+ url: UtilsCore.window.location.href,
443
443
  name: "",
444
444
  // @ts-ignore
445
445
  firstPartyDomain: "",
446
446
  };
447
447
  details = utils.assign(details, paramDetails);
448
448
  let cookieStr = details.name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
449
- document.cookie = cookieStr;
449
+ UtilsCore.document.cookie = cookieStr;
450
450
  callback();
451
451
  }
452
452
  catch (error) {
@@ -1215,7 +1215,7 @@ class GMMenu {
1215
1215
  }
1216
1216
  /* 不刷新网页就刷新菜单 */
1217
1217
  if (menuOption.autoReload) {
1218
- window.location.reload();
1218
+ UtilsCore.window.location.reload();
1219
1219
  }
1220
1220
  else {
1221
1221
  that.context.update();
@@ -1907,13 +1907,14 @@ class Httpx {
1907
1907
  }
1908
1908
  catch (error) {
1909
1909
  if (details.url.startsWith("//")) {
1910
- details.url = globalThis.location.protocol + details.url;
1910
+ details.url = UtilsCore.globalThis.location.protocol + details.url;
1911
1911
  }
1912
1912
  else if (details.url.startsWith("/")) {
1913
- details.url = globalThis.location.origin + details.url;
1913
+ details.url = UtilsCore.globalThis.location.origin + details.url;
1914
1914
  }
1915
1915
  else {
1916
- details.url = globalThis.location.origin + "/" + details.url;
1916
+ details.url =
1917
+ UtilsCore.globalThis.location.origin + "/" + details.url;
1917
1918
  }
1918
1919
  }
1919
1920
  return details;
@@ -2538,10 +2539,10 @@ class indexedDB {
2538
2539
  /* websql的版本号,由于ios的问题,版本号的写法不一样 */
2539
2540
  #slqVersion = "1";
2540
2541
  /* 监听IndexDB */
2541
- #indexedDB = window.indexedDB ||
2542
- window.mozIndexedDB ||
2543
- window.webkitIndexedDB ||
2544
- window.msIndexedDB;
2542
+ #indexedDB = UtilsCore.window.indexedDB ||
2543
+ UtilsCore.window.mozIndexedDB ||
2544
+ UtilsCore.window.webkitIndexedDB ||
2545
+ UtilsCore.window.msIndexedDB;
2545
2546
  /* 缓存数据库,避免同一个页面重复创建和销毁 */
2546
2547
  #db = {};
2547
2548
  #store = null;
@@ -2965,7 +2966,7 @@ class Log {
2965
2966
  * @param _GM_info_ 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}},或者直接是一个字符串
2966
2967
  * @param console 可指定console对象为unsafeWindow下的console或者是油猴window下的console
2967
2968
  */
2968
- constructor(_GM_info_, console = globalThis.console) {
2969
+ constructor(_GM_info_, console = UtilsCore.window.console) {
2969
2970
  if (typeof _GM_info_ === "string") {
2970
2971
  this.tag = _GM_info_;
2971
2972
  }
@@ -3226,12 +3227,14 @@ class Progress {
3226
3227
  /* 元素高度 */
3227
3228
  this.#height = this.#config.canvasNode.height;
3228
3229
  /* 清除锯齿 */
3229
- if (window.devicePixelRatio) {
3230
+ if (UtilsCore.window.devicePixelRatio) {
3230
3231
  this.#config.canvasNode.style.width = this.#width + "px";
3231
3232
  this.#config.canvasNode.style.height = this.#height + "px";
3232
- this.#config.canvasNode.height = this.#height * window.devicePixelRatio;
3233
- this.#config.canvasNode.width = this.#width * window.devicePixelRatio;
3234
- this.#ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
3233
+ this.#config.canvasNode.height =
3234
+ this.#height * UtilsCore.window.devicePixelRatio;
3235
+ this.#config.canvasNode.width =
3236
+ this.#width * UtilsCore.window.devicePixelRatio;
3237
+ this.#ctx.scale(UtilsCore.window.devicePixelRatio, UtilsCore.window.devicePixelRatio);
3235
3238
  }
3236
3239
  /* 设置线宽 */
3237
3240
  this.#ctx.lineWidth = this.#config.lineWidth;
@@ -3510,24 +3513,24 @@ class Utils {
3510
3513
  if (typeof cssText !== "string") {
3511
3514
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
3512
3515
  }
3513
- let cssNode = document.createElement("style");
3516
+ let cssNode = UtilsCore.document.createElement("style");
3514
3517
  cssNode.setAttribute("type", "text/css");
3515
3518
  cssNode.innerHTML = cssText;
3516
- if (document.head) {
3519
+ if (UtilsCore.document.head) {
3517
3520
  /* 插入head最后 */
3518
- document.head.appendChild(cssNode);
3521
+ UtilsCore.document.head.appendChild(cssNode);
3519
3522
  }
3520
- else if (document.body) {
3523
+ else if (UtilsCore.document.body) {
3521
3524
  /* 插入body后 */
3522
- document.body.appendChild(cssNode);
3525
+ UtilsCore.document.body.appendChild(cssNode);
3523
3526
  }
3524
- else if (document.documentElement.childNodes.length === 0) {
3527
+ else if (UtilsCore.document.documentElement.childNodes.length === 0) {
3525
3528
  /* 插入#html第一个元素后 */
3526
- document.documentElement.appendChild(cssNode);
3529
+ UtilsCore.document.documentElement.appendChild(cssNode);
3527
3530
  }
3528
3531
  else {
3529
3532
  /* 插入head前面 */
3530
- document.documentElement.insertBefore(cssNode, document.documentElement.childNodes[0]);
3533
+ UtilsCore.document.documentElement.insertBefore(cssNode, UtilsCore.document.documentElement.childNodes[0]);
3531
3534
  }
3532
3535
  return cssNode;
3533
3536
  }
@@ -3658,7 +3661,8 @@ class Utils {
3658
3661
  let elementPosXRight = Number(element.getBoundingClientRect().right); /* 要检测的元素的相对屏幕的横坐标最右边 */
3659
3662
  let elementPosYTop = Number(element.getBoundingClientRect().top); /* 要检测的元素的相对屏幕的纵坐标最上边 */
3660
3663
  let elementPosYBottom = Number(element.getBoundingClientRect().bottom); /* 要检测的元素的相对屏幕的纵坐标最下边 */
3661
- let clickNodeHTML = window.event.target.innerHTML;
3664
+ let clickNodeHTML = UtilsCore.window.event.target
3665
+ .innerHTML;
3662
3666
  if (mouseClickPosX >= elementPosXLeft &&
3663
3667
  mouseClickPosX <= elementPosXRight &&
3664
3668
  mouseClickPosY >= elementPosYTop &&
@@ -3786,18 +3790,18 @@ class Utils {
3786
3790
  }
3787
3791
  if (isIFrame) {
3788
3792
  /* 使用iframe */
3789
- const iframeElement = document.createElement("iframe");
3793
+ const iframeElement = UtilsCore.document.createElement("iframe");
3790
3794
  iframeElement.style.display = "none";
3791
3795
  iframeElement.src = base64Data;
3792
- document.body.appendChild(iframeElement);
3796
+ UtilsCore.document.body.appendChild(iframeElement);
3793
3797
  setTimeout(() => {
3794
3798
  iframeElement.contentWindow.document.execCommand("SaveAs", true, fileName);
3795
- document.body.removeChild(iframeElement);
3799
+ UtilsCore.document.body.removeChild(iframeElement);
3796
3800
  }, 100);
3797
3801
  }
3798
3802
  else {
3799
3803
  /* 使用A标签 */
3800
- const linkElement = document.createElement("a");
3804
+ const linkElement = UtilsCore.document.createElement("a");
3801
3805
  linkElement.setAttribute("target", "_blank");
3802
3806
  linkElement.download = fileName;
3803
3807
  linkElement.href = base64Data;
@@ -3811,7 +3815,7 @@ class Utils {
3811
3815
  /* CODE FOR BROWSERS THAT SUPPORT window.find */
3812
3816
  let windowFind = UtilsCore.self.find;
3813
3817
  strFound = windowFind(str, caseSensitive, true, true, false);
3814
- if (strFound && self.getSelection && !self.getSelection().anchorNode) {
3818
+ if (strFound && UtilsCore.self.getSelection && !UtilsCore.self.getSelection().anchorNode) {
3815
3819
  strFound = windowFind(str, caseSensitive, true, true, false);
3816
3820
  }
3817
3821
  if (!strFound) {
@@ -4206,8 +4210,8 @@ class Utils {
4206
4210
  let maxZIndexCompare = 2 * Math.pow(10, 9);
4207
4211
  // 当前页面最大的z-index
4208
4212
  let zIndex = 0;
4209
- document.querySelectorAll("*").forEach((element, index) => {
4210
- let nodeStyle = window.getComputedStyle(element);
4213
+ UtilsCore.document.querySelectorAll("*").forEach(($ele, index) => {
4214
+ let nodeStyle = UtilsCore.window.getComputedStyle($ele);
4211
4215
  /* 不对position为static和display为none的元素进行获取它们的z-index */
4212
4216
  if (nodeStyle.position !== "static" && nodeStyle.display !== "none") {
4213
4217
  let nodeZIndex = parseInt(nodeStyle.zIndex);
@@ -4421,7 +4425,7 @@ class Utils {
4421
4425
  if (url.trim() === "") {
4422
4426
  throw new TypeError("url不能为空字符串或纯空格");
4423
4427
  }
4424
- return `thunder://${globalThis.btoa("AA" + url + "ZZ")}`;
4428
+ return `thunder://${UtilsCore.globalThis.btoa("AA" + url + "ZZ")}`;
4425
4429
  }
4426
4430
  /**
4427
4431
  * 对于GM_cookie的兼容写法,当无法使用GM_cookie时可以使用这个,但是并不完全兼容,有些写不出来且限制了httponly是无法访问的
@@ -4636,9 +4640,11 @@ class Utils {
4636
4640
  return Boolean(target.toString().match(/^function .*\(\) { \[native code\] }$/));
4637
4641
  }
4638
4642
  isNearBottom(nearValue = 50) {
4639
- var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
4640
- var windowHeight = window.innerHeight || document.documentElement.clientHeight;
4641
- var documentHeight = document.documentElement.scrollHeight;
4643
+ var scrollTop = UtilsCore.window.pageYOffset ||
4644
+ UtilsCore.document.documentElement.scrollTop;
4645
+ var windowHeight = UtilsCore.window.innerHeight ||
4646
+ UtilsCore.document.documentElement.clientHeight;
4647
+ var documentHeight = UtilsCore.document.documentElement.scrollHeight;
4642
4648
  return scrollTop + windowHeight >= documentHeight - nearValue;
4643
4649
  }
4644
4650
  isDOM(target) {
@@ -4881,7 +4887,7 @@ class Utils {
4881
4887
  return result;
4882
4888
  }
4883
4889
  isThemeDark() {
4884
- return globalThis.matchMedia("(prefers-color-scheme: dark)").matches;
4890
+ return UtilsCore.globalThis.matchMedia("(prefers-color-scheme: dark)").matches;
4885
4891
  }
4886
4892
  isVisible(element, inView = false) {
4887
4893
  let needCheckDomList = [];
@@ -4894,15 +4900,17 @@ class Utils {
4894
4900
  }
4895
4901
  let result = true;
4896
4902
  for (const domItem of needCheckDomList) {
4897
- let domDisplay = window.getComputedStyle(domItem);
4903
+ let domDisplay = UtilsCore.window.getComputedStyle(domItem);
4898
4904
  if (domDisplay.display === "none") {
4899
4905
  result = false;
4900
4906
  }
4901
4907
  else {
4902
4908
  let domClientRect = domItem.getBoundingClientRect();
4903
4909
  if (inView) {
4904
- let viewportWidth = window.innerWidth || document.documentElement.clientWidth;
4905
- let viewportHeight = window.innerHeight || document.documentElement.clientHeight;
4910
+ let viewportWidth = UtilsCore.window.innerWidth ||
4911
+ UtilsCore.document.documentElement.clientWidth;
4912
+ let viewportHeight = UtilsCore.window.innerHeight ||
4913
+ UtilsCore.document.documentElement.clientHeight;
4906
4914
  result = !(domClientRect.right < 0 ||
4907
4915
  domClientRect.left > viewportWidth ||
4908
4916
  domClientRect.bottom < 0 ||
@@ -5125,7 +5133,7 @@ class Utils {
5125
5133
  immediate: false,
5126
5134
  };
5127
5135
  observer_config = UtilsContext.assign(default_obverser_config, observer_config);
5128
- let windowMutationObserver = window.MutationObserver ||
5136
+ let windowMutationObserver = UtilsCore.window.MutationObserver ||
5129
5137
  UtilsCore.window.webkitMutationObserver ||
5130
5138
  UtilsCore.window.MozMutationObserver;
5131
5139
  let mutationObserver = new windowMutationObserver(function (mutations, observer) {
@@ -5444,7 +5452,7 @@ class Utils {
5444
5452
  return isNegative ? -reversedNum : reversedNum;
5445
5453
  }
5446
5454
  selectElementText(element, childTextNode, startIndex, endIndex) {
5447
- let range = document.createRange();
5455
+ let range = UtilsCore.document.createRange();
5448
5456
  range.selectNodeContents(element);
5449
5457
  if (childTextNode) {
5450
5458
  if (childTextNode.nodeType !== Node.TEXT_NODE) {
@@ -5455,7 +5463,7 @@ class Utils {
5455
5463
  range.setEnd(childTextNode, endIndex);
5456
5464
  }
5457
5465
  }
5458
- let selection = globalThis.getSelection();
5466
+ let selection = UtilsCore.globalThis.getSelection();
5459
5467
  if (selection) {
5460
5468
  selection.removeAllRanges();
5461
5469
  selection.addRange(range);
@@ -5536,15 +5544,15 @@ class Utils {
5536
5544
  */
5537
5545
  copyTextByTextArea() {
5538
5546
  try {
5539
- let copyElement = document.createElement("textarea");
5547
+ let copyElement = UtilsCore.document.createElement("textarea");
5540
5548
  copyElement.value = this.#copyData;
5541
5549
  copyElement.setAttribute("type", "text");
5542
5550
  copyElement.setAttribute("style", "opacity:0;position:absolute;");
5543
5551
  copyElement.setAttribute("readonly", "readonly");
5544
- document.body.appendChild(copyElement);
5552
+ UtilsCore.document.body.appendChild(copyElement);
5545
5553
  copyElement.select();
5546
- document.execCommand("copy");
5547
- document.body.removeChild(copyElement);
5554
+ UtilsCore.document.execCommand("copy");
5555
+ UtilsCore.document.body.removeChild(copyElement);
5548
5556
  return true;
5549
5557
  }
5550
5558
  catch (error) {
@@ -5621,11 +5629,11 @@ class Utils {
5621
5629
  }
5622
5630
  return new Promise((resolve) => {
5623
5631
  const utilsClipboard = new UtilsClipboard(resolve, data, textType);
5624
- if (document.hasFocus()) {
5632
+ if (UtilsCore.document.hasFocus()) {
5625
5633
  utilsClipboard.init();
5626
5634
  }
5627
5635
  else {
5628
- window.addEventListener("focus", () => {
5636
+ UtilsCore.window.addEventListener("focus", () => {
5629
5637
  utilsClipboard.init();
5630
5638
  }, { once: true });
5631
5639
  }
@@ -5658,12 +5666,12 @@ class Utils {
5658
5666
  dragSlider(selector, offsetX = UtilsCore.window.innerWidth) {
5659
5667
  function initMouseEvent(eventName, offSetX, offSetY) {
5660
5668
  let win = unsafeWindow || window;
5661
- let mouseEvent = document.createEvent("MouseEvents");
5669
+ let mouseEvent = UtilsCore.document.createEvent("MouseEvents");
5662
5670
  mouseEvent.initMouseEvent(eventName, true, true, win, 0, offSetX, offSetY, offSetX, offSetY, false, false, false, false, 0, null);
5663
5671
  return mouseEvent;
5664
5672
  }
5665
5673
  let sliderElement = typeof selector === "string"
5666
- ? document.querySelector(selector)
5674
+ ? UtilsCore.document.querySelector(selector)
5667
5675
  : selector;
5668
5676
  if (!(sliderElement instanceof Node) ||
5669
5677
  !(sliderElement instanceof Element)) {
@@ -5697,7 +5705,7 @@ class Utils {
5697
5705
  console.error(err);
5698
5706
  }
5699
5707
  }
5700
- exitFullScreen(element = document.documentElement) {
5708
+ exitFullScreen(element = UtilsCore.document.documentElement) {
5701
5709
  if (UtilsCore.document.exitFullscreen) {
5702
5710
  return UtilsCore.document.exitFullscreen();
5703
5711
  }