import { GM, GM_setValues } from "ViteGM"; import { ApiTestBase } from "../base/ApiTestBase"; import type { PopsPanelContentConfig } from "@whitesev/pops/dist/types/src/components/panel/types/index.js"; import { StorageApi } from "../StorageApi"; import { PanelKeyConfig } from "@/setting/panel-key-config"; import { UIInfo } from "@/setting/components/ui-info"; import { DOMUtils, utils } from "@/env"; import { CommonUtil } from "@components/utils/CommonUtil"; import { ApiAsyncTestBase } from "../base/ApiAsyncTestBase"; import Qmsg from "qmsg"; import { TamperMonkeyUtils } from "@/utils/TamperMonkeyUtils"; import type { PopsPanelContainerConfig } from "@whitesev/pops/dist/types/src/components/panel/types/components-container.js"; export class ApiTest_setValues extends ApiAsyncTestBase { public isSupport() { return typeof GM_setValues === "function"; } public getApiName() { return "GM_setValues"; } public getAsyncApiOption() { return { name: "GM.setValues", isSupport: this.isSupportGM() && typeof GM.setValues === "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.isSupport ? { 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_setValues(...args); resolve(ret); }); }, formList: (result["views"][1]).views, }, { name: apiAsyncInfo.name, fn: GM.setValues, formList: (result["views"][2]).views, }, ].forEach((data) => { let apiNameTag = data.name; data.formList.push( (() => { let localStorageDataValue = { foo: 1, bar: 2 }; return UIInfo(() => { return { text: "测试存储对象", description: JSON.stringify(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 { await data.fn(localStorageDataValue); Qmsg.info("执行写入完毕,请自行查看是否成功写入"); } catch (error: any) { Qmsg.error(error.toString()); } }); }, }; }); })() ); }); } return result; } }