Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
popstate event listener in Tampermonkey script only works after I click within the page

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)

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda