Skip to content

Commit

Permalink
Apply pagemods to sites with ports consistently
Browse files Browse the repository at this point in the history
The initial load would apply files in 'example.org' if you visit
'example.org:8080', but a reload wouldn't reapply files from the later
directory. For consistency and to support both possible choices on that
spectrum 'example.org' files will be applied to visits and reloads
regardless of a portnumber present or not. If you want files to apply
only to hostnames without a port you can use a portspecific directory
with the portnumber being the default for the used protocol.

Test is adapted and the README has a cheatsheet entry now about the
possible naming of directories (not) involving ports. Its not THAT
common in practice as it took me 2 years to notice…
  • Loading branch information
DonKult committed Jul 1, 2018
1 parent a6e9f0f commit 053c629
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ and [decode](https://unix.stackexchange.com/questions/159253/decoding-url-encodi
The URI needs to be `url(data:image/TYPE;base64,ENCODED)`.
Encoding can be done with `base64 -w 0 < image.file`.

### URL to directory resolution

* `http://dotpagemod.example.com:8080/` => `ALL`, `ALL_http`, `com`, `com_8080`, `example.com`, `example.com_8080`, `dotpagemod.example.com`, `dotpagemod.example.com_8080`
* `http://dot.page.mod.example.com/` => `ALL`, `ALL_http`, `com`, `com_80`, `example.com`, `example.com_80`, `dotpagemod.example.com`, `dotpagemod.example.com_80`
* `https://dot.page.mod.example.com/` => `ALL`, `ALL_https`, `com`, `com_443`, `example.com`, `example.com_443`, `dotpagemod.example.com`, `dotpagemod.example.com_443`
* `ftp://dot.page.mod.example.com/` => `ALL_ftp`, `com`, `com_21`, `example.com`, `example.com_21`, `dotpagemod.example.com`, `dotpagemod.example.com_21`

## Examples

Websites like [OpenUserJS](https://openuserjs.org/), [Greasy
Expand Down
28 changes: 21 additions & 7 deletions background/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,31 @@ const getHostDirsFromURI = url => {
if (u[0] === 'http:' || u[0] === 'https:' || u[0] === 'ftp:') {
// this builds an array like [ ALL, com, example.com, foo.example.com ]
let preset;
if (u[0] === 'ftp:')
let preset_port;
if (u[0] === 'ftp:') {
preset = [ 'ALL_ftp' ];
else if (u[0] === 'https:')
preset_port = 21;
} else if (u[0] === 'https:') {
preset = [ 'ALL', 'ALL_https' ];
else
preset_port = 443;
} else {
preset = [ 'ALL', 'ALL_http' ];
Array.prototype.push.apply(pros, u[2].split('.').reverse().reduce((a,v,i) => {
preset_port = 80;
}
let hostname_port = u[2].split(':');
let hostname = hostname_port[0];
let port;
if (hostname_port.length === 1)
port = preset_port;
else
port = hostname_port[1];
Array.prototype.push.apply(pros, hostname.split('.').reverse().reduce((a,v,i) => {
if (i === 0)
a.push(v);
else
a.push([v,a[a.length - 1]].join('.'));
a.push(v, [v, port].join(':'));
else {
let toplevelhost = [v,a[a.length - 2]].join('.');
a.push(toplevelhost, [toplevelhost, port].join(':'));
}
return a;
}, preset));
} else if (u[0] === 'file:')
Expand Down
30 changes: 15 additions & 15 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@ describe('Utils', () => {
describe('getHostDirsFromURI', () => {
it('localhost without port', () => {
let hosts = getHostDirsFromURI('http://localhost/');
expect(hosts).toEqual(['ALL', 'ALL_http', 'localhost']);
expect(hosts).toEqual(['ALL', 'ALL_http', 'localhost', 'localhost:80']);
hosts = getHostDirsFromURI('https://localhost/');
expect(hosts).toEqual(['ALL', 'ALL_https', 'localhost']);
expect(hosts).toEqual(['ALL', 'ALL_https', 'localhost', 'localhost:443']);
hosts = getHostDirsFromURI('ftp://localhost/');
expect(hosts).toEqual(['ALL_ftp', 'localhost']);
expect(hosts).toEqual(['ALL_ftp', 'localhost', 'localhost:21']);
});
it('localhost with port', () => {
let hosts = getHostDirsFromURI('http://localhost:8080/');
expect(hosts).toEqual(['ALL', 'ALL_http', 'localhost:8080']);
expect(hosts).toEqual(['ALL', 'ALL_http', 'localhost', 'localhost:8080']);
hosts = getHostDirsFromURI('https://localhost:8080/');
expect(hosts).toEqual(['ALL', 'ALL_https', 'localhost:8080']);
expect(hosts).toEqual(['ALL', 'ALL_https', 'localhost', 'localhost:8080']);
hosts = getHostDirsFromURI('ftp://localhost:8080/');
expect(hosts).toEqual(['ALL_ftp', 'localhost:8080']);
expect(hosts).toEqual(['ALL_ftp', 'localhost', 'localhost:8080']);
});
it('example.org without port', () => {
let hosts = getHostDirsFromURI('http://example.org/');
expect(hosts).toEqual(['ALL', 'ALL_http', 'org', 'example.org']);
expect(hosts).toEqual(['ALL', 'ALL_http', 'org', 'org:80', 'example.org', 'example.org:80']);
hosts = getHostDirsFromURI('https://example.org/');
expect(hosts).toEqual(['ALL', 'ALL_https', 'org', 'example.org']);
expect(hosts).toEqual(['ALL', 'ALL_https', 'org', 'org:443', 'example.org', 'example.org:443']);
hosts = getHostDirsFromURI('ftp://example.org/');
expect(hosts).toEqual(['ALL_ftp', 'org', 'example.org']);
expect(hosts).toEqual(['ALL_ftp', 'org', 'org:21', 'example.org', 'example.org:21']);
});
it('example.org with port', () => {
let hosts = getHostDirsFromURI('http://example.org:8080/');
expect(hosts).toEqual(['ALL', 'ALL_http', 'org:8080', 'example.org:8080']);
expect(hosts).toEqual(['ALL', 'ALL_http', 'org', 'org:8080', 'example.org', 'example.org:8080']);
hosts = getHostDirsFromURI('https://example.org:8080/');
expect(hosts).toEqual(['ALL', 'ALL_https', 'org:8080', 'example.org:8080']);
expect(hosts).toEqual(['ALL', 'ALL_https', 'org', 'org:8080', 'example.org', 'example.org:8080']);
hosts = getHostDirsFromURI('ftp://example.org:8080/');
expect(hosts).toEqual(['ALL_ftp', 'org:8080', 'example.org:8080']);
expect(hosts).toEqual(['ALL_ftp', 'org', 'org:8080', 'example.org', 'example.org:8080']);
});
it('dot.page.mod.example.com', () => {
let hosts = getHostDirsFromURI('http://dot.page.mod.example.com/');
expect(hosts).toEqual(['ALL', 'ALL_http', 'com', 'example.com', 'mod.example.com', 'page.mod.example.com', 'dot.page.mod.example.com']);
expect(hosts).toEqual(['ALL', 'ALL_http', 'com', 'com:80', 'example.com', 'example.com:80', 'mod.example.com', 'mod.example.com:80', 'page.mod.example.com', 'page.mod.example.com:80', 'dot.page.mod.example.com', 'dot.page.mod.example.com:80']);
hosts = getHostDirsFromURI('https://dot.page.mod.example.com/');
expect(hosts).toEqual(['ALL', 'ALL_https', 'com', 'example.com', 'mod.example.com', 'page.mod.example.com', 'dot.page.mod.example.com']);
expect(hosts).toEqual(['ALL', 'ALL_https', 'com', 'com:443', 'example.com', 'example.com:443', 'mod.example.com', 'mod.example.com:443', 'page.mod.example.com', 'page.mod.example.com:443', 'dot.page.mod.example.com', 'dot.page.mod.example.com:443']);
hosts = getHostDirsFromURI('ftp://dot.page.mod.example.com/');
expect(hosts).toEqual(['ALL_ftp', 'com', 'example.com', 'mod.example.com', 'page.mod.example.com', 'dot.page.mod.example.com']);
expect(hosts).toEqual(['ALL_ftp', 'com', 'com:21', 'example.com', 'example.com:21', 'mod.example.com', 'mod.example.com:21', 'page.mod.example.com', 'page.mod.example.com:21', 'dot.page.mod.example.com', 'dot.page.mod.example.com:21']);
});
it('file uri', () => {
let hosts = getHostDirsFromURI('file:///nonexistent/path/');
Expand Down

0 comments on commit 053c629

Please sign in to comment.