Surfingkeys
Surfingkeys copied to clipboard
Custom functions do not work as expected on Firefox
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));
});
Works on Brave, btw.
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;
}