I have a Tampermonkey script, it should prevent the back action if the user clicked the back button of the browser. I'm using code from the answers to this question.
// ==UserScript==
// @name Prevent touchpad gestures/button of the page back action
// @namespace https://github.com/remi-crystal
// @version 0.1
// @description Use google.com for testing propose
// @author Remi Crystal
// @match https://*.google.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// always works
console.log(document.title, location.href);
// only works after any interaction
window.addEventListener('popstate', function (event) {
history.pushState(null, document.title, location.href);
});
history.pushState(null, document.title, location.href);
})();
This event-listener works fine in general. But if I didn't interact like clicking or pressing a key within the page (moving my cursor doesn't count), it won't work (the page just goes back if I click on the back button or use the touchpad gesture). Other code doesn't matter.
Why could this happen? How do I solve this problem?
My browser version: Chrome 96.0.4664.110 (Official Build) (arm64)