Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
Show the grey icon for whitelisted sites with a port number (#19294)
Browse files Browse the repository at this point in the history
* Update background.js
* Update getNormalisedHostname to handle port numbers

Co-authored-by: Chan Chak Shing <[email protected]>
  • Loading branch information
cschanaj and Chan Chak Shing authored Nov 16, 2020
1 parent 0242bd1 commit acc5b60
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 3 additions & 2 deletions chromium/background-scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,11 @@ function updateState () {
return;
}

// tabUrl.host instead of hostname should be used to show the "disabled" status properly (#19293)
const tabUrl = new URL(tabs[0].url);
const hostname = util.getNormalisedHostname(tabUrl.hostname);
const host = util.getNormalisedHostname(tabUrl.host);

if (isExtensionDisabledOnSite(hostname) || iconState == "disabled") {
if (isExtensionDisabledOnSite(host) || iconState == "disabled") {
if ('setIcon' in chrome.browserAction) {
chrome.browserAction.setIcon({
path: {
Expand Down
7 changes: 6 additions & 1 deletion chromium/background-scripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,16 @@ function loadExtensionFile(url, returnType) {

/**
* Remove tailing dots from hostname, e.g. "www.example.com."
* Preserve port numbers if they are used
*/
function getNormalisedHostname(hostname) {
function getNormalisedHostname(host) {
let [ hostname, port ] = host.split(":");
while (hostname && hostname[hostname.length - 1] === '.' && hostname !== '.') {
hostname = hostname.slice(0, -1);
}
if (port) {
return `${hostname}:${port}`;
}
return hostname;
}

Expand Down
9 changes: 8 additions & 1 deletion chromium/test/util_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ describe('util.js', function() {
});

describe('getNormalisedHostname', function() {
it('removes tailing dots', function() {
it('preserve port numbers', function() {
assert.strictEqual(util.getNormalisedHostname('example.com'), 'example.com');
assert.strictEqual(util.getNormalisedHostname('example.com:8080'), 'example.com:8080');
});

it('removes tailing dots and preserve port numbers', function() {
assert.strictEqual(util.getNormalisedHostname('example.com.'), 'example.com');
assert.strictEqual(util.getNormalisedHostname('example.com.:8080'), 'example.com:8080');
assert.strictEqual(util.getNormalisedHostname('example.com..'), 'example.com');
assert.strictEqual(util.getNormalisedHostname('example.com..:8080'), 'example.com:8080');
});

it('preserves a single dot', function() {
Expand Down

0 comments on commit acc5b60

Please sign in to comment.