gbCookies

优雅的搜索引擎跳转助手专用COOKIES处理函数

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/460897/1277476/gbCookies.js

  1. // ==UserScript==
  2. // @name gbCookies
  3. // @description cookies tool for SearchEngine Assistant
  4. // @author F9y4ng
  5. // @version 1.6
  6. // @license GPL-3.0-only
  7. // ==/UserScript==
  8.  
  9. (function (global, factory) {
  10. global = global || self;
  11. global.gbCookies = factory();
  12. })(typeof window !== "undefined" ? window : this, function () {
  13. "use strict";
  14. const gbCookies = {
  15. getItem: function (sKey) {
  16. return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + sKey.replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
  17. },
  18. setItem: function ({ sKey, sValue, sEnd, sPath, sDomain, sSameSite, sSecure }) {
  19. if (!sKey || /^(?:expires|max-age|path|domain|samesite|secure)$/i.test(sKey)) return false;
  20. let sExpires = "";
  21. if (sEnd) {
  22. switch (sEnd.constructor) {
  23. case Number:
  24. sExpires = sEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; expires=" + new Date(Number(new Date()) + sEnd).toUTCString();
  25. break;
  26. case String:
  27. sExpires = "; expires=" + new Date(sEnd);
  28. break;
  29. case Date:
  30. sExpires = "; expires=" + sEnd.toUTCString();
  31. break;
  32. }
  33. }
  34. document.cookie =
  35. sKey +
  36. "=" +
  37. sValue +
  38. sExpires +
  39. (sDomain ? "; domain=" + sDomain : "") +
  40. (sPath ? "; path=" + sPath : "") +
  41. (sSameSite ? "; SameSite=" + sSameSite : "") +
  42. (sSecure ? "; secure" : "");
  43. return true;
  44. },
  45. removeItem: function ({ sKey, sPath, sDomain }) {
  46. if (!sKey || !this.hasItem(sKey)) return false;
  47. document.cookie = sKey + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
  48. return true;
  49. },
  50. hasItem: function (sKey) {
  51. return new RegExp("(?:^|;\\s*)" + sKey.replace(/[-.+*]/g, "\\$&") + "\\s*\\=").test(document.cookie);
  52. },
  53. };
  54. return gbCookies;
  55. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址