import { GM, GM_cookie, type GmCookieType } from "ViteGM"; import type { PopsPanelContentConfig } from "@whitesev/pops/dist/types/src/components/panel/types/index.js"; import { StorageApi } from "../StorageApi"; import { CommonUtil } from "@components/utils/CommonUtil"; import { ApiAsyncTestBase } from "../base/ApiAsyncTestBase"; import { DOMUtils, pops, utils } from "@/env"; import Qmsg from "qmsg"; import { TamperMonkeyUtils } from "@/utils/TamperMonkeyUtils"; import { PanelKeyConfig } from "@/setting/panel-key-config"; import { UIInfo } from "@/setting/components/ui-info"; import { PanelUISize } from "@components/setting/panel-ui-size"; import type { PopsPanelContainerConfig } from "@whitesev/pops/dist/types/src/components/panel/types/components-container.js"; interface GmCookieSetOptions { url?: string; name: string; value: string; domain?: string; firstPartyDomain?: string; partitionKey?: { topLevelSite?: string; }; path?: string; secure?: boolean; httpOnly?: boolean; expirationDate?: number; } export class ApiTest_cookie extends ApiAsyncTestBase { public isSupport() { return (typeof GM_cookie === "object" || typeof GM_cookie === "function") && GM_cookie != null; } public getApiOption() { let isSupport = this.isSupport(); return { isSupportList: isSupport && typeof GM_cookie.list === "function", isSupportSet: isSupport && typeof GM_cookie.set === "function", isSupportDelete: isSupport && typeof GM_cookie.delete === "function", }; } public getApiName() { return "GM_cookie"; } public getAsyncApiOption() { let isSupportAsync = this.isSupportGM() && (typeof GM.cookie === "object" || typeof GM.cookie === "function") && GM.cookie != null; return { name: "GM.cookie", isSupport: isSupportAsync, isSupportList: isSupportAsync && typeof GM.cookie.list === "function", isSupportSet: isSupportAsync && typeof GM.cookie.set === "function", isSupportDelete: isSupportAsync && typeof GM.cookie.delete === "function", }; } public getUIOption() { const that = this; let apiName = this.getApiName(); let apiInfo = this.getApiOption(); let apiAsyncInfo = this.getAsyncApiOption(); let result: PopsPanelContentConfig = { id: "aside-" + apiName, title: "" + apiName, headerTitle: `${TamperMonkeyUtils.getApiDocUrl(apiName + ".list", `${apiName} & ${apiAsyncInfo.name}`)}`, scrollToDefaultView: true, isDefault() { return StorageApi.get(PanelKeyConfig.asideLastVisit) === apiName; }, clickCallback(data) { StorageApi.set(PanelKeyConfig.asideLastVisit, apiName); }, views: [ { type: "container", text: "函数测试", views: [ UIInfo(() => this.isSupport() ? { text: `支持 ${apiName},类型 ${typeof GM_cookie}`, tag: "success", } : { text: "不支持 " + apiName, tag: "error", } ), ], }, { type: "container", text: "功能测试", views: [], }, { type: "container", text: "功能测试(异步)", views: [], }, ], }; let firstFormList = (result["views"][0]).views; if (this.isSupport()) { firstFormList.push( UIInfo(() => { return apiInfo.isSupportList ? { text: `支持 ${apiName}.list`, tag: "success", } : { text: `不支持 ${apiName}.list`, tag: "error", }; }), UIInfo(() => { return apiInfo.isSupportSet ? { text: `支持 ${apiName}.set`, tag: "success", } : { text: `不支持 ${apiName}.set`, tag: "error", }; }), UIInfo(() => { return apiInfo.isSupportDelete ? { text: `支持 ${apiName}.delete`, tag: "success", } : { text: `不支持 ${apiName}.delete`, tag: "error", }; }) ); } if (apiAsyncInfo.isSupport) { firstFormList.push( UIInfo(() => { return apiAsyncInfo.isSupportList ? { text: `支持 ${apiAsyncInfo.name}.list`, tag: "success", } : { text: `不支持 ${apiAsyncInfo.name}.list`, tag: "error", }; }), UIInfo(() => { return apiAsyncInfo.isSupportSet ? { text: `支持 ${apiAsyncInfo.name}.set`, tag: "success", } : { text: `不支持 ${apiAsyncInfo.name}.set`, tag: "error", }; }), UIInfo(() => { return apiAsyncInfo.isSupportDelete ? { text: `支持 ${apiAsyncInfo.name}.delete`, tag: "success", } : { text: `不支持 ${apiAsyncInfo.name}.delete`, tag: "error", }; }) ); } else { firstFormList.push( UIInfo(() => { return { text: "不支持 " + apiAsyncInfo.name, tag: "error" }; }) ); } if (this.isSupport()) { let newCookieInfo: GmCookieSetOptions = { name: "test", value: "1", expirationDate: (Date.now() + 24 * 60 * 60 * 1000) / 1000, }; [ { name: apiName, list: async (...args: any[]) => { return new Promise((resolve, reject) => { const [details, cb] = args; GM_cookie.list(details, (cookies, error) => { if (error) { reject(error); } else { resolve(cookies); } }); }); }, set: async (...args: any[]) => { return new Promise((resolve, reject) => { const [details, cb] = args; GM_cookie.set(details, (error) => { if (error) { reject(error); } else { resolve(undefined); } }); }); }, delete: async (...args: any[]) => { return new Promise((resolve, reject) => { const [details, cb] = args; GM_cookie.delete(details, (error) => { if (error) { reject(error); } else { resolve(undefined); } }); }); }, formList: (result["views"][1]).views, }, { name: apiAsyncInfo.name, list: GM.cookie?.list, set: GM.cookie?.set, delete: GM.cookie?.delete, formList: (result["views"][2]).views, }, ].forEach((data) => { let apiNameTag = data.name; data.formList.push( UIInfo(() => { try { return { text: CommonUtil.escapeHtml("测试list获取所有Cookie"), tag: "info", description: "点击按钮进行测试", afterRender(container) { let $button = DOMUtils.toElement( /*html*/ `
`, false, false ); DOMUtils.on($button, "click", async (event) => { try { DOMUtils.preventEvent(event); const cookies = await data.list({}); console.log(cookies); if (Array.isArray(cookies)) { pops.alert({ title: { text: data.name + ".list", position: "center", }, content: { text: JSON.stringify(cookies, null, 4), html: true, }, drag: true, mask: { enable: true, }, width: PanelUISize.setting.width, height: PanelUISize.setting.height, style: /*css*/ ` .pops-alert-content{ white-space: pre-wrap; } `, }); } else { alert("获取的cookie信息不是数组"); } } catch (error: any) { Qmsg.error(error.toString()); } }); DOMUtils.after(container.$leftContainer, $button); }, }; } catch (error) { console.error(error); return { text: "执行错误 " + error, tag: "error", }; } finally { } }), UIInfo(() => { try { return { text: CommonUtil.escapeHtml("测试set新增Cookie"), tag: "info", description: JSON.stringify(newCookieInfo), afterRender(container) { let $button = DOMUtils.toElement( /*html*/ `
`, false, false ); DOMUtils.on($button, "click", async (event) => { try { DOMUtils.preventEvent(event); await data.set(newCookieInfo); Qmsg.success(data.name + " set cookie success"); } catch (error: any) { Qmsg.error(error.toString()); } }); DOMUtils.after(container.$leftContainer, $button); }, }; } catch (error) { console.error(error); return { text: "执行错误 " + error, tag: "error", }; } finally { } }), UIInfo(() => { try { let deleteCookieInfo = { name: "test", }; return { text: CommonUtil.escapeHtml("测试delete删除Cookie"), tag: "info", description: JSON.stringify(deleteCookieInfo), afterRender(container) { let $button = DOMUtils.toElement( /*html*/ `
`, false, false ); DOMUtils.on($button, "click", async (event) => { try { DOMUtils.preventEvent(event); await data.delete(deleteCookieInfo); Qmsg.success(data.name + " delete cookie success"); } catch (error: any) { Qmsg.error(error.toString()); } }); DOMUtils.after(container.$leftContainer, $button); }, }; } catch (error) { console.error(error); return { text: "执行错误 " + error, tag: "error", }; } finally { } }) ); }); } return result; } }