Below is a userscript that prevents JavaScript code from redirecting pages using location.href. This script overrides the relevant properties and methods of the window.location object to block redirects while still allowing the page to function normally in other ways.
How It Works:
- Overrides
location.href setter: When code attempts to set location.href to a new URL (e.g., location.href = 'https://example.com'), the script intercepts it and does nothing, preventing the redirect.
- Overrides
location.assign() and location.replace(): These methods are also common for redirects, so they are blocked as well.
- Preserves getters: The script ensures that reading values (e.g.,
location.href) still works by using the original getter.
- Logging: For debugging, it logs a warning to the console when a redirect attempt is blocked.
- Run-at document-start: The script is configured to run as early as possible to intercept redirects before they occur.
Installation:
- Install a userscript manager like Tampermonkey or Greasemonkey for your browser.
- Create a new script in the manager and paste the code below.
- Save and enable the script. It will run on all pages by default (as specified in
@match), but you can adjust the @match directive to limit it to specific sites.
Potential Caveats:
- This might break some legitimate functionality on websites that use these methods for non-redirect purposes.
- It only blocks the specified methods; other redirection techniques (e.g., using
window.open() or meta tags) are not covered.
- In rare cases, if a site uses strict Content Security Policy (CSP) or other security features, the overrides might not work.
Here's the userscript:
// ==UserScript==
// @name Stop Redirects via location.href
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Prevents JavaScript from redirecting pages using location.href, location.assign(), or location.replace().
// @author You
// @match *://*/* // Applies to all websites; change to specific domains if needed, e.g., *://*.example.com/*
// @grant none
// @run-at document-start // Runs as early as possible to intercept redirects
// ==/UserScript==
(function() {
'use strict';
// Override location.href setter and getter
try {
const locationObj = window.location;
const originalHrefDescriptor = Object.getOwnPropertyDescriptor(locationObj, 'href');
if (originalHrefDescriptor) {
Object.defineProperty(locationObj, 'href', {
get: function() {
// Use the original getter to return the current value
return originalHrefDescriptor.get.call(locationObj);
},
set: function(url) {
// Block the setter: Do nothing, but log the attempt
console.warn('Blocked redirection attempt: location.href set to ' + url);
// You could add logic here to allow redirects under certain conditions
},
configurable: true // Allow reconfiguration if needed
});
}
} catch (error) {
console.error('Failed to override location.href: ', error);
}
// Override location.assign()
if (window.location.assign) {
const originalAssign = window.location.assign;
window.location.assign = function(url) {
console.warn('Blocked redirection attempt: location.assign(' + url + ')');
// originalAssign.call(window.location, url); // Uncomment to allow it
};
}
// Override location.replace()
if (window.location.replace) {
const originalReplace = window.location.replace;
window.location.replace = function(url) {
console.warn('Blocked redirection attempt: location.replace(' + url + ')');
// originalReplace.call(window.location, url); // Uncomment to allow it
};
}
console.log('Userscript: Redirect blocking is active.');
})();
Testing:
- Visit a page that attempts to redirect using
location.href (e.g., a site with a script like setTimeout(() => { location.href = 'https://evil.com'; }, 1000);).
- Open your browser's console to see the warning logs when redirects are blocked.
This script should effectively stop most common JavaScript-based redirects using location.href. If you encounter issues or need modifications (e.g., to allow redirects on specific domains), let me know!