Logging all visited URL's from browser

So I'm trying to POST the URLs I visit in my browser(Firefox) to my Node-red instance on the same machine.

I set up a very simple flow with an "HTTP in" node to grab them:

[{"id":"74ec266a4dce6944","type":"http in","z":"1540ac82c636ea50","name":"","url":"/firefox","method":"post","upload":false,"swaggerDoc":"","x":190,"y":200,"wires":[["5f8f7eb0e6d819a0","11d8ea58f48f31b6"]]},{"id":"14c08e65804dea2a","type":"http response","z":"1540ac82c636ea50","name":"","statusCode":"","headers":{},"x":610,"y":200,"wires":[]},{"id":"5f8f7eb0e6d819a0","type":"debug","z":"1540ac82c636ea50","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":410,"y":160,"wires":[]},{"id":"11d8ea58f48f31b6","type":"change","z":"1540ac82c636ea50","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"success","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":200,"wires":[["14c08e65804dea2a"]]}]

It works okay for most pages, other than YouTube. I believe this is because the way YouTube page script intercepts changes to the URL (as long as they're within the same domain) and uses a View Router to re-render the page with the new details, including making requests for new data.

The userscript I've made only seems to throw one error in the Web Dev Console:

Error: Promised response from onMessage listener went out of scope

The userscript code is:

var address = location.href;
(function() {
'use strict';
    function GetURL() {
        let requestOptions = {
            method: "POST",
            url: "http://192.168.50.224:6969/firefox",
            data: `${address}`,
            headers: {
                "Content-Type": "application/javascript"
            },
            onload: function track() {
                console.log(`Successfully logged ${address}.`);
            }
        };
        GM_xmlhttpRequest(requestOptions)
    };

    GetURL();

    let mutationObserver = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (address != document.location.href) {
                address = document.location.href;
                GetURL();
                return true;
            }
        });
    });
    if (document.querySelector("#movie_player") !== null) {
        mutationObserver.observe(document.querySelector("#movie_player"), {
            attributes: false,
            characterData: false,
            childList: true,
            subtree: true,
            attributeOldValue: false,
            characterDataOldValue: false
        });
    }
})();

I can't seem to figure out the fix, any ideas?

So I scraped the mutationObserver idea and now I've got this:

var address = location.href;
(function() {
'use strict';
    function GetURL() {
        let requestOptions = {
            method: "POST",
            url: "http://192.168.50.224:6969/firefox",
            data: `${address}`,
            headers: {
                "Content-Type": "application/javascript"
            },
            onload: function track() {
                console.log(`Successfully logged ${address}.`);
            }
        };
        GM_xmlhttpRequest(requestOptions)
    };

    GetURL();

    var previousState = window.history.state;
    setInterval(function() {
        if (previousState !== window.history.state) {
            previousState = window.history.state;
            address = location.href;
            GetURL();
        }
    }, 250);

})();

It does work, but is this really the best way of doing this?

Cleaned it up a bit and added a way to catch hash changes:

(function() {
    'use strict';
    let address = location.href;
    let previousState = window.history.state;
    let hash = window.location.hash;

    function GetURL() {
        let requestOptions = {
            method: "POST",
            url: "http://192.168.50.224:6969/firefox",
            data: `${address}`,
            headers: {
                "Content-Type": "application/javascript"
            }
        };
        GM_xmlhttpRequest(requestOptions);
        console.log(`Successfully logged ${address}`);
    };

    GetURL();

    setInterval(function() {
        if (previousState !== window.history.state) {
            previousState = window.history.state;
            address = location.href;
            GetURL();
        };
        if (hash !== window.location.hash) {
            hash = window.location.hash;
            address = location.href;
            GetURL();
        };
    }, 2000);

})();

Everything seems to work now.
I guess my only remaining concern is, is it okay to poll every 2000ms with setInterval() in a userscript?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.