Check browser back in Chrome extension in Trello

I’m developing a Chrome extension, that processes cards in Trello in My Cards list.

Even though Trello is a dynamic JS app, my extension is able to detect URL change when user clicks a link.

BUT how to detect when user clicks a Back/Forward button in browser?

I tried many (easy) ways to detect the Back button I found on the internet, but with no luck. Here’s my current detection function.

function detectHistoryChange(handler) {
    document.head.appendChild(document.createElement('script')).text = '(' +
        function () {
            // injected DOM script is not a content script anymore,
            // it can modify objects and functions of the page
            var _pushState = history.pushState;
            history.pushState = function (state, title, url) {
      , state, title, url);
                window.dispatchEvent(new CustomEvent('state-changed', { detail: state }));
            // repeat the above for replaceState too
        } + ')(); $(this).remove();'; // remove the DOM script element

    // And here content script listens to our DOM script custom events
    window.addEventListener('state-changed', function (e) {
        console.log('History state changed', e.detail, location.hash);


Chrome Extension Injected Javascript Button Changes Page

So, I am working on an extension that will scoop up the values in some text fields on a page, and search them on another site to make sure they are not trademarked.

I have injected a button in the page, and when I click it, it works, but it also boots me out to the main page of the site, as though I were not logged in. However, I am still logged in and can hit “back” to get back to where I was, but then all my text fields are no longer filled in. This is obviously a problem for the end user.

Any ideas?

Here is my Content Script:

window.addEventListener("load", function load(event){

    var amz = document.getElementById("draft-field");
    var TMbutton = document.createElement("button");
    var text = document.createTextNode("TMHunt Your Product");
    TMbutton.className = "a-button a-button-primary scripter-button  button-fill"


TMbutton.onclick = function() {

This does create the button and has the desired effect, except for this side-effect of popping us into a different page. Would this be something to do with the host page receiving a message to do something from my Javascript somehow that I am not seeing?