javascriptgoogle-chromebrowser-historybookmarklet

Bookmarklet causes additional entry in Google Chrome's browser history


The following bookmarklet causes an additional entry in Google Chrome's browser history:

data:text/html,<script>fetch('https://www.example.com/').then(r=>r.text()).catch(e=>console.log(e)).then(t=>location.href='https://www.example.com/').catch(e=>console.log(e))</script>

The following bookmarklet, however, does not cause an additional entry in Google Chrome's browser history:

data:text/html,<script>fetch('https://www.example.com/').then(r=>r.text()).catch(e=>console.log(e));location.href='https://www.example.com/'</script>

Why is that and how to fix it?

Note: The bookmarklets are wrapped by data:text/html,<script> and </script> instead of being preceded by javascript: to be used in new about:blank tabs.


Solution

  • The method in the second Bookmarklet IS the fix. Fetch is async, so the first entry is the data-URI or the about blank and se second is the result of the fetch

    Try replacing the url instead:

    fetch('https://www.example.com/')
      .then(r => r.text())
      .catch(e => console.log(e))
      .then(t => location.replace('https://www.example.com/'))
      .catch(e => console.log(e))
    

    I do not get an error when I do this

    data:text/html,<script>fetch('https://www.example.com/').then(r => r.text()) .catch(e => console.log(e)).then(t => location.replace('https://www.example.com/')).catch(e => console.log(e))</script>