includes function is too broad for URL matching
Closed this issue · 7 comments
Some URLs may include the addresses of blocked sites, despite being separate sites. For example, I was accessing a YouTube video via an RSS reader, and the tab was closed. Arguably this is intended behaviour, but I think it's too strict.
Instead, maybe match window.location.host
to the restricted sites, instead of using includes
?
Will be implemented in next version! (Definitely a great suggestion, Thanks!)
I have already checked on window.location.host
inside background.js
and during development it returns a really strange value, like gipebbenhibhdifokjmpdnpeigjfgopa (probably running through a local proxy).
I understand your idea and will make it work! Not based on window.location.host
but will check the host as needed.
After then. Opening addresses like:
https://my-website.com/something/facebook.com
Will work!
Actually. There is already a way how to solve it right now! :)))
In Options, you may define more specific domains, like:
https://web.facebook.com
https://instagram.com
https://www.instagram.com
https://www.reddit.com
And so on!
I will try to make it more intelligent though. To assume http(s), www, no-www, and to not look beyond the first slash.
Challenge accepted!
Update:
And if there is NO http(s), www, no-www, present in the URL, I'll make sure it works both ways :))
In that case, a combination of options like this:
https://facebook.com
instagram.com
Will also work! :)
I think I'll make it easier and just look at the beggining of the string, right around the //
Simply as: * // * your blocked address
That's it! :D
A RegExp is probably best: https://stackoverflow.com/a/32730577/4979742
Actually found a very best solution: (no need for regex)
Here it is:
// 1. You get the url from the tab
var url = tab.url;
// 2. This url is tricky! It has some flow of updates.
// It can be URL from Chrome Extensions.
// Some fancy other url with no www, http or whats-ever (basically not a valid URL)!
// And even, it can get to be "chrome://newtab" !!!
// 3. But no worries, you make a simple filter
if (url.startsWith("http") === false) {
return;
}
// 4. Then, you ARE actually able to get a hostname (URL is valid at this point)
function getDomain(myUrl) {
var url = new URL(myUrl);
var hostname = url.hostname;
return hostname;
}
// 5. Lastly, you just compare the domains
getDomain(url).includes(blockedItem);
:)
Hi @ItsNickBarry,
It is now resolved. Releasing version 3.0.