Load an external library dynamically in Moodle Mobile

Re: Load an external library dynamically in Moodle Mobile

by Dani Palou -
Number of replies: 4
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hi Adrián,

we only allow injecting scripts that are hosted in the device itself (for security reasons and to make them work in offline mode too). So you should download your script first and then inject it using the local URL.

You can do it easily using our CoreFilepoolProvider. The JS code returned by plugins should have access to that provider:

var that = this;
loadOlScript(url) {
    // Download the file if it isn't downloaded already.
    return that.CoreFilepoolProvider.downloadUrl(url).then(function(localUrl) {
        //load script
        ...
    });
}
Please let me know if this doesn't work for you or you find any other problem 😉

Cheers,
Dani
In reply to Dani Palou

Re: Load an external library dynamically in Moodle Mobile

by Akin Kazakci -
Hi,
Do we need to download nested scripts as well?
That is, if the script I download via corefilepoolprovider is requiring another 3rd party script, do I need to download it via corefilepoolprovider as well?
In reply to Akin Kazakci

Re: Load an external library dynamically in Moodle Mobile

by Dani Palou -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Hi Akin,

if your main script is loading the 3rd party script using an URL to get the code, then it will work without any further work but please notice it will only work in online.

Otherwise, if your main script requires that the 3rd party script has already been loaded, then you will need to download the 3rd party script too and load it into the app.

Cheers,
Dani
Average of ratings: Useful (1)
In reply to Dani Palou

Re: Load an external library dynamically in Moodle Mobile

by Akin Kazakci -
Thank you Dani.

Why would it only online though? - Since the scripts are already loaded (or once they are loaded) could not they continue to work on device as long as the app is used?

best
In reply to Akin Kazakci

Re: Load an external library dynamically in Moodle Mobile

by Dani Palou -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Yes, but if the user restarts the app then all the scripts are unloaded. In that case the app will be able to load the scripts that have been downloaded, but not the 3rd party ones that are loaded via a URL.