Smile4ever / firefoxaddons

Extend the functionality of Firefox with cool addons
82 stars 20 forks source link

[Neat Url] Don't catch url with # #83

Closed AntoineLemaire closed 6 years ago

AntoineLemaire commented 6 years ago

I noticed on a link redirect link from Facebook where Neat Url did not clean it:

www.cuisineactuelle.fr/recettes/mini-burgers-au-foie-gras-331523#utm_source=Facebook&utm_medium=social&utm_campaign=PassionApero

It seams that's the # instead of ? which introduce this behavior

Smile4ever commented 6 years ago

I will look what I can do.

kafene commented 6 years ago

I think I have a related issue

I encounted this url: https://vivaldi.com/blog/teamblog/vivaldi-1-13-adds-window-panel/#pk_campaign=newsletter&pk_kwd=community

I want to filter #pk_campaign and #pk_kwd

I see in the source code it does build a list of hashParams but only replaces the whole thing if it matches.

I came up with a simplistic implementation using a secondary URL object to track the hash as if it were a query string, which is an approach that many sites use.

(function () {
    const url = new URL('https://vivaldi.com/blog/teamblog/vivaldi-1-13-adds-window-panel/?removethis=true#pk_campaign=newsletter&pk_kwd=community');
    const fields = ['#pk_campaign', '#pk_kwd', 'removethis'];

    const hashUrl = new URL(url.href);
    hashUrl.search = hashUrl.hash.replace('#', '');
    hashUrl.hash = '';

    for (const field of fields) {
        if (field.startsWith('#')) {
            const fieldName = field.replace('#', '');
            hashUrl.searchParams.delete(fieldName);
        } else if (url.searchParams.has(field)) {
            url.searchParams.delete(field);
        }
    }

    const newHash = hashUrl.search.replace('?', '');
    url.hash = newHash ? `#${newHash}` : '';

    return url.href;
})();

// returns 'https://vivaldi.com/blog/teamblog/vivaldi-1-13-adds-window-panel/'

Perhaps this sort of replacement needs a special notation, like #&pk_campaign to indicate that it's both a hash string and the item is to be treated as a parameter within that string.

Anyways, thank you for your hard work with Neat URL!

Smile4ever commented 6 years ago

Thank you for your hard work. It is indeed a simplistic implementation but it shows the intent. Maybe I can use the code with some changes. Please be patient as this will take some time to adapt for Neat URL.

Smile4ever commented 6 years ago

Implemented in Neat URL 4.0.0 which is now available on addons.mozilla.org.

Geobert commented 6 years ago

This is great! Should update the documentation though: we still refer #xtor=RSS-8 as valid but it doesn't work anymore, I needed to change it to #?xtor