Interface VMScriptGMObject

interface VMScriptGMObject {
    addElement: {
        (tagName: string, attributes?: Record<string, string>): HTMLElement;
        (
            parentNode: HTMLElement,
            tagName: string,
            attributes?: Record<string, string>,
        ): HTMLElement;
    };
    addStyle: (css: string) => HTMLStyleElement;
    addValueChangeListener: <T>(
        name: string,
        callback: VMScriptGMValueChangeCallback<T>,
    ) => string;
    deleteValue: (name: string) => Promise<void>;
    deleteValues: (names: string[]) => Promise<void>;
    download:
        | (options: VMScriptGMDownloadOptions) => void | Promise<Blob>
        | (url: string, name: string) => void | Promise<Blob>;
    getResourceText: (name: string) => string;
    getResourceUrl: (name: string, isBlobUrl?: boolean) => Promise<string>;
    getValue: <T>(name: string, defaultValue?: T) => Promise<T>;
    getValues:
        | (names: string[]) => Promise<GenericObject>
        | (namesWithDefaults: GenericObject) => Promise<GenericObject>;
    info: VMScriptGMInfoObject;
    listValues: () => Promise<string[]>;
    log: (...args: any) => void;
    notification: {
        (options: VMScriptGMNotificationOptions): VMScriptGMNotificationControl;
        (
            text: string,
            title?: string,
            image?: string,
            onclick?: () => void,
        ): VMScriptGMNotificationControl;
    };
    openInTab: {
        (url: string, options?: VMScriptGMTabOptions): VMScriptGMTabControl;
        (url: string, openInBackground?: boolean): VMScriptGMTabControl;
    };
    registerMenuCommand: (
        caption: string,
        onClick: (event: MouseEvent | KeyboardEvent) => void,
        options?: { autoClose?: boolean; id?: string; title?: string },
    ) => string;
    removeValueChangeListener: (listenerId: string) => void;
    setClipboard: (data: string, type?: string) => void;
    setValue: <T>(name: string, value: T) => Promise<void>;
    setValues: (values: GenericObject) => Promise<void>;
    unregisterMenuCommand: (caption: string) => void;
    unsafeWindow: Window;
    xmlHttpRequest: <T = string | object | ArrayBuffer | Blob | Document>(
        details: VMScriptGMXHRDetails<T>,
    ) => Promise<T> & VMScriptXHRControl;
}

Hierarchy (View Summary)

Properties

addElement: {
    (tagName: string, attributes?: Record<string, string>): HTMLElement;
    (
        parentNode: HTMLElement,
        tagName: string,
        attributes?: Record<string, string>,
    ): HTMLElement;
}

Type declaration

    • (tagName: string, attributes?: Record<string, string>): HTMLElement
    • Appends and returns an element with the specified attributes.

      Examples:

      // using a private function in `onload`
      let el = GM_addElement('script', { src: 'https://....' });
      el.onload = () => console.log('loaded', el);

      // same as GM_addStyle('a { color:red }')
      let el = GM_addElement('style', { textContent: 'a { color:red }' });

      // appending to an arbitrary node
      let el = GM_addElement(parentElement.shadowRoot, 'iframe', { src: url });

      Parameters

      • tagName: string

        A tag name like script. Any valid HTML tag can be used, but the only motivation for this API was to add script, link, style elements when they are disallowed by a strict Content-Security-Policy of the site e.g. github.com, twitter.com.

      • Optionalattributes: Record<string, string>

        The keys are HTML attributes, not DOM properties, except textContent which sets DOM property textContent. The values are strings so if you want to assign a private function to onload you can do it after the element is created.

      Returns HTMLElement

    • (
          parentNode: HTMLElement,
          tagName: string,
          attributes?: Record<string, string>,
      ): HTMLElement
    • Appends and returns an element with the specified attributes.

      Examples:

      // using a private function in `onload`
      let el = GM_addElement('script', { src: 'https://....' });
      el.onload = () => console.log('loaded', el);

      // same as GM_addStyle('a { color:red }')
      let el = GM_addElement('style', { textContent: 'a { color:red }' });

      // appending to an arbitrary node
      let el = GM_addElement(parentElement.shadowRoot, 'iframe', { src: url });

      Parameters

      • parentNode: HTMLElement

        The parent node to which the new node will be appended. It can be inside ShadowDOM: someElement.shadowRoot. When omitted, it'll be determined automatically:

        • document.head (<head>) for script, link, style, meta tags.
        • document.body (<body>) for other tags or when there's no <head>.
        • document.documentElement (<html> or an XML root node) otherwise.
      • tagName: string

        A tag name like script. Any valid HTML tag can be used, but the only motivation for this API was to add script, link, style elements when they are disallowed by a strict Content-Security-Policy of the site e.g. github.com, twitter.com.

      • Optionalattributes: Record<string, string>

        The keys are HTML attributes, not DOM properties, except textContent which sets DOM property textContent. The values are strings so if you want to assign a private function to onload you can do it after the element is created.

      Returns HTMLElement

