@yiero/gmlib
Version:
GM Lib for Tampermonkey
418 lines (414 loc) • 11.8 kB
TypeScript
// Generated by dts-bundle-generator v9.5.1
/**
* Cookie 接口
*/
export declare interface ICookie {
domain: string;
name: string;
value: string;
expirationDate: number;
hostOnly: boolean;
httpOnly: boolean;
path: string;
sameSite: string;
secure: boolean;
session: boolean;
storeId: string;
}
/**
* 获取 输入域名 下的所有 cookie 列表
*
* @param domain 域名
* @returns cookieList Cookie列表
*
* @warn 需要授权函数 `GM_cookie` 和 `GM_info`
* @warn 只能在 ScriptCat 环境中使用
*
* @example getCookie( '.bilibili.com' ) - 获取 bilibili 下的所有 cookie
*/
export declare function getCookie(domain: string): Promise<ICookie[]>;
/**
* 获取 输入域名 下的某一项 cookie 内容
*
* @param domain 域名
* @param key cookie 键名
* @returns cookie cookie 值
*
* @warn 需要授权函数 `GM_cookie` 和 `GM_info`
* @warn 只能在 ScriptCat 环境中使用
*
* @example getCookie( '.bilibili.com', 'DedeUserID' ) - 获取 bilibili uid
*/
export declare function getCookie(domain: string, key: string): Promise<string>;
/**
* 传入网站 Cookie 文本内容, 解析出对应的 key 的值
*
* @param documentCookieContent 网站 Cookie 文本, 通常为 document.cookie
* @param key cookie 键名
*
* @warn 需要授权函数 `GM_cookie` 和 `GM_info`
* @warn 只能在 ScriptCat 环境中使用
*
* @example getCookie( document.cookie, 'DedeUserID' ) - 获取 bilibili uid
*/
export declare function getCookie(documentCookieContent: string, key: string): Promise<string>;
/**
* 通过 GM_xmlhttpRequest, 发送 GET 请求
*
* @param url 请求地址
* @param [method] 请求方式(GET)
* @param [param] 请求网页参数
* @param [GMXmlHttpRequestConfig] 油猴请求参数
*
* @warn 需要授权函数 `GM_xmlhttpRequest`
*
* @example gmRequest( 'https://baidu.com' ) - 请求网页内容
* @example gmRequest( 'https://api.bilibili.com/x/report/click/now' ) - 请求接口 JSON 内容
* @example gmRequest( 'https://api.bilibili.com/x/player/videoshot', 'GET', {aid: 999} - 携带网页参数请求
*
*
* @see https://www.tampermonkey.net/documentation.php?ext=dhdg#api:GM_xmlhttpRequest
*/
export declare function gmRequest<T extends string | Record<string, any> | Document>(url: string, method?: "GET", param?: Record<string, string>, GMXmlHttpRequestConfig?: Partial<Tampermonkey.Request>): Promise<T>;
/**
* 通过 GM_xmlhttpRequest, 发送 POST 请求
*
* @param url 请求地址
* @param method 请求方式(POST)
* @param [data] 请求体参数
* @param [GMXmlHttpRequestConfig] 油猴请求参数
*
* @warn 需要授权函数 `GM_xmlhttpRequest`
*
* @example gmRequest( 'https://reqres.in/api/users', 'POST', {name: 'paul rudd',movies: [ 'I Love You Man', 'Role Models' ]} ) - 发送 POST 请求, 并携带数据
*
* @see https://www.tampermonkey.net/documentation.php?ext=dhdg#api:GM_xmlhttpRequest
*/
export declare function gmRequest<T extends string | Record<string, any> | Document, K extends any>(url: string, method: "POST", data?: Record<string, K>, GMXmlHttpRequestConfig?: Partial<Tampermonkey.Request>): Promise<T>;
/**
* 调用油猴API配置参数, 进行网络请求
*
* @param GMXmlHttpRequestConfig 油猴请求参数
*
* @warn 需要授权函数 `GM_xmlhttpRequest`
*
* @example gmRequest( {url: 'https://reqres.in/api/users',method: 'POST',name: 'paul rudd',movies: [ 'I Love You Man', 'Role Models' ],headers: {'Content-Type': 'application/json',}} ) - 发送 POST 请求
*
* @see https://www.tampermonkey.net/documentation.php?ext=dhdg#api:GM_xmlhttpRequest
*/
export declare function gmRequest<T extends string | Record<string, any> | Document>(GMXmlHttpRequestConfig: Tampermonkey.Request): Promise<T>;
/**
* 劫持 xhr 的返回数据
*/
export declare const hookXhr: <T extends string | Record<string, any> | Document>(hookUrl: (url: string) => boolean, callback: (response: T, requestUrl: string) => void | string) => void;
export type DownloadRequest = Omit<Tampermonkey.DownloadRequest, "url" | "name" | "onerror" | "onprogress"> & {
onerror?: (error: Tampermonkey.DownloadErrorResponse) => void;
onprogress?: (response: Tampermonkey.DownloadProgressResponse, abortHandle: Tampermonkey.AbortHandle<boolean>) => void;
};
/**
* 下载文件
*
* @param url 下载地址
* @param filename 文件名
* @param details GM_download details 配置对象参数
*
* @returns Promise
*/
export declare const gmDownload: {
(url: string, filename: string, details?: DownloadRequest): Promise<boolean>;
blob(blob: Blob | File, filename: string, details?: DownloadRequest): Promise<boolean>;
text(content: string, filename: string, mimeType?: string, details?: DownloadRequest): Promise<boolean>;
};
/**
* 滚动指定元素, 到指定容器的指定百分比位置
*
* @param targetElement 将要滚动的元素
* @param [container = window] 滚动元素参照的父容器
* @param [scrollPercent = .5] 滚动的百分比, 范围 [0~1]
*
* @example
* const targetContainer = document.querySelector( '.item' );
* scroll( targetContainer, targetContainer.parentElement, .5 );
*/
declare function scroll$1(targetElement: HTMLElement, container: HTMLElement | Window, scrollPercent?: number): void;
/**
* 滚动页面到指定百分比
*
* @param [scrollPercent = .5] 滚动的百分比, 范围 [0~1]
*
* @example scroll( 0 ); // 滚动页面到顶部
* @example scroll( 1 ); // 滚动页面到底部
*/
declare function scroll$1(scrollPercent?: number): void;
/**
* 元素等待器选项
*/
export interface IElementWaiterOption {
/**
* 监听器容器
*
* @default document
*/
parent: HTMLElement | DocumentFragment | Document;
/**
* 超时时间
*
* @default 20
*/
timeoutPerSecond: number;
/**
* 监听到元素触发后, 延时获取元素的时间
*
* @default .5
*/
delayPerSecond: number;
}
/**
* 等待元素载入
*
* @param selector CSS选择器
* @param options 选项
*
* @example await elementWaiter( '#app' ) - 等待 `#app` 元素载入
*/
export declare function elementWaiter<T extends HTMLElement>(selector: string, options?: Partial<IElementWaiterOption>): Promise<T>;
export interface MessageDetail {
message: string;
type: "success" | "warning" | "error" | "info";
duration: number;
position: "top" | "top-left" | "top-right" | "left" | "right" | "bottom" | "bottom-left" | "bottom-right";
}
export declare function Message(options: string | MessageDetail): void;
export declare namespace Message {
var success: (message: string, options: Omit<MessageDetail, "type" | "message">) => void;
var warning: (message: string, options: Omit<MessageDetail, "type" | "message">) => void;
var error: (message: string, options: Omit<MessageDetail, "type" | "message">) => void;
var info: (message: string, options: Omit<MessageDetail, "type" | "message">) => void;
}
export interface MenuCommand {
title: string;
onClick: () => void;
isActive: boolean;
id: number;
}
export declare class gmMenuCommand {
/**
* 菜单按钮列表
* */
static list: MenuCommand[];
private constructor();
/**
* 获取一个菜单按钮
*/
static get(title: string): MenuCommand;
/**
* 创建一个带有状态的菜单按钮
*/
static createToggle(details: {
active: Omit<MenuCommand, "id" | "isActive">;
inactive: Omit<MenuCommand, "id" | "isActive">;
}): typeof gmMenuCommand;
/**
* 手动激活一个菜单按钮
*/
static click(title: string): typeof gmMenuCommand;
/**
* 创建一个菜单按钮
*/
static create(title: string, onClick: () => void, isActive?: boolean): typeof gmMenuCommand;
/**
* 删除一个菜单按钮
*/
static remove(title: string): typeof gmMenuCommand;
/**
* 修改两个菜单按钮的顺序
*/
static swap(title1: string, title2: string): typeof gmMenuCommand;
/**
* 修改一个菜单按钮
*/
static modify(title: string, details: Partial<Omit<MenuCommand, "title" | "id">>): typeof gmMenuCommand;
/**
* 切换菜单按钮激活状态
*/
static toggleActive(title: string): typeof gmMenuCommand;
/**
* 渲染所有激活的菜单按钮
*/
static render(): void;
}
/**
* 判断当前页面是否为 iframe 页面
*/
export declare const isIframe: () => boolean;
/**
* 安装环境
*/
export type IEnvironment = "ScriptCat" | "Tampermonkey";
/**
* 输出脚本安装环境
*
* @warn 需要授权函数 `GM_info`
*/
export declare const environmentTest: () => IEnvironment;
/**
* 接口: uiImporter 选项
*/
export interface IUiImporterOption {
/**
* 是否默认将解析的 Css 添加到页面中
*
* @default true
*/
isAppendCssToDocument: boolean;
/**
* 是否默认将解析的 html Dom 树添加到页面中
*
* @default true
*/
isAppendHtmlToDocument: boolean;
/**
* 解析的 html Dom 树加载到页面中的容器.
*
* 该属性只有在 `isAppendHtmlToDocument = true` 时才会生效.
*
* @default document.body
*/
appendHtmlContainer: HTMLElement;
/**
* 是否过滤解析到的 script 标签元素
*
* @default true
*/
isFilterScriptNode: boolean;
}
/**
* 接口: uiImporter 返回值
*/
export interface IUiImporterResult {
styleNode?: HTMLStyleElement;
appendNodeList: HTMLElement[];
}
/**
* 解析 html 字符串, 返回 DocumentFragment
*
* @param htmlContent html文本内容
* @param [cssContent] css文本内容
* @param [options] 选项
*
* @warn 需要授权函数 `GM_addStyle`
*/
export declare const uiImporter: (htmlContent: string, cssContent?: string, options?: Partial<IUiImporterOption>) => IUiImporterResult;
export interface IGMStorageChangeDetail<T> {
key: string;
oldValue: T;
newValue: T;
remote: boolean;
}
/**
* 油猴存储
*/
export declare class GmStorage<T extends unknown> {
protected readonly key: string;
protected readonly defaultValue?: T | undefined;
protected listenerId: number;
constructor(key: string, defaultValue?: T | undefined);
/**
* 获取当前存储的值
*
* @alias get()
*/
get value(): T;
/**
* 获取当前存储的值
*/
get(): T;
/**
* 给当前存储设置一个新值
*/
set(value: T): void;
/**
* 移除当前键
*/
remove(): void;
/**
* 监听元素更新, 同时只能存在 1 个监听器
*/
updateListener(callback: (changeDetail: IGMStorageChangeDetail<T>) => void): void;
/**
* 移除元素更新回调
*/
removeListener(): void;
}
/**
* 储存数组的油猴存储, 方便处理数组
*
* @class
*/
export declare class GmArrayStorage<T> extends GmStorage<Array<T>> {
protected readonly key: string;
protected readonly defaultValue: Array<T>;
constructor(key: string, defaultValue?: Array<T>);
/**
* 获取数组长度
*/
get length(): number;
/**
* 获取数组最后一个项
*/
get lastItem(): T | undefined;
/**
* 设置值, 有类型检查
*/
set(value: Array<T>): void;
/**
* 基于索引修改数组项
*/
modify(value: T, index: number): void;
/**
* 清空储存, 将其变更为默认值
*/
reset(): void;
/**
* 基于索引删除数组项
*/
delete(index: number): void;
/**
* 向数组的最后添加项
*/
push(value: T): void;
/**
* 删除数组的最后一个元素
*/
pop(): void;
/**
* 向数组的最开始添加项
*/
unshift(value: T): void;
/**
* 删除数组的第一个元素
*/
shift(): void;
/**
* 遍历数组
*/
forEach(callback: (value: T, index: number, array: T[]) => void): void;
/**
* 覆盖数组
*/
map(callback: (value: T, index: number, array: T[]) => T): void;
/**
* 过滤数组
*/
filter(callback: (value: T, index: number, array: T[]) => boolean): void;
/**
* 校验输入的值是否为数组
*
* @throws TypeError
*/
private checkIsArray;
}
export {
scroll$1 as scroll,
};
export {};