Interface VMScriptGMObjectVMExtensions

Aliases for GM_ methods that are not included in Greasemonkey4 API

interface VMScriptGMObjectVMExtensions {
    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;
    deleteValues: (names: string[]) => Promise<void>;
    download:
        | (options: VMScriptGMDownloadOptions) => void | Promise<Blob>
        | (url: string, name: string) => void | Promise<Blob>;
    getResourceText: (name: string) => string;
    getValues:
        | (names: string[]) => Promise<GenericObject>
        | (namesWithDefaults: GenericObject) => Promise<GenericObject>;
    log: (...args: any) => void;
    removeValueChangeListener: (listenerId: string) => void;
    setValues: (values: GenericObject) => Promise<void>;
    unregisterMenuCommand: (caption: string) => void;
}

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

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

getValues:
    | (names: string[]) => Promise<GenericObject>
    | (namesWithDefaults: GenericObject) => Promise<GenericObject>

VM2.19.1

log: (...args: any) => void

Type declaration

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

      Parameters

      • ...args: any

      Returns void

removeValueChangeListener: (listenerId: string) => void

Type declaration

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

      Parameters

      • listenerId: string

      Returns 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