addStyle: (css: string) => HTMLStyleElement

Type declaration

    • (css: string): HTMLStyleElement
    • Appends and returns a <style> element with the specified CSS.

      Parameters

      • css: string

      Returns HTMLStyleElement

addValueChangeListener: <T>(
    name: string,
    callback: VMScriptGMValueChangeCallback<T>,
) => string

Type declaration

deleteValue: (name: string) => Promise<void>
deleteValues: (names: string[]) => Promise<void>

VM2.19.1

download:
    | (options: VMScriptGMDownloadOptions) => void | Promise<Blob>
    | (url: string, name: string) => void | Promise<Blob>
getResourceText: (name: string) => string

Type declaration

    • (name: string): string
    • Retrieves a text resource from the Metadata Block.

      Parameters

      • name: string

        Name of a resource defined in the Metadata Block.

      Returns string

getResourceUrl: (name: string, isBlobUrl?: boolean) => Promise<string>
getValue: <T>(name: string, defaultValue?: T) => Promise<T>
getValues:
    | (names: string[]) => Promise<GenericObject>
    | (namesWithDefaults: GenericObject) => Promise<GenericObject>

VM2.19.1

listValues: () => Promise<string[]>
log: (...args: any) => void

Type declaration

    • (...args: any): void
    • The original console.log

      Parameters

      • ...args: any

      Returns void

notification: {
    (options: VMScriptGMNotificationOptions): VMScriptGMNotificationControl;
    (
        text: string,
        title?: string,
        image?: string,
        onclick?: () => void,
    ): VMScriptGMNotificationControl;
}

Type declaration

openInTab: {
    (url: string, options?: VMScriptGMTabOptions): VMScriptGMTabControl;
    (url: string, openInBackground?: boolean): VMScriptGMTabControl;
}

Type declaration

    • (url: string, options?: VMScriptGMTabOptions): VMScriptGMTabControl
    • Opens URL in a new tab.

      Parameters

      • url: string

        The URL to open in a new tab. URL relative to current page is also allowed. Note: Firefox does not support data URLs.

      • Optionaloptions: VMScriptGMTabOptions

      Returns VMScriptGMTabControl

    • (url: string, openInBackground?: boolean): VMScriptGMTabControl
    • Opens URL in a new tab.

      Parameters

      • url: string

        The URL to open in a new tab. URL relative to current page is also allowed. Note: Firefox does not support data URLs.

      • OptionalopenInBackground: boolean

        Open the tab in background. Note, this is a reverse of the first usage method so for example true is the same as { active: false }.

      Returns VMScriptGMTabControl

registerMenuCommand: (
    caption: string,
    onClick: (event: MouseEvent | KeyboardEvent) => void,
    options?: { autoClose?: boolean; id?: string; title?: string },
) => string

Type declaration

    • (
          caption: string,
          onClick: (event: MouseEvent | KeyboardEvent) => void,
          options?: { autoClose?: boolean; id?: string; title?: string },
      ): string
    • Registers a command in Violentmonkey popup menu. Returns the command's id since VM2.12.5, see description of the id parameter. If you want to add a shortcut, please see @violentmonkey/shortcut.

      Parameters

      • caption: string

        The name to show in the popup menu.

      • onClick: (event: MouseEvent | KeyboardEvent) => void

        Callback function when the command is clicked in the menu.

      • Optionaloptions: { autoClose?: boolean; id?: string; title?: string }

        VM2.15.9

        • OptionalautoClose?: boolean

          Default: true. Whether to auto-close the popup after the user invoked the command.

        • Optionalid?: string

          Default: the caption parameter. In 2.15.9-2.16.1 the default was a randomly generated string.

        • Optionaltitle?: string

          A hint shown in the status bar when hovering the command.

      Returns string

removeValueChangeListener: (listenerId: string) => void

Type declaration

    • (listenerId: string): void
    • Removes a change listener by its ID.

      Parameters

      • listenerId: string

      Returns void

setClipboard: (data: string, type?: string) => void

Type declaration

    • (data: string, type?: string): void
    • Sets data to system clipboard.

      Parameters

      • data: string

        The data to be copied to system clipboard.

      • Optionaltype: string

        The MIME type of data to copy. Default as text/plain.

      Returns void

setValue: <T>(name: string, value: T) => Promise<void>
setValues: (values: GenericObject) => Promise<void>

VM2.19.1

unregisterMenuCommand: (caption: string) => void

Type declaration

    • (caption: string): void
    • Unregisters a command which has been registered to Violentmonkey popup menu.

      Parameters

      • caption: string

        The name of command to unregister.

      Returns void

unsafeWindow: Window
xmlHttpRequest: <T = string | object | ArrayBuffer | Blob | Document>(
    details: VMScriptGMXHRDetails<T>,
) => Promise<T> & VMScriptXHRControl