GoogleChrome / chrome-types

Code to parse Chrome's internal extension type definitions—published on NPM as chrome-types

Home Page:https://www.npmjs.com/package/chrome-types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exposing parameter types for standalone listeners outside of addListener

tophf opened this issue · comments

Currently the parameters for addListener are inlined, i.e. types/hints are available only in an inline listener:

chrome.webNavigation.onHistoryStateUpdated.addListener(info => { ...... });

but we can't use types in a standalone listener function outside of addListener (e.g. to toggle it somewhere else):

chrome.webNavigation.onHistoryStateUpdated.addListener(onNav);
function onNav(info) { ...... }

Here's the current generated code for reference:

spoiler
    export const onHistoryStateUpdated: CustomChromeEvent<(
      callback: (
        details: {

          /**
           * The ID of the tab in which the navigation occurs.
           */
          tabId: number,

          url: string,

          /**
           * The ID of the process that runs the renderer for this frame.
           */
          processId: number,

          /**
           * 0 indicates the navigation happens in the tab content window; a positive value indicates navigation in a subframe. Frame IDs are unique within a tab.
           */
          frameId: number,

          /**
           * The ID of the parent frame, or `-1` if this is the main frame.
           *
           * @since Chrome 74
           */
          parentFrameId: number,

          /**
           * Cause of the navigation.
           */
          transitionType: TransitionType,

          /**
           * A list of transition qualifiers.
           */
          transitionQualifiers: TransitionQualifier[],

          /**
           * The time when the navigation was committed, in milliseconds since the epoch.
           */
          timeStamp: number,

          /**
           * A UUID of the document loaded.
           *
           * @since Chrome 106
           */
          documentId: string,

          /**
           * A UUID of the parent document owning this frame. This is not set if there is no parent.
           *
           * @since Chrome 106
           */
          parentDocumentId?: string,

          /**
           * The lifecycle the document is in.
           *
           * @since Chrome 106
           */
          documentLifecycle: extensionTypes.DocumentLifecycle,

          /**
           * The type of frame the navigation occurred in.
           *
           * @since Chrome 106
           */
          frameType: extensionTypes.FrameType,
        },
      ) => void,
      filters?: {

        /**
         * Conditions that the URL being navigated to must satisfy. The 'schemes' and 'ports' fields of UrlFilter are ignored for this event.
         */
        url: events.UrlFilter[],
      },
    ) => void>;

Suggesting to auto-generate a dummy type for each object parameter:

export const onHistoryStateUpdated: CustomChromeEvent<(
  callback: (details: OnHistoryStateUpdatedDetails) => void,
  filters?: OnHistoryStateUpdatedFilters,
) => void>;

These dummy types would be also exported, so we can use them in typescript:

chrome.webNavigation.onHistoryStateUpdated.addListener(onNav);
function onNav(info: chrome.webNavigation.OnHistoryStateUpdatedDetails) { ...... }

as well as in jsdoc:

chrome.webNavigation.onHistoryStateUpdated.addListener(onNav);
/** @param {chrome.webNavigation.OnHistoryStateUpdatedDetails} info */
function onNav(info) { ...... }

For comparison, here's how it's implemented in DefinitelyTyped/chrome: link.