使用我的chrome扩展程序时RAM一直很高

时间:2017-03-06 17:10:26

标签: javascript google-chrome-extension

正如标题所说,RAM在使用我的扩展程序时越来越高,直到我的电脑freezs,我不知道它有什么问题。 这是我的代码:
的manifest.json

"manifest_version": 2,
    "name": "Facebook liker",
    "description": "Extension de like automatique",
    "version": "1.0",
    "browser_action": {
        "default_icon": "icone.png",
        "default_popup": "popup.html"
    },
    "permissions": ["activeTab", "tabs", "unlimitedStorage", "alarms", "*://*/*"],
    "background": {
        "page": "background.html"
    }

popup.html:

<!DOCTYPE html>
<html>
<head>
    <link href=bootstrap/css/bootstrap.min.css rel=stylesheet>
    <style>
    </style>
</head>
<body>
    <button value=commencer type=button id=start class="btn btn-primary">Commencer</button>
    <button value=arreter type=button id=stop class="btn btn-danger">Arreter</button>
    <br>
    <br>
    <select id="mySelect">
        <option name=jaime value=1 class=jaime>J'aime</option>

        <option name=jaime value=2 class=jaime>J'adore</option>

        <option name=jaime value=4 class=jaime>Haha</option>

        <option name=jaime value=3 class=jaime>Wouah</option>

        <option name=jaime value=7 class=jaime>Triste</option>

        <option name=jaime value=8 class=jaime>Grrr</option>
    </select>
    <br>
    <br>
    <label>timer en secondes</label>
    <input id=timer type=text class=form-control style=width:100px>
    <br>
    <input id=newAdresse type=text class=form-control placeholder="Saisir les adresses" style="height: 100px;width:460px">
    <br>
    <div class=btn-group>
        <button value=add type=button id=add class="btn btn-default" style="padding-top:16px;padding-bottom:16px">Ajouter les adresses</button>
        <button value=delet type=button id=deletSelected class="btn btn-default">Supprimer les adresses
            <br>selectionnés</button>
        <button value=deletAll type=button id=deletAll class="btn btn-default">Supprimer tous
            <br>les adresses</button>
    </div>
    <br>
    <br>
    <div id=listeAdresse>
        <label>Liste des adresses</label>
    </div>
    <script src=popup.js></script>
</body>

</html>

popup.js:

var start = document.getElementById('start');
var stop = document.getElementById('stop');
var timerInput = document.getElementById('timer');
var add = document.getElementById('add');
var deletSelected = document.getElementById('deletSelected');
var deletAll = document.getElementById('deletAll');
var listeAdresse = document.getElementById('listeAdresse');
var selectReaction = document.getElementById("mySelect");
var radios = document.getElementsByClassName("jaime");
var address = new Array();

function restaurerLesOptions() {
    if (localStorage['start'] && parseInt(localStorage['start']) > 0) {
        start.disabled = true;
        stop.disabled = false;
        localStorage['start'] = 1;
    } else {
        start.disabled = false;
        stop.disabled = true;
        localStorage['start'] = 0;
    }
    if (localStorage['timer'] && parseInt(localStorage['timer']) > 0) {
        timerInput.value = parseInt(localStorage['timer']);
    } else {
        timerInput.value = 400;
        localStorage.timer = 400;
    }

    if (localStorage["address"])
        address = JSON.parse(localStorage["address"]);

    for (i = 0; i < address.length; i++) {
        showAdddress(i);
    }
    if (localStorage.selectReaction)
        selectReaction.value = localStorage.selectReaction;
    else {
        localStorage.selectReaction = "1";
        selectReaction.value = "1";
    }
};

window.onload = function () {
    restaurerLesOptions();
    newAdresse.ondrop = function (e) {
        e.preventDefault();

        var file = e.dataTransfer.files[0],
            reader = new FileReader();
        reader.onload = function (event) {
            if (address.length > 0)
                deletAll.click();
            address = event.target.result.split(/ |\n/);
            if (address[address.length - 1].length < 15)
                address.splice(address.length - 1, 1);
            address.sort(function (a, b) {
                return 0.5 - Math.random()
            });
            for (i = 0; i < address.length; i++) {
                address[i] = address[i].replace("\r", "");
                address[i] += "?v=timeline";
                address[i] = address[i].replace("www", "mbasic");
                showAdddress(i);
            }
            localStorage["address"] = JSON.stringify(address);
        };
        reader.readAsText(file);

        return false;
    };
};
start.onclick = function () {
    start.disabled = true;
    stop.disabled = false;
    localStorage['start'] = 1;

    chrome.runtime.getBackgroundPage(function (bgWindow) {
        bgWindow.startLikes(parseInt(localStorage['timer']), selectReaction.value);
    });

};

stop.onclick = function () {
    start.disabled = false;
    stop.disabled = true;
    localStorage['start'] = 0;
    chrome.runtime.getBackgroundPage(function (bgWindow) {
        bgWindow.stopLikes();
    });
};

timerInput.onchange = function () {
    localStorage['timer'] = parseInt(timerInput.value);
};

selectReaction.onchange = function () {
    localStorage['selectReaction'] = selectReaction.value;
};

