Saurabh Agarwal
Saurabh Agarwal

Reputation: 527

What may be an efficient way to judge whether a user stays on the same domain while browsing (using a Firefox extension)?

While browsing a website, say www.example.com, it is possible that the user enters some subdomain say www.sub.example.com

But the base webdomain remains the same.

In my firefox extension, I need to develop a "session" which remains active until a user remains on the same domain.

A simple solution would be to extract the domain from the url first. Then split the string with "." as token. Thus, we get sub and example for the second case and just example for the second case. I can then compare both. If any are equal, I deduce that the domains are same.

Though this might work, this seems more like a hack and might be prone to false positives/negatives.

Is there a more efficient / cleaner solution to the same problem?

Info: I am using GWT to build the extension

Upvotes: 0

Views: 43

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57681

You should use nsIEffectiveTLDService, it will handle things like sub.example.co.uk correctly. You can pass an nsIURI instance to nsIEffectiveTLDService.getBaseDomain(), aAdditionalParts parameter should be zero. For http://sub.example.co.uk/foo you will get example.co.uk back - the actual "domain" part. Then you can simply compare the domain names for two URLs.

Upvotes: 2

Related Questions