Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access config.xml preferences from JavaScript plugins

Tags:

cordova

In my project's config.xml I added a custom preference:

<preference name="FooBar" value="Baz" />

Then, in my custom plugin's JavaScript inside <plugin>/www/plugin.js I want to access the value of such feature.

Does Cordova exposes those values to the JavaScript side? I couldn't find any information in the documentation about it.

Tried:

var argscheck = require('cordova/argscheck');
argscheck.getValue('FooBar'); // Returns just FooBar
like image 764
jviotti Avatar asked Jul 24 '14 17:07

jviotti


People also ask

How do I add a plugin to config xml cordova?

When adding plugins or platforms, use the --save flag to add them to config. xml. Ex: cordova platform add android --save. Existing projects can use cordova plugin save and cordova platform save commands to save all previously installed plugins and platforms into your project's config.

What is config xml in ionic?

The config. xml file is the place where we can change the configuration of the app. When we created our app in the last tutorial, we set reverse domain and name. The values can be changed in the config.

Where is plugin xml in cordova?

The plugin. xml file is an XML document in the plugins namespace: http://apache.org/cordova/ns/plugins/1.0 .


1 Answers

You could use following code on iOS, WP7, WP8, Windows8, and probably Ubuntu

function readConfig() {
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", function () {
        var parser = new DOMParser();
        var doc = parser.parseFromString(xhr.responseText, "application/xml");
        alert("Description : " + 
              doc.getElementsByTagName("description").item(0).textContent);
    });
    xhr.open("get", "../config.xml", true);
    xhr.send();
}

for the Android you have to change path to file from "../config.xml" to "../../android_res/xml/config.xml"

Taken from Cordova mailing where discussed answer: https://www.mail-archive.com/[email protected]/msg14313.html

Also there not-official plugin for reading configuration: https://github.com/apache/cordova-labs/tree/cdvtest/cordova-plugin-appsettings

like image 143
codevision Avatar answered Dec 02 '22 18:12

codevision