@whitesev/utils 2.5.3 → 2.5.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.umd.js CHANGED
@@ -276,24 +276,24 @@
276
276
  }
277
277
  /**
278
278
  * 获取多组Cookie
279
- * @param paramDetails 配置
279
+ * @param option 配置
280
280
  * @param callback 获取操作后的回调
281
281
  * + cookies object[]
282
282
  * + error string|undefined
283
283
  **/
284
- list(paramDetails, callback) {
285
- if (paramDetails == null) {
284
+ list(option, callback) {
285
+ if (option == null) {
286
286
  throw new Error("Utils.GMCookie.list 参数不能为空");
287
287
  }
288
288
  let resultData = [];
289
289
  try {
290
- let details = {
290
+ let defaultOption = {
291
291
  url: this.windowApi.window.location.href,
292
292
  domain: this.windowApi.window.location.hostname,
293
293
  name: "",
294
294
  path: "/",
295
295
  };
296
- details = utils.assign(details, paramDetails);
296
+ defaultOption = utils.assign(defaultOption, option);
297
297
  let cookies = this.windowApi.document.cookie.split(";");
298
298
  cookies.forEach((item) => {
299
299
  item = item.trim();
@@ -301,9 +301,9 @@
301
301
  let itemName = itemSplit[0];
302
302
  itemSplit.splice(0, 1);
303
303
  let itemValue = decodeURIComponent(itemSplit.join(""));
304
- let nameRegexp = details.name instanceof RegExp
305
- ? details.name
306
- : new RegExp("^" + details.name, "g");
304
+ let nameRegexp = defaultOption.name instanceof RegExp
305
+ ? defaultOption.name
306
+ : new RegExp("^" + defaultOption.name, "g");
307
307
  if (itemName.match(nameRegexp)) {
308
308
  resultData.push({
309
309
  domain: this.windowApi.window.location.hostname,
@@ -331,20 +331,20 @@
331
331
  }
332
332
  /**
333
333
  * 获取多组Cookie
334
- * @param paramDetails 配置
334
+ * @param option 配置
335
335
  **/
336
- getList(paramDetails) {
337
- if (paramDetails == null) {
336
+ getList(option) {
337
+ if (option == null) {
338
338
  throw new Error("Utils.GMCookie.list 参数不能为空");
339
339
  }
340
340
  let resultData = [];
341
- let details = {
341
+ let defaultOption = {
342
342
  url: this.windowApi.window.location.href,
343
343
  domain: this.windowApi.window.location.hostname,
344
344
  name: "",
345
345
  path: "/",
346
346
  };
347
- details = utils.assign(details, paramDetails);
347
+ defaultOption = utils.assign(defaultOption, option);
348
348
  let cookies = this.windowApi.document.cookie.split(";");
349
349
  cookies.forEach((item) => {
350
350
  item = item.trim();
@@ -352,9 +352,9 @@
352
352
  let itemName = itemSplit[0];
353
353
  itemSplit.splice(0, 1);
354
354
  let itemValue = decodeURIComponent(itemSplit.join(""));
355
- let nameRegexp = details.name instanceof RegExp
356
- ? details.name
357
- : new RegExp("^" + details.name, "g");
355
+ let nameRegexp = defaultOption.name instanceof RegExp
356
+ ? defaultOption.name
357
+ : new RegExp("^" + defaultOption.name, "g");
358
358
  if (itemName.match(nameRegexp)) {
359
359
  resultData.push({
360
360
  domain: this.windowApi.window.location.hostname,
@@ -374,12 +374,13 @@
374
374
  }
375
375
  /**
376
376
  * 设置Cookie
377
- * @param paramDetails 配置
377
+ * @param option 配置
378
378
  * @param callback 设置操作后的回调(成功/失败)
379
379
  */
380
- set(paramDetails, callback = (error) => { }) {
380
+ set(option, callback) {
381
+ let errorInfo;
381
382
  try {
382
- let details = {
383
+ let defaultOption = {
383
384
  url: this.windowApi.window.location.href,
384
385
  name: "",
385
386
  value: "",
@@ -392,43 +393,52 @@
392
393
  */
393
394
  expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
394
395
  };
395
- details = utils.assign(details, paramDetails);
396
- let life = details.expirationDate
397
- ? details.expirationDate
396
+ defaultOption = utils.assign(defaultOption, option);
397
+ let life = defaultOption.expirationDate
398
+ ? defaultOption.expirationDate
398
399
  : Math.floor(Date.now()) + 60 * 60 * 24 * 30;
399
- let cookieStr = details.name +
400
+ let cookieStr = defaultOption.name +
400
401
  "=" +
401
- decodeURIComponent(details.value) +
402
+ decodeURIComponent(defaultOption.value) +
402
403
  ";expires=" +
403
404
  new Date(life).toGMTString() +
404
405
  "; path=/";
405
406
  this.windowApi.document.cookie = cookieStr;
406
- callback();
407
407
  }
408
408
  catch (error) {
409
- callback(error);
409
+ errorInfo = error;
410
+ }
411
+ finally {
412
+ if (typeof callback === "function") {
413
+ callback(errorInfo);
414
+ }
410
415
  }
411
416
  }
412
417
  /**
413
418
  * 删除Cookie
414
- * @param paramDetails 配置
419
+ * @param option 配置
415
420
  * @param callback 删除操作后的回调(成功/失败)
416
421
  */
417
- delete(paramDetails, callback = (error) => { }) {
422
+ delete(option, callback) {
423
+ let errorInfo;
418
424
  try {
419
- let details = {
425
+ let defaultOption = {
420
426
  url: this.windowApi.window.location.href,
421
427
  name: "",
422
- // @ts-ignore
423
- firstPartyDomain: "",
428
+ path: "/",
429
+ firstPartyDomain: this.windowApi.window.location.hostname,
424
430
  };
425
- details = utils.assign(details, paramDetails);
426
- let cookieStr = details.name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
431
+ defaultOption = utils.assign(defaultOption, option);
432
+ let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}; domain=${defaultOption.firstPartyDomain};`;
427
433
  this.windowApi.document.cookie = cookieStr;
428
- callback();
429
434
  }
430
435
  catch (error) {
431
- callback(error);
436
+ errorInfo = error;
437
+ }
438
+ finally {
439
+ if (typeof callback === "function") {
440
+ callback(errorInfo);
441
+ }
432
442
  }
433
443
  }
434
444
  }
@@ -4114,7 +4124,7 @@
4114
4124
  this.windowApi = new WindowApi(option);
4115
4125
  }
4116
4126
  /** 版本号 */
4117
- version = "2024.11.16";
4127
+ version = "2024.12.3";
4118
4128
  addStyle(cssText) {
4119
4129
  if (typeof cssText !== "string") {
4120
4130
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");