Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将UserConfig暴露至GM_info对象中 #206

Merged
merged 3 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion example/userconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,25 @@ group2:
default: 默认值
==/UserConfig== */

// 通过GM_info新方法获取UserConfig对象
const rawUserConfig = GM_info.userConfig;
// 定义一个对象暂存读取到的UserConfig值
const userConfig = {};
// 解构遍历读取UserConfig并赋缺省值
Object.entries(rawUserConfig).forEach(([mainKey, configs]) => {
Object.entries(configs).forEach(([subKey, { default: defaultValue }]) => {
userConfig[`${mainKey}.${subKey}`] = GM_getValue(`${mainKey}.${subKey}`, defaultValue)
})
})

setInterval(() => {
console.log(GM_getValue("group1.configA"));
// 传统方法读取UserConfig,每个缺省值需要单独静态声明,修改UserConfig缺省值后代码也需要手动修改
console.log(GM_getValue("group1.configA", "默认值"));
console.log(GM_getValue("group1.configG", 11));
// GM_info新方法读取UserConfig,可直接关联读取缺省值,无需额外修改
console.log(userConfig["group1.configA"]);
console.log(userConfig["group1.configG"]);
}, 5000)

// 打开用户配置
CAT_userConfig();
11 changes: 10 additions & 1 deletion src/pkg/utils/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ export function getMetadataStr(code: string): string | null {
if (start === -1 || end === -1) {
return null;
}
return `// ${code.substring(start, end + 14)}`;
return `// ${code.substring(start, end + 15)}`;
DreamNya marked this conversation as resolved.
Show resolved Hide resolved
}

export function getUserConfigStr(code: string): string | null {
const start = code.indexOf("==UserConfig==");
const end = code.indexOf("==/UserConfig==");
if (start === -1 || end === -1) {
return null;
}
return `/* ${code.substring(start, end + 15)} */`;
}

export function parseMetadata(code: string): Metadata | null {
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/content/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
base64ToBlob,
blobToBase64,
getMetadataStr,
getUserConfigStr,
parseUserConfig,
} from "@App/pkg/utils/script";
import { v4 as uuidv4 } from "uuid";
import { ValueUpdateData } from "./exec_script";
Expand Down Expand Up @@ -124,6 +126,7 @@ export default class GMApi {
// 获取脚本信息和管理器信息
static GM_info(script: ScriptRunResouce) {
const metadataStr = getMetadataStr(script.sourceCode);
const userConfigStr = getUserConfigStr(script.sourceCode) || "";
const options = {
description:
(script.metadata.description && script.metadata.description[0]) || null,
Expand All @@ -145,6 +148,8 @@ export default class GMApi {
scriptHandler: "ScriptCat",
scriptUpdateURL: script.downloadUrl,
scriptMetaStr: metadataStr,
userConfig: parseUserConfig(userConfigStr),
userConfigStr,
// scriptSource: script.sourceCode,
version: ExtVersion,
script: {
Expand Down
52 changes: 41 additions & 11 deletions src/types/scriptcat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,63 @@

declare const unsafeWindow: Window;

declare type ConfigType =
| "text"
| "checkbox"
| "select"
| "mult-select"
| "number"
| "textarea"
| "time";

declare interface Config {
[key: string]: any;
title: string;
description: string;
default?: any;
type?: ConfigType;
bind?: string;
values?: any[];
password?: boolean;
// 文本类型时是字符串长度,数字类型时是最大值
max?: number;
min?: number;
rows?: number; // textarea行数
}

declare type UserConfig = { [key: string]: { [key: string]: Config } };

declare const GM_info: {
version: string;
scriptWillUpdate: boolean;
scriptHandler: "ScriptCat";
scriptUpdateURL?: string;
scriptSource: string;
// scriptSource: string;
scriptMetaStr?: string;
isIncognito: boolean;
downloadMode: "native" | "disabled" | "browser";
userConfig?: UserConfig,
userConfigStr?: string,
// isIncognito: boolean;
// downloadMode: "native" | "disabled" | "browser";
script: {
author?: string;
description?: string;
excludes: string[];
homepage?: string;
// excludes: string[];
grant: string[];
header: string;
// homepage?: string;
icon?: string;
icon64?: string;
includes?: string[];
lastModified: number;
// lastModified: number;
matches: string[];
name: string;
namespace?: string;
position: number;
// position: number;
"run-at": string;
resources: string[];
unwrap: boolean;
// resources: string[];
// unwrap: boolean;
version: string;
options: {
/* options: {
awareOfChrome: boolean;
run_at: string;
noframes?: boolean;
Expand All @@ -45,7 +75,7 @@ declare const GM_info: {
[key: string]: any;
};
[key: string]: any;
};
}; */
[key: string]: any;
};
[key: string]: any;
Expand Down