"Could not convert Javascript argument" when adding a preference observer

Talk about code development, features, specific bugzilla bugs, enhancements, patches, and other highly technical things.
Forum rules
Please keep everything here strictly on-topic.
This board is meant for Pale Moon source code development related subjects only like code snippets, patches, specific referenced Bugzilla bugs, mercurial, etc.

This is not for tech support! Please do not post tech support questions in the "Development" board!
Please make sure not to use this board for support questions. Most "bug reports" do not belong in this board and should initially be posted in Community Support or other relevant support boards.

Please keep things on-topic as this forum will be used for reference for Pale Moon development. Expect topics that aren't relevant as such to be moved or deleted.
Post Reply
User avatar
moonbat
Board Warrior
Board Warrior
Posts: 1603
Joined: 2015-12-09, 15:45

"Could not convert Javascript argument" when adding a preference observer

Post by moonbat » 2020-04-18, 07:27

I am following the MDN instructions here and here.

To wit -

Code: Select all

branch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService)	.getBranch("extensions.pure-url@palemoon.");
branch.QueryInterface(Ci.nsIPrefBranch2);
[b]branch.addObserver("", this, false);[/b]
shows an error at the last line saying

Code: Select all

NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument arg 1 [nsIPrefBranch.addObserver]  
I've implemented a dummy observer callback function for now in the javascript file where this is called -

Code: Select all

observe : function(aSubject, aTopic, aData) {
}
What's the error here?
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Linux Mint 20 Xfce x64 on HP i5 laptop with 12 GB RAM, always latest versions of PM & Basilisk unless specified.

User avatar
adesh
Board Warrior
Board Warrior
Posts: 1045
Joined: 2017-06-06, 07:38

Re: "Could not convert Javascript argument" when adding a preference observer

Post by adesh » 2020-04-18, 09:10

What is "this" in your code? Second argument must be an object which implements ObserverInterface.

User avatar
moonbat
Board Warrior
Board Warrior
Posts: 1603
Joined: 2015-12-09, 15:45

Re: "Could not convert Javascript argument" when adding a preference observer

Post by moonbat » 2020-04-18, 09:21

adesh wrote:
2020-04-18, 09:10
What is "this" in your code? Second argument must be an object which implements ObserverInterface.
That's the current Javascript file, which has the required observer interface's method.
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Linux Mint 20 Xfce x64 on HP i5 laptop with 12 GB RAM, always latest versions of PM & Basilisk unless specified.

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 27036
Joined: 2011-08-28, 17:27
Location: 58°2'16"N 14°58'31"E
Contact:

Re: "Could not convert Javascript argument" when adding a preference observer

Post by Moonchild » 2020-04-18, 10:19

You have to be explicit where "branch" is rooted.

using this object

Code: Select all

var mycontainingobject = {
  branch: null;  // make sure the object property exists
[...]
  this.branch = Components.classes["@mozilla.org/preferences-service;1"]
                                      .getService(Components.interfaces.nsIPrefService)
                                      .getBranch("extensions.pure-url@palemoon.");
  this.branch.QueryInterface(Ci.nsIPrefBranch2);
  this.branch.addObserver("", this, false);
[...]
}
using another object

Code: Select all

var prefObj = {
  branch: null;
}

var mycontainingobject = {
[...]
  prefObj.branch = Components.classes["@mozilla.org/preferences-service;1"]
                                      .getService(Components.interfaces.nsIPrefService)
                                      .getBranch("extensions.pure-url@palemoon.");
  prefObj.branch.QueryInterface(Ci.nsIPrefBranch2);
  prefObj.branch.addObserver("", prefObj, false);
[...]
}
"There will be times when the position you advocate, no matter how well framed and supported, will not be accepted by the public simply because you are who you are." -- Merrill Rose
Image

User avatar
moonbat
Board Warrior
Board Warrior
Posts: 1603
Joined: 2015-12-09, 15:45

Re: "Could not convert Javascript argument" when adding a preference observer

Post by moonbat » 2020-04-19, 03:24

Thanks, that fixed it!
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Linux Mint 20 Xfce x64 on HP i5 laptop with 12 GB RAM, always latest versions of PM & Basilisk unless specified.

Post Reply