Injecting a preference (advanced)

I’m having some problems with using window Observer. I got this code from Nathan and changed it a bit for my needs, but it keeps creating 2 and more injections even if there’s only one call.
If someone from the devs have some time to look into it, please help me :slight_smile:

If you want help with your problem please provide a detailed explanation of your problem. You’re giving us a very high level picture that would require us to figure out exactly how it relates to your code.

I have a file called prefs.js that injects my custom UI into an existing prefs page. injectInterpreterPref is a function that pushes an inject configuration, I call it only once in my init function of Xemmet (so it SHOULD inject a configuration only once) and this seems to be true because I’ve added a simple log message when injectInterpreterPref has been called - I can see this message in my log only once. But the problem that injections variable has MORE than one injection configuration, and I can see that by adding another log message to prefWindowObserver.observe (inside for cycle). It does nothing on start, but when I open Preferences - I can see the message once, then if I switch to another page of preferences, I can see this again appearing in my log, and by navigating between preferences pages I can see that count of messages appeared in the log increases. And when I switch to the page with my injected preference, I can see that there was 2 iterations of injections variable, because there are 2 groupboxes created by this function. That’s the problem. I know I can just don’t iterate over this variable, but I want to know why this is calling twice.

Try logging the contents of injections at the start and end of injectInterpreterPref(). You can use console.log() for this.

Added console.log before pushing an injection and after, got this:

[2016-05-31 22:16:26,349] [DEBUG] console: info: Array []
[2016-05-31 22:16:26,351] [DEBUG] console: info: Array [{"basename":"pref-editsmart","siblingSelector":"#collaboration_groupbox","prefname":"xemmet_snippets_are_important","caption":"Xemmet"}]

prefWindowObserver.observe for some reasons is calling twice:

Array [0: Object, length: 1]length: Number 10: Object {basename: "pref-edit .., siblingSelector: "#collabor .., prefname: "xemmet_sn .., caption: "Xemmet"}22:18:54 914ms
Array [0: Object, length: 1]length: Number 10: Object {basename: "pref-edit .., siblingSelector: "#collabor .., prefname: "xemmet_sn .., caption: "Xemmet"}

Okay, I found a workaround for it. Gonna push it to the master.

pref_page_loaded is called for every pref page that is loaded, you still need to do a check to see if it’s the pref page you’re interested in.

1 Like

There’s a check for it:

prefWindowObserver.observe = ...
    if (data.indexOf(basename) == -1)

basename contains the name of a page I want to hack and inject something into it (in this case: editsmart). prefWindowObserver.observe just being called 2 times for no reasons, I’m digging in it cause my workaround is breaking the injection if I re-open Preferences.

That’s odd. What’s to stop you from simply checking if you have already injected though?

I did it and this “was” a workaround, but the problem is that once I close Preferences, the “it’s already injected” flag will stay and if I re-open Preferences, injection won’t work.

You miss-understand me. You can check on the actual page on which you are injecting whether you have already injected (ie. check if the DOM structure you are adding already exists).

Hmm, that’s a good idea! Gonna do that.