17

I'm building a Chrome Packaged App. I want to put the script configuration if a config file in a resource directory and on startup want to read that by Javascript.

For example

  • Project
    • WebContent
      • index.html
      • manifest.json
      • main.js
      • resource
        • config.properties

Here I want main.js to load config.properties file in the beginning and get key-value pairs.

Have anyone done something like this?

D.S
  • 1,110
  • 3
  • 11
  • 26

6 Answers6

40

There is a super simple way to do this, along the lines of sowbug's answer, but which doesn't need any XHR or file reading.

Step 1. Create resource/config.js like so:

gOptions = {
  // This can have nested stuff, arrays, etc.
  color: 'red',
  size: 'big',
  enabled: true,
  count: 5
}

Step 2. Include this file in your index.html:

<!doctype html>
<head>
  <script src="resource/config.js"></script>
  ...

Step 3. Access your options directly from your main.js (or anywhere):

  ...
  if (gOptions.enabled) {
    for (var i = 0; i < gOptions.count; i++) {
      console.log(gOptions.color);
    }
  }
  ...
Ben Wells
  • 1,896
  • 13
  • 9
  • 11
    The answer does not resolve the problem. I also have a properties file used in 3rd party code. I cannot rewrite it to json as you suggested. – anakkin Dec 20 '13 at 14:54
  • I think it did. Maybe it wasn't clear to you what was meant by a ["properties file"](http://en.wikipedia.org/wiki/.properties) – bacar Jan 29 '15 at 17:44
  • 11
    The question is about a `properties` file and not json. – AlikElzin-kilaka Jan 26 '17 at 12:47
7

You can use messageResource.js, a simple javascript library created by me for loading properties file.

1) Include messageResource.js in your index.html.

<script src="messageResource.min.js"></script>    

2) You can get key-value pairs of config.properties from main.js using the following code.

// initialize messageResource.js  
messageResource.init({
  // path to directory containing config.properties
  filePath : 'resource'
});

// load config.properties file
messageResource.load('config', function(){ 
  // load file callback 

  // get value corresponding  to a key from config.properties  
  var value = messageResource.get('key', 'config');
}); 
Khan
  • 429
  • 7
  • 14
  • 3
    why I am getting `key` as value instead of the written value in properties file? – sarwar026 Oct 13 '14 at 11:57
  • 1
    @Khan I've tried the exact process you mentioned in your answer but I'm having the same problem as sarwar026. – chaitanya89 Dec 26 '14 at 07:11
  • First make sure config.properties is accessible via a url. Before calling messageResource.get function you should load the properties file using the below code. `messageResource.load('config', function(){ // load file callback }); ` – Khan Dec 28 '14 at 05:31
  • library that silently returns garbage if you made some minor misconfiguration == no thanks – bacar Jan 29 '15 at 17:55
2

I know this was accepted as answered a long time ago but there never was a true "as is" .properties answer. There was only don't use that and instead convert it to .js. Obviously that would be preferable but not always possible. If it's not possible, say in a Java application that also has JavaScript somewhere and the .properties file is very much used by Java and shared by the JavaScript to avoid duplication, then an actual .properties answer would be best.

If you are using ES6, React, Vue, Angular, etc. then you can import it. Let's say it's a list of URLs in a URL.properties file. There is no need to specify the path even in JavaScript but a unique name is required.

import URL from 'URL';

The syntax can be tricky for keys with dots in them, such as widgetAPI.dev. You can't simply invoke it as URL.widgetAPI.dev. Since the properties file (content) is an object once it gets to JavaScript so you can reference it like any Object key, such as:

console.log(URL['widgetAPI.dev'])

If you are not in ES6, jQuery has libraries and there is always the ubiquitous messageResource already mentioned.

codebeard
  • 81
  • 7
1

npm install dotenv -D

let port = "8080";

//region read from local.properties
const result = require("dotenv").config({path: "local.properties"});
if (!result.error && !!result.parsed) port = result.parsed.port;
//endregion

local.properties

port=9000
Michael Mao
  • 433
  • 5
  • 9
  • Nice! Solved it for me because I already used dotenv. Actually, it's `const properties = dotenv.parse(fileContent)` where `fileContent` is the content of the file as a string. Simple as that. – Andreas Dolk Nov 27 '22 at 21:47
-1

Structure the file as JSON. Read it into a string using the File API or XHR. Then JSON.parse(string).

Community
  • 1
  • 1
sowbug
  • 4,644
  • 22
  • 29
-3

It is pretty simple with JSF, but you will have to modify the HTML file every time you need a new parameter to be read from JavaScript. My property file (config.properties) in project directory has the following parameter as a key value pair.

parameter.key=parameter.value

The property file name is configured in faces-config.xml

<application>
    <resource-bundle>
        <base-name>com.example.project.properties.config</base-name>
        <var>configuration</var>
    </resource-bundle>
</application>

Therefore, you can use a property value in a hidden input inside the HTML file.

<h:inputHidden id="parameter_key" value="#{configuration['parameter.key']}" />

And from JavaScript function, use the following line to read the parameter value.

var parameter_value = document.getElementById("parameter_key").value;

Enjoy your day..!!!

Keshan Fernando
  • 347
  • 1
  • 4
  • 16