Load configuration from a JSON file?

Talk about code development, features, specific bugs, enhancements, patches, and similar 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 bugs, git, the repositories, 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. Please post issues with specific websites, extensions, etc. in the relevant boards for those topics.

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.
User avatar
moonbat
Knows the dark side
Knows the dark side
Posts: 4942
Joined: 2015-12-09, 15:45
Contact:

Load configuration from a JSON file?

Unread 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."

Image
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

JustOff

Re: Load configuration from a JSON file?

Unread 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.

User avatar
moonbat
Knows the dark side
Knows the dark side
Posts: 4942
Joined: 2015-12-09, 15:45
Contact:

Re: Load configuration from a JSON file?

Unread 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."

Image
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

JustOff

Re: Load configuration from a JSON file?

Unread 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.

User avatar
moonbat
Knows the dark side
Knows the dark side
Posts: 4942
Joined: 2015-12-09, 15:45
Contact:

Re: Load configuration from a JSON file?

Unread 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."

Image
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

JustOff

Re: Load configuration from a JSON file?

Unread 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();

User avatar
moonbat
Knows the dark side
Knows the dark side
Posts: 4942
Joined: 2015-12-09, 15:45
Contact:

Re: Load configuration from a JSON file?

Unread 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."

Image
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

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

Re: Load configuration from a JSON file?

Unread 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
Moon Magic practitioner
Moon Magic practitioner
Posts: 2183
Joined: 2018-05-05, 13:29

Re: Load configuration from a JSON file?

Unread 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
Knows the dark side
Knows the dark side
Posts: 4942
Joined: 2015-12-09, 15:45
Contact:

Re: Load configuration from a JSON file?

Unread 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."

Image
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

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

Re: Load configuration from a JSON file?

Unread 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
Knows the dark side
Knows the dark side
Posts: 4942
Joined: 2015-12-09, 15:45
Contact:

Re: Load configuration from a JSON file?

Unread 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."

Image
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

New Tobin Paradigm

Re: Load configuration from a JSON file?

Unread 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.

User avatar
moonbat
Knows the dark side
Knows the dark side
Posts: 4942
Joined: 2015-12-09, 15:45
Contact:

Re: Load configuration from a JSON file?

Unread 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."

Image
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

Locked