gbCookies

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

当前为 2023-11-03 提交的版本,查看 最新版本

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

// ==UserScript==
// @name        gbCookies
// @author      F9y4ng
// @version     1.3
// @license     GPL-3.0-only
// ==/UserScript==

const gbCookies = {
  getItem: function (sKey) {
    return (
      decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) ||
      null
    );
  },

  setItem: function ({ sKey, sValue, vEnd, sPath, sDomain, sSameSite, bSecure }) {
    if (!sKey || /^(?:expires|max-age|path|domain|samesite|secure)$/i.test(sKey)) return false;
    let sExpires = "";
    if (vEnd) {
      switch (vEnd.constructor) {
        case Number:
          sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
          break;
        case String:
          sExpires = "; expires=" + vEnd;
          break;
        case Date:
          sExpires = "; expires=" + vEnd.toUTCString();
          break;
      }
    }
    sSameSite = sSameSite ?? "";
    bSecure = bSecure ?? true;
    document.cookie =
      encodeURIComponent(sKey) +
      "=" +
      encodeURIComponent(sValue) +
      sExpires +
      (sDomain ? "; domain=" + sDomain : "") +
      (sPath ? "; path=" + sPath : "") +
      (sSameSite.tolowerCase() !== "none" ? "; SameSite=" + sSameSite : "; SameSite=None") +
      (bSecure ? "; secure" : "");
    return true;
  },

  removeItem: function (sKey, sPath, sDomain) {
    if (!sKey || !this._hasItem(sKey)) return false;
    document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
    return true;
  },

  _hasItem: function (sKey) {
    return new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=").test(document.cookie);
  },
};

QingJ © 2025

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