Skip to content

Add new scriptlet — 'prevent-navigation' #532

@AdamWr

Description

@AdamWr

Some websites when adblocker is detected or when devtools are opened use location.href/location.replace/location.assign/location.reload to redirect to another URL or reload website.
The problem with that is that window.location is a read-only and properties/methods cannot be overridden, but in the case of Chromium based browsers as a workaround a Navigation API (https://developer.mozilla.org/en-US/docs/Web/API/Navigation/navigate_event) can be used to prevent redirection.
So I think that we could add a scriptlet for it.

For example:

(() => {
    const preventNavigation = (url) => {
        window.navigation.addEventListener('navigate', (event) => {
            if (!url) {
                return;
            }
            const destinationURL = event.destination.url;
            console.log(`Navigation attempt to: ${destinationURL}`);
            if (
                (typeof url === 'string' && destinationURL.includes(url))
                || (url instanceof RegExp && url.test(destinationURL))
            ) {
                console.trace(`Blocked navigation to ${destinationURL}.`);
                event.preventDefault();
                return;
            }
            console.trace(`Allowed navigation to: ${destinationURL}`);
        });
    };

    // Example usage:
    // Regex: preventNavigation(/example\.com/);
    // Regex to block all navigation: preventNavigation(/.*/);
    // Simple usage: preventNavigation('example.com');
    // To prevent location.reload use location.href: preventNavigation(location.href);
    preventNavigation(/.*/);
})();

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions