Load configuration from a JSON file?

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: 1896
Joined: 2015-12-09, 15:45

Load configuration from a JSON file?

Post by moonbat » 2020-08-01, 05:01

I want to store some configuration in a JSON file within my extension for easier maintenance later, that's too large for the usual preferences. Is there a way to directly load them as chrome:// resources, the way one can with .jsm files or do I have to go through the whole nsiFile API?
"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
JustOff
Localization Coordinator
Localization Coordinator
Posts: 1994
Joined: 2015-09-03, 19:47
Location: UA
Contact:

Re: Load configuration from a JSON file?

Post by JustOff » 2020-08-01, 09:37

In general, for reading data from chrome resources, I usually prefer using XMLHttpRequest, but in your case I would consider just using jsm instead of json.
Here are the add-ons I made in a spare time. That was fun!

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

Re: Load configuration from a JSON file?

Post by moonbat » 2020-08-01, 09:42

So can I create a .jsm file with just JSON syntax in it, as in an object?
"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
JustOff
Localization Coordinator
Localization Coordinator
Posts: 1994
Joined: 2015-09-03, 19:47
Location: UA
Contact:

Re: Load configuration from a JSON file?

Post by JustOff » 2020-08-01, 09:58

No, you still need to wrap the json in a standard jsm file, but I think it will be just as easy to maintain as plain json.
Here are the add-ons I made in a spare time. That was fun!

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

Re: Load configuration from a JSON file?

Post by moonbat » 2020-08-01, 10:09

I tried the first approach with XMLHttpRequest and got a network error trying to load a chrome URL. Let me try the .jsm then.
"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
JustOff
Localization Coordinator
Localization Coordinator
Posts: 1994
Joined: 2015-09-03, 19:47
Location: UA
Contact:

Re: Load configuration from a JSON file?

Post by JustOff » 2020-08-01, 10:23

moonbat wrote:
2020-08-01, 10:09
I tried the first approach with XMLHttpRequest and got a network error trying to load a chrome URL.
It is rather strange, usually this Swiss Army Knife works flawlessly.

Code: Select all

function reqListener () {
  console.log(this.responseText);
}

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", "chrome://global/content/aboutRights.xhtml");
oReq.send();
Here are the add-ons I made in a spare time. That was fun!

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

Re: Load configuration from a JSON file?

Post by moonbat » 2020-08-01, 10:42

Maybe because I was doing it in a .jsm and not the main window. Anyhow, I've gone with using a .jsm only, less complicated.
"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.

thosrtanner
Lunatic
Lunatic
Posts: 315
Joined: 2014-05-10, 18:19
Location: UK

Re: Load configuration from a JSON file?

Post by thosrtanner » 2020-08-01, 17:19

Wouldn't it be a bit tricky to store changes to the configuration if it was in a module?

vannilla
Board Warrior
Board Warrior
Posts: 1067
Joined: 2018-05-05, 13:29

Re: Load configuration from a JSON file?

Post by vannilla » 2020-08-01, 22:58

thosrtanner wrote:
2020-08-01, 17:19
Wouldn't it be a bit tricky to store changes to the configuration if it was in a module?
Doesn't seem like the JSON has to be edited.
In fact at first I was about to reply with a different solution, but then rereading the OP it sounds like the configuration is actually just a one-off initialization.

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

Re: Load configuration from a JSON file?

Post by moonbat » 2020-08-02, 02:34

Ok, my use case is where I want to provide a template of links that can be configured by the end user. For eg, PMPlayer currently supports a few websites using regex to detect the URL and video embeds, but these are hardcoded so it only supports the popular ones (Youtube, Dailymotion, Vimeo etc). If I can store the configuration for each site (URL detection regex, video embed template and name), and provide a UI for the user to edit or add their own, they can use it with any other site as well. I could store it in a preference string, but I thought a separate JSON would be easier to maintain.
"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.

thosrtanner
Lunatic
Lunatic
Posts: 315
Joined: 2014-05-10, 18:19
Location: UK

Re: Load configuration from a JSON file?

Post by thosrtanner » 2020-08-02, 06:15

So that looks like you want to make changes to it. I think nsiFile is going to be where you need to go.

You can still read/write the configuration as a single JSON string though, with JSON.parse and JSON.stringify

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

Re: Load configuration from a JSON file?

Post by moonbat » 2020-08-02, 07:43

Yeah, I've used JSON before, just wasn't clear how I'd load the string before passing it to JSON.parse.
"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
New Tobin Paradigm
Knows the dark side
Knows the dark side
Posts: 7577
Joined: 2012-10-09, 19:37
Location: Just beyond the Lament Configuration

Re: Load configuration from a JSON file?

Post by New Tobin Paradigm » 2020-08-02, 18:20

Put the json in a location that is mapped to a resource uri not chrome uri.. Might have better luck.
Image
- Welcome to the worst nightmare of all... reality! -

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

Re: Load configuration from a JSON file?

Post by moonbat » 2020-08-03, 04:01

Thanks, I'll try that.
"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