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

removeQueryParameters fails in Firefox content scripts: Error URLSearchParams.keys() is not a method #167

Closed
akindyakov opened this issue Jul 15, 2022 · 4 comments

Comments

@akindyakov
Copy link

The option removeQueryParameters of normalizeUrl with a list of RegExps raises an error of "Error: o.searchParams.keys() is not iterable" only in Firefox content scripts.

I compile code from TypeScript, the code snipet with usage of normalizeUrl is following:

normalizeUrl('https://github.com/sindresorhus/normalize-url/issues/', {
   forceHttps: true,
   normalizeProtocol: true,
   removeTrailingSlash: true,
   removeQueryParameters: [/^utm_\w+/i, /^itm_\w+/i],
   sortQueryParameters: true,
   stripAuthentication: true,
   stripHash: true,
   stripProtocol: false,
   stripTextFragment: true,
   stripWWW: true,
})
  • Version: 7.0.3

  • tsconfig.compilerOptions:

    • lib: ["ES2020","dom","dom.iterable"]
    • target: es6
    • module: ES2020
    • moduleResolution: node
    • allowJs: true

    Update: if rewrite related code to use urlObject.searchParams.forEach it works just fine.

@sindresorhus
Copy link
Owner

This has nothing to do specifically with this package. It's either a TS config issue or a Firefox issue.

@sindresorhus sindresorhus closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2022
@akindyakov
Copy link
Author

This has nothing to do specifically with this package. It's either a TS config issue or a Firefox issue

Thanks for the update!

Do you think it doesn't make sense to use .forEach instead? It would be more transferable and even cheaper for URLs with many query params -

  • Complexity O(n), where n is a number query parameters. Same as before.
  • Memory O(k), where k is a number of query parameters to delete. k ≪ n. Now it's O(n).

I can make a PR to fix it. It can be something like this:

  if (Array.isArray(removeQueryParameters)) {
    const keysToDelete: string[] = []
    urlObject.searchParams.forEach((_value: string, key: string) => {
      if (testParameter(key, removeQueryParameters)) {
        keysToDelete.push(key)
      }
    })
    keysToDelete.forEach((key: string) => urlObject.searchParams.delete(key))
  }

What do you think @sindresorhus ?

@sindresorhus
Copy link
Owner

Do you think it doesn't make sense to use .forEach instead?

I'm not interested in using .forEach. I prefer for-of.

@akindyakov
Copy link
Author

Sure, thanks for the information then!

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

2 participants