2

I'm trying to write my first firefox addon at the moment. The goal is quite simple: I'm trying to find every occurrence of a specific username on a website and replace them with a String. I can search for those occurrences manually, since theres not a huge quantity. This worked for one of the occurrences in the HTML of the website, an element which has an ID and gets displayed in a header on every instance of the website:

<a href="/user/MyUserName" id="user-profile-name" class="head-link">MyUserName</a>

This is how I replaced the username with with the String:

document.getElementById("user-profile-name").innerHTML = "The String";

I tried this and many other methods for the other occurrences, which don't have any id's, they're mainly classes:

<a href="/user/MyUsername" class="user um1">MyUserName</a>

If I try to change them with the same method, it works the first time I load the addon, but they just change back to the username when I visit a new page (with the same classes used on occurrences of the username (user um1)) or reload the page.

var list = document.getElementsByClassName("um1");
for (i=0; i<list.length; i++) {
  list[i].innerHTML = "The String";
}

How can I make these changes persistent? The change should only be visual, the addons purpose is to hide a username.

C. Miller
  • 31
  • 1
  • 4

1 Answers1

0

The changes can't be made persistently. They need to be applied every time a page from that site loads. However, doing so is easier if you use querySelectorAll().

var list = document.querySelectorAll("#user-profile-name,.um1");
for (i=0; i<list.length; i++) {
  list[i].innerHTML = "The String";
}
Kravimir
  • 691
  • 1
  • 6
  • 7