import { DOMUtils } from "@/env"; import { UIInfo } from "@/setting/components/ui-info"; import { PanelKeyConfig } from "@/setting/panel-key-config"; import { TamperMonkeyUtils } from "@/utils/TamperMonkeyUtils"; import type { PopsPanelContainerConfig } from "@whitesev/pops/dist/types/src/components/panel/types/components-container.js"; import type { PopsPanelContentConfig } from "@whitesev/pops/dist/types/src/components/panel/types/index.js"; import Qmsg from "qmsg"; import { GM, GM_deleteValue, GM_getValue, GM_setValue } from "ViteGM"; import { ApiAsyncTestBase } from "../base/ApiAsyncTestBase"; import { StorageApi } from "../StorageApi"; export class ApiTest_deleteValue extends ApiAsyncTestBase { public isSupport() { return typeof GM_deleteValue === "function"; } public getApiName() { return "GM_deleteValue"; } public getAsyncApiOption() { return { name: "GM.deleteValue", isSupport: this.isSupportGM() && typeof GM.deleteValue === "function", }; } public getUIOption() { const that = this; let apiName = this.getApiName(); let apiAsyncInfo = this.getAsyncApiOption(); let result: PopsPanelContentConfig = { id: "aside-" + apiName, title: "" + apiName, headerTitle: `${TamperMonkeyUtils.getApiDocUrl(apiName, `${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, tag: "success", } : { text: "不支持 " + apiName, tag: "error", } ), UIInfo(() => apiAsyncInfo.name ? { text: "支持 " + apiAsyncInfo.name, tag: "success", } : { text: "不支持 " + apiAsyncInfo.name, tag: "error", } ), ], }, { type: "container", text: "功能测试", views: [], }, { type: "container", text: "功能测试(异步)", views: [], }, ], }; if (this.isSupport()) { [ { name: apiName, fn: async (...args: any[]) => { return new Promise((resolve) => { // @ts-ignore const ret = GM_deleteValue(...args); resolve(ret); }); }, formList: (result["views"][1]).views, }, { name: apiAsyncInfo.name, fn: GM.deleteValue, formList: (result["views"][2]).views, }, ].forEach((data) => { let apiNameTag = data.name; data.formList.push( (() => { let localStorageDataKey = `Test ${apiNameTag} null`; let localStorageDataValue = null; return UIInfo(() => { return { text: "测试存储null值并删除", description: `"${localStorageDataKey}": ${localStorageDataValue}`, tag: "info", afterRender(container) { let $button = DOMUtils.toElement( /*html*/ `
`, false, false ); DOMUtils.after(container.$leftContainer, $button); // 点击事件 DOMUtils.on($button, "click", async (event) => { DOMUtils.preventEvent(event); try { GM_setValue(localStorageDataKey, localStorageDataValue); await data.fn(localStorageDataKey); let value = GM_getValue(localStorageDataKey); if (typeof value === "object" && value === null) { Qmsg.error("该值未删除,读取的值:" + value); } else { Qmsg.success("成功删除该值"); } } catch (error: any) { Qmsg.error(error.toString()); } }); }, }; }); })() ); }); } return result; } }