Skip to content

Commit

Permalink
Improve urlskip= filter option
Browse files Browse the repository at this point in the history
New step: `#`, to extract the hash part of a URL.

Example, URL:
https://example.com/#aHR0cHM6Ly9naXRodWIuY29tL3VCbG9ja09yaWdpbi8=

Filter:
||example.com^$urlskip=# -base64

As a result, navigate to https://github.com/uBlockOrigin/
  • Loading branch information
gorhill committed Dec 24, 2024
1 parent 27a72b8 commit a7aa755
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/js/urlskip.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
* `&i`: extract the name of the parameter at position `i` as the current
* string. The position is 1-based.
*
* `#`: extract the hash as the current string.
*
* `/.../`: extract the first capture group of a regex as the current string.
*
* `+https`: prepend the current string with `https://`.
Expand Down Expand Up @@ -77,6 +79,12 @@ export function urlSkip(url, blocked, steps, directive = {}) {
for ( const step of steps ) {
const urlin = urlout;
const c0 = step.charCodeAt(0);
// Extract from hash
if ( c0 === 0x23 && step === '#' ) { // #
const pos = urlin.indexOf('#');
urlout = pos !== -1 ? urlin.slice(pos+1) : '';
continue;
}
// Extract from URL parameter name at position i
if ( c0 === 0x26 ) { // &
const i = (parseInt(step.slice(1)) || 0) - 1;
Expand All @@ -88,14 +96,14 @@ export function urlSkip(url, blocked, steps, directive = {}) {
continue;
}
// Enforce https
if ( c0 === 0x2B && step === '+https' ) {
if ( c0 === 0x2B && step === '+https' ) { // +
const s = urlin.replace(/^https?:\/\//, '');
if ( /^[\w-]:\/\//.test(s) ) { return; }
urlout = `https://${s}`;
continue;
}
// Decode
if ( c0 === 0x2D ) {
if ( c0 === 0x2D ) { // -
// Base64
if ( step === '-base64' ) {
urlout = self.atob(urlin);
Expand Down

0 comments on commit a7aa755

Please sign in to comment.