add.onclick = function () {
    var newadresse = document.getElementById('newAdresse');
    if (newadresse.value) {
        if (address.length > 0)
            deletAll.click();
        address = newadresse.value.split(/ |\n/);
        if (address[address.length - 1].length < 15)
            address.splice(address.length - 1, 1);
        address.sort(function (a, b) {
            return 0.5 - Math.random()
        });
        for (i = 0; i < address.length; i++) {
            address[i] = address[i].replace(" ", "");
            address[i] += "?v=timeline";
            address[i] = address[i].replace("www", "mbasic");
            showAdddress(i);
        }
        localStorage["address"] = JSON.stringify(address);
        newadresse.value = "";
    }
};
deletAll.onclick = function () {
    localStorage["address"] = "";
    address = [];
    start.disabled = false;
    stop.disabled = true;
    localStorage['start'] = 0;
    while (listeAdresse.childNodes.length > 2) {
        listeAdresse.removeChild(listeAdresse.childNodes[2]);
    }
};
deletSelected.onclick = function () {
    var checkboxs = document.getElementsByClassName("checkBoxClass");
    var copieAdresse = address.slice();
    var w = 0;
    for (k = 0; k < checkboxs.length; k++) {
        if (checkboxs[k].childNodes[0].checked) {
            copieAdresse.splice(parseInt(checkboxs[k].childNodes[0].value) - w, 1);
            w++;
        }
    }
    deletAll.click();
    address = copieAdresse.slice();
    localStorage["address"] = JSON.stringify(address);
    for (k = 0; k < address.length; k++) {
        showAdddress(k);
    }
};

function showAdddress(j) {
    var cbox;
    var lien;

    cbox = document.createElement("input");
    cbox.setAttribute("type", "checkbox");
    cbox.setAttribute("value", i);
    lien = document.createElement("label");
    lien.style.fontWeight = "normal";
    lien.setAttribute("class", "checkBoxClass");
    lien.appendChild(cbox);
    lien.innerHTML += "    " + address[j];
    listeAdresse.appendChild(document.createElement("br"));
    listeAdresse.appendChild(lien);
};

background.js:

function startLikes(timer, emotion) {

    var address = JSON.parse(localStorage["address"]);
    var tabId;
    var i;

    if (parseInt(localStorage.start) > 0 && timer > 0 && emotion && address.length > 1) {
        i = 0;
        chrome.alarms.create("alarm", {
            periodInMinutes: timer / 60
        });
        chrome.tabs.create({
            active: true,
            url: address[i]
        }, function (tab) {
            i++;
            tabId = tab.id;
            chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {

                if (changeInfo.status == "complete") {

                    chrome.tabs.executeScript(
                        tabId, {
                            file: "myscript.js"
                        },
                        function () {
                            chrome.tabs.sendMessage(tabId, {
                                "emotion": emotion
                            });
                        }
                    );

                }
            });
            chrome.tabs.onRemoved.addListener(function (tabRemoved, removeInfo) {
                if (tabRemoved == tabId) {
                    localStorage.start = 0;
                    chrome.tabs.onRemoved.removeListener(function () {});
                }
            });
        });
        chrome.alarms.onAlarm.addListener(function (alarm) {
            if (navigator.onLine) {

                chrome.tabs.update(tabId, {
                    active: true,
                    url: address[i]
                });
                i++;
                if (i == address.length) {
                    i = 0;
                }
            }
        });
    }
};

function stopLikes() {
    if (parseInt(localStorage.start) == 0) {
        chrome.alarms.clear("alarm");
        i = 0;
    }
}

myscript.js:

var emotion = "";
var url = window.location.href;
var patt = /facebook.com\/\?|facebook.fr\/\?|facebook.com$|facebook.fr$|facebook.fr\/$|facebook.com\/$|facebook.com\/home.php|facebook.fr\/home.php/;
var match = patt.test(url);
var start = 0;

chrome.runtime.onMessage.addListener(
    function (msg, _, sendResponse) {
        emotion = msg.emotion;
        if (!match && url.search(".facebook.") > 0) { //c'est un profil facebook
            var likes = document.querySelector("a[href^='/reactions/picker/']");

            if (likes && likes.firstChild && likes.firstChild.nodeName != "B") { //si il n'y a pas de j'aime
                likes.click();
            } else if (url.search("reactions/picker") > 0) {

                var supprimer = document.querySelectorAll("span[class='z']");
                if (supprimer.length < 1) {
                    var reactions = document.querySelectorAll("a[href^='/ufi/reaction/']");
                    var reaction;
                    switch (emotion) {
                    case "1":
                        reaction = "reaction_type=1";
                        break;
                    case "2":
                        reaction = "reaction_type=2";
                        break;
                    case "4":
                        reaction = "reaction_type=4";
                        break;
                    case "3":
                        reaction = "reaction_type=3";
                        break;
                    case "7":
                        reaction = "reaction_type=7";
                        break;
                    case "8":
                        reaction = "reaction_type=8";
                        break;
                    default:
                        console.log(emotion);
                        console.log(reaction);
                    }
                    for (i = 0; i < reactions.length; i++) {

                        if (reactions[i].href.indexOf(reaction) > 0) {
                            reactions[i].click();
                            break;
                        }
                    }

                }
            }
        }


    });

我搜索了很多垃圾收集和糟糕的javascript编码习惯,但我仍然没有想到这一点。至于文件,它不超过2000行和80Ko。请帮帮我。

1 个答案:

答案 0 :(得分:1)

你在每个AJAX导航上为facebook网站触发的每个tabs.onUpdated事件上注入myscript.js,所以如果用户点击100次站内链接,你会附加100个runtime.onMessage监听器,每个都留在内存中,因为你& #39;重新使用匿名函数。

在内容脚本myscript.js中使用一个变量并检查它:

if (!window.__injected) {
    window.__injected = true;
    .................
    // attach the listener, do the stuff
    .................
}

此变量仅对您的内容脚本可见,因为它在isolated world中运行 该网站或其他扩展程序不会看到此变量。

您还可以添加匿名函数作为chrome.tabs.onUpdated,chrome.tabs.onRemoved,chrome.alarms.onAlarm的侦听器,这意味着每次创建新实例并将其保留在内存中,直到浏览器关闭为止。重做背景代码以附加一次监听器,甚至切换到不需要时卸载的event page