From 9f40fc3d0bcbab0c4ad34cdba90c226b44a1345e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 7 Sep 2021 23:52:11 +0200 Subject: background: Build wildcard domains from a hostname This will give us a list of filenames that we should look for to insert CSS on the page. --- background.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/background.js b/background.js index c898d8c..7a884a1 100644 --- a/background.js +++ b/background.js @@ -33,3 +33,37 @@ browser.runtime.onMessage.addListener(function(message) { } }); }); + + +// function insert_css (hostname) { +function wildcard_domains (hostname) { + var domain_parts = hostname.split('.'); + var domains = []; + + // while (domain_parts.length > 0) { + // domains.unshift(domain_parts.pop()); + // + // var s = domains.join('.'); + // } + + for (var i = domain_parts.length - 1; i >= 0; i--) { + var domain; + + if (domains[domains.length - 1]) { + var domain = domain_parts[i] + '.' + domains[domains.length - 1]; + } + else { + var domain = domain_parts[i]; + } + + domains.push(domain); + } + + for (var i = 0; i < domains.length - 1; i++) { + domains[i] = '*.' + domains[i]; + } + + domains.unshift('*'); + + return domains; +} -- cgit v1.2.3 From d759fde7f33da1b93ed3571ab254acefa0f0fdc9 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 7 Sep 2021 23:55:56 +0200 Subject: background: Describe the `wildcard_domains` function --- background.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/background.js b/background.js index 7a884a1..7553a30 100644 --- a/background.js +++ b/background.js @@ -35,7 +35,12 @@ browser.runtime.onMessage.addListener(function(message) { }); -// function insert_css (hostname) { +// Build a list of wildcard domains from the given hostname. +// +// Example: +// +// wildcard_domains('en.wikipedia.org'); +// => [ '*', '*.org', '*.wikipedia.org', 'en.wikipedia.org' ] function wildcard_domains (hostname) { var domain_parts = hostname.split('.'); var domains = []; -- cgit v1.2.3 From de5f4ccd88076be4e532b4d0137d8404d458dcd7 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 7 Sep 2021 23:56:38 +0200 Subject: wildcard_domains: Remove in-progress code --- background.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/background.js b/background.js index 7553a30..b187850 100644 --- a/background.js +++ b/background.js @@ -45,12 +45,6 @@ function wildcard_domains (hostname) { var domain_parts = hostname.split('.'); var domains = []; - // while (domain_parts.length > 0) { - // domains.unshift(domain_parts.pop()); - // - // var s = domains.join('.'); - // } - for (var i = domain_parts.length - 1; i >= 0; i--) { var domain; -- cgit v1.2.3 From 1cc24417e2f8a6420503735c10577f57c9b4e323 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 8 Sep 2021 00:09:59 +0200 Subject: background: Apply CSS from all files for wildcard domains For the hostname "en.wikipedia.org", all of the following CSS files are inserted: * `*.css` * `*.org.css` * `*.wikipedia.org.css` * `en.wikipedia.org.css` --- background.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/background.js b/background.js index b187850..2b5dc1d 100644 --- a/background.js +++ b/background.js @@ -26,10 +26,13 @@ browser.runtime.onMessage.addListener(function(message) { var url = new URL(details.url); if (url.hostname === message.domain) { - browser.tabs.insertCSS( - details.tabId, - { file: '/css/' + message.domain + '.css' } - ); + wildcard_domains(message.domain) + .forEach(function(domain) { + browser.tabs.insertCSS( + details.tabId, + { file: '/css/' + domain + '.css' } + ); + }); } }); }); -- cgit v1.2.3 From 452afd6544088c92e1b460808764813632318718 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 8 Sep 2021 00:14:15 +0200 Subject: wildcard_domains: Add TODO for different wildcard character --- background.js | 1 + 1 file changed, 1 insertion(+) diff --git a/background.js b/background.js index 2b5dc1d..beee8e0 100644 --- a/background.js +++ b/background.js @@ -38,6 +38,7 @@ browser.runtime.onMessage.addListener(function(message) { }); +// TODO: Consider choosing a different wildcard character, as '*' is troublesome in a shell (maybe '%') // Build a list of wildcard domains from the given hostname. // // Example: -- cgit v1.2.3 From 87e585c3cb9ff18cc34159d19645f3df887d2f32 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 8 Sep 2021 21:26:51 +0200 Subject: Add TODO --- TODO | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 TODO diff --git a/TODO b/TODO new file mode 100644 index 0000000..6ff2ed7 --- /dev/null +++ b/TODO @@ -0,0 +1,5 @@ +TODO + +2018.08.03: +- Make extension icon grey when no CSS file activated, glow when a CSS file was + found. -- cgit v1.2.3 From e45127b404ce194e389b447b68a33957ad9f3492 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 8 Sep 2021 21:34:00 +0200 Subject: wildcard_domains: Change wildcard character to '%' The '*' character is not ideal to use in filenames because if you forget to escape it in the shell, it can have harmful consequences. --- background.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/background.js b/background.js index beee8e0..5bde79a 100644 --- a/background.js +++ b/background.js @@ -38,13 +38,12 @@ browser.runtime.onMessage.addListener(function(message) { }); -// TODO: Consider choosing a different wildcard character, as '*' is troublesome in a shell (maybe '%') // Build a list of wildcard domains from the given hostname. // // Example: // // wildcard_domains('en.wikipedia.org'); -// => [ '*', '*.org', '*.wikipedia.org', 'en.wikipedia.org' ] +// => [ '%', '%.org', '%.wikipedia.org', 'en.wikipedia.org' ] function wildcard_domains (hostname) { var domain_parts = hostname.split('.'); var domains = []; @@ -63,10 +62,10 @@ function wildcard_domains (hostname) { } for (var i = 0; i < domains.length - 1; i++) { - domains[i] = '*.' + domains[i]; + domains[i] = '%.' + domains[i]; } - domains.unshift('*'); + domains.unshift('%'); return domains; } -- cgit v1.2.3