gpt4 book ai didi

javascript - 使用 Chrome 扩展程序时 RAM 不断变高

转载 作者:行者123 更新时间:2023-12-03 04:51:42 31 4
gpt4 key购买 nike

正如标题所说,当使用我的扩展时,RAM 变得越来越高,直到我的电脑死机,我不知道出了什么问题。这是我的代码:
list .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);
};

背景.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。请帮助我。

最佳答案

您在每个 tabs.onUpdated 事件上注入(inject) myscript.js,该事件在每个 AJAX 导航上为 facebook 站点触发,因此,如果用户单击站点内链接 100 次,您附加了 100 个 runtime.onMessage 监听器,每个监听器都会保留在内存中,因为您正在使用匿名函数。

在内容脚本 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不需要时卸载。

关于javascript - 使用 Chrome 扩展程序时 RAM 不断变高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42631529/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com