Skip to content

Instantly share code, notes, and snippets.

@ceshine
Created September 25, 2025 14:51
Show Gist options
  • Save ceshine/aa3fcc6ba952a8144dc0eaf5c1b8eca7 to your computer and use it in GitHub Desktop.
Save ceshine/aa3fcc6ba952a8144dc0eaf5c1b8eca7 to your computer and use it in GitHub Desktop.
A User Script to Block `disable-devtools`
// ==UserScript==
// @name Disable Anti-DevTools Mechanism
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Prevents JavaScript from redirecting the page.
// @author Ceshine Lee
// @match *://*.example.com/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
console.log('User script to block anti-devtools is running!');
window.addEventListener('beforeunload', function (e) {
// Cancel the event
e.preventDefault();
// Chrome requires returnValue to be set to an empty string
e.returnValue = '';
});
// Override the functions that cause page navigation.
// We replace them with functions that do nothing except log to the console for debugging.
window.history.back = function() { console.log('[UserScript] history.back() was blocked.'); };
window.history.go = function() { console.log('[UserScript] history.go() was blocked.'); };
// Instead of window.location.assign, we modify the prototype.
// This is more robust and avoids the "read-only" error.
Location.prototype.assign = function() {
console.log('[UserScript] location.assign() was blocked.');
};
Location.prototype.replace = function() {
console.log('[UserScript] location.replace() was blocked.');
};
// Disable setInterval to block the fallback attempts from disable-devtools
// This is very effective but may break other site functionality.
window.setInterval = function() { console.log('[UserScript] setInterval() was blocked.'); };
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment