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

Custom functions do not work as expected on Firefox #2052

Open
vizmoe opened this issue Nov 16, 2023 · 2 comments
Open

Custom functions do not work as expected on Firefox #2052

vizmoe opened this issue Nov 16, 2023 · 2 comments

Comments

@vizmoe
Copy link

vizmoe commented Nov 16, 2023

Prelude

No.

  • Have you searched your problem in issues?

Yes.

Error details

I wrote a function to return clean URLs, which removes unnecessary query parameters outside the whitelist. However, for some reason, it does not work properly on Surfingkeys. I have verified it on JavaScript Playground and it should be able to work correctly.

SurfingKeys: 1.15.0

Browser: Firefox 119.0.1 (64-bit)
URL: hhttps://www.google.com/search?q=surfingkeys

Content

const queryParamWhitelistMap = {
  "www.youtube.com": ["v", "t"],
  "news.ycombinator.com": ["id"],
};

function _getCleanUrl(url) {
  // Parse the URL
  const urlObj = new URL(url);

  // Get the hostname and corresponding query parameter whitelist
  const queryParamWhitelist = queryParamWhitelistMap[urlObj.hostname] || [];

  // Use Array.filter to keep only the whitelisted query parameters
  Array.from(urlObj.searchParams.keys())
    .filter((key) => !queryParamWhitelist.includes(key))
    .forEach((key) => urlObj.searchParams.delete(key));

  // Return the modified URL as a string
  return urlObj.toString();
}

api.mapkey("yU", "Copy current tab as a clean link", function () {
  api.Clipboard.write(_getCleanUrl(window.location.href));
});
@eugercek
Copy link
Contributor

Works on Brave, btw.

@vizmoe vizmoe changed the title Custom functions do not work as expected. Custom functions do not work as expected on Firefox Nov 17, 2023
@vizmoe
Copy link
Author

vizmoe commented Nov 17, 2023

Thank you for your reply. I just tested it on Google Chrome and it works fine, but it just doesn't work on Firefox. As an alternative, I wrote a longer but more compatible replacement function that can work properly on Firefox.

function _getCleanUrl(url) {
  var a = document.createElement('a');
  a.href = url;

  var queryParamWhitelist = queryParamWhitelistMap[a.hostname] || [];
  var searchParams = a.search.slice(1).split('&');
  var filteredSearchParams = [];

  for (var i = 0; i < searchParams.length; i++) {
    var keyValuePair = searchParams[i].split('=');
    var key = keyValuePair[0];

    if (queryParamWhitelist.indexOf(key) !== -1) {
      filteredSearchParams.push(searchParams[i]);
    }
  }

  a.search = filteredSearchParams.length > 0 ? '?' + filteredSearchParams.join('&') : '';
  return a.href;
}

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