I'm trying to write a little extension which changes all links with a specific string and then sets the font color to red/yellow/green. I first tried only changing the first link I find and everything worked great (faster for debugging), now that I removed the break statement something weird happens.
If I let my program search through all links nothing changes. But if I set it to stop at a certain string which I know should be changed it only changes the last one and nothing else. So it kinda seems like every time I try to manipulate a link every progress is gone. But this reset only happens in the threads handling a http request. In my main loop through all the links I initially set every string to the color red and that doesn't change. Any ideas? Here's the code:
var links = document.getElementsByTagName("a");
$(document).ready(function() {
"use strict";
var links = document.getElementsByTagName("a");
for(let i=0;i<links.length;i++){
if(links[i].toString().includes("/anime/")) {
//Color red
links[i].innerHTML = "<font color=\"" + red + "\">" + links[i].innerHTML + "</font>";
var searchURL = "http://random.url";
var client = new XMLHttpRequest();
client.onload = function () {
firstRequestHandler(client, i);
};
client.open("GET", proxerSearchURL);
client.send();
}
}
});
function firstRequestHandler(client, index) {
if (client.status == 200 && client.responseText != "")
{
var response = client.responseText;
//Color green
links[index].innerHTML = hrefLink.innerHTML.replace(red, green);
}
}
After this piece of code every link is red. If I set a break statement directly after "client.send();" the last link it "worked on" is colored green, every link before that is red and every link after that is unchanged (obviously).
EDIT: Tried fixing closure issue by using "let" in "strict mode" but it still doesn't work. Am I missing something? Also made links global because I thought it might help as well? Doesn't tho... Javascript is weird...