Question

Is there a way to only make my OWN browser (Chrome) not be able to go back / forward / refresh?

This happens rather often that when Im developing and playing around in devtools (Changing HTML and CSS just to try things out) I sometimes accidentally swipe back or out of habit hit refresh. I would like to be able to disable the back or forward button via some sort of extension?

I am NOT trying to disable the button on any live-website, just for me locally. Any ideas?

Was it helpful?

Solution

If you want to prevent accidental navigations, there's no need to install any extension. Just open the console, and run the following code:

window.onbeforeunload = function() {
    return 'Want to unload?';
};

With this code, you will get a confirmation prompt.


If you really want to prevent the page from unloading via an extension, use the technique described in the answers to How to cancel webRequest silently in chrome extension.

Here's a minimal demo extension that adds a button to your browser. Upon click, you cannot navigate to a different page any more. You can still close the tab without any warning, though:

// background.js
chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.webRequest.onBeforeRequest.addListener(function(details) {
        var scheme = /^https/.test(details.url) ? 'https' : 'http';
        return { redirectUrl: scheme + '://robwu.nl/204' };
        // Or (seems to work now, but future support not guaranteed):
        // return { redirectUrl: 'javascript:' };
    }, {
        urls: ['*://*/*'],
        types: ['main_frame'],
        tabId: tab.id
    }, ['blocking']);
});

manifest.json for this extension:

{
    "name": "Never unload the current page any more!",
    "version": "1",
    "manifest_version": 2,
    "background": {
        "scripts": ["background.js"],
        "persistent": true
    },
    "browser_action": {
        "default_title": ""
    },
    "permissions": [
        "<all_urls>",
        "webRequest",
        "webRequestBlocking"
    ]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top