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

Exposing parameter types for standalone listeners outside of addListener #72

Open
tophf opened this issue May 28, 2024 · 1 comment
Open

Comments

@tophf
Copy link

tophf commented May 28, 2024

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) { ...... }
@tophf
Copy link
Author

tophf commented May 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant