Simple script with GM_xmlhttpRequest in tampermonkey running every seconds - memory leak

619 views Asked by At

I have running tampermonkey script running every seconds - doing request to server to get data, data is there only around 0.1% of time, when data is there - it is making another ajax request using jQuery $.ajax

Now, running this is making firefox grow about 1GB every 1,5-2h and eventually crashing in few hours.

script is simple, it tests web address for a string and if its there - it tests: res.responseText.length == 36 && $('#xxxxx').length (second part tests if user is still logged to website) and then making api request.

i tried around 30 versions - this one is the best one i could make :( - worse one was about 1gb memory grow every few minutes :D

// ==UserScript==
// @name         xxxx
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://xxxx.xx/
// @grant        GM_xmlhttpRequest
// @require https://xxx.xx/content/js/jquery-3.1.1.min.js
// @require https://xxx.xx/content/js/jquery.cookie.js
// ==/UserScript==
function xxxxxxxxx() {
            GM_xmlhttpRequest({
            method: "GET",
            url: "http://xxx.xxx.xxx.xxx/api.php?method=data&action=getData",
            onload: function (res) {
//VALID only ~0.1% of time
                if (res.responseText.length == 36 &&         $('#xxxxx').length) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        headers: {
                            "X-XSRF-TOKEN": $.cookie("XSRF-TOKEN-VALUE")
                        },
                        data: JSON.stringify({'data': res.responseText}),
                        url: "https://xxxx.xxx.xx",
                        onload: function (result) {
                            console.log(result.responseText);
                        }
                    });
                }
                res = undefined;
                delete (res);
            },
            onerror: function () {
            //nothing
            }
        });
    }


(function () {
    'use strict';
    //only running if correct website with #
    if (!/#\/xxxxxx.*/.test(location.hash)) return;

    //running every seconds
    setInterval(xxxxxxxxx, 1000);
    //reload page every 10 minutes  
    setInterval(function () {
        location.reload();
    }, 360 * 1000);
})();

Firefox 66.0.5 (current) TamperMonkey 4.9.5941 (current)

0

There are 0 answers