Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Using the Moodle App in a browser: Difference between revisions

From MoodleDocs
(Update after 3.9.5 release)
Line 1: Line 1:
Chromium or Google Chrome browser are the recommended tools for Moodle App development. But remember that if you are going to use functions provided by Phonegap you should use the Android SDK or iOs developer tools.
Browsers are not officially supported by the application, but you can use a Chromium-based browser for development if you don’t need any native functionality.
 
Please notice that it has to be a Chromium-based browser, because the application relies on the [https://caniuse.com/?search=websql Web SQL Database API] which isn't supported in most browsers. This is a non-standard API, but that's not a problem because this is only used in the browser. Running on a native environment, the application relies on native APIs that are well supported.
 
{{Moodle App (Ionic 5)}}
{{Moodle App (Ionic 5)}}
{{Work in progress}}


== Differences between Chromium and Google Chrome ==
== Differences between Chromium and Google Chrome ==


Google Chrome is the Chromium open source project built, packaged, and distributed by Google. We can say that Chromium is Google Chrome without the "Google" add-ons
Google Chrome is the Chromium open source project built, packaged, and distributed by Google. We can say that Chromium is Google Chrome without the "Google" add-ons. For that reason, we recommend using Chromium instead of Google Chrome.
 
See https://code.google.com/p/chromium/wiki/ChromiumBrowserVsGoogleChrome for more information
 
We recommend using Chromium instead Google Chrome


== Advantages and disadvantages of using Chromium/Google Chrome ==
== Advantages and disadvantages of using a browser instead of a native device ==


Main advantages
Main advantages
* Quick development
* Faster development.
* DOM inspector
* DOM inspector.
* Network monitor
* Network monitor.
* Database (local storage) inspector
* Database inspector.
* Emulation options
* Emulation options.


Disadvantages
Disadvantages
* You can't test/develop using Phonegap APIs and Plugins
* You can't use native functionality.
* If you use custom Phonegap code (including plugins) you will need to edit the mm.cordova.js for "emulate" those APIs in a browser
* If you need to use Cordova plugins, you will probably need to provide a way to emulate those APIs in the browser or avoid calling them in the browser environment.
* You will always need to test in a real device and emulator prior to a production release
* You will always need to test in a native device prior to a production release.
* You will need to verify that your CSS/layout works the same in an Android/iOs device
* You will need to verify that your CSS/layout looks the same in native devices.
 
== Installation ==
 
Install the “Chromium” browser just downloading it from https://download-chromium.appspot.com/
 
In order to be able to access any domain via AJAX from the local file:/// protocol, you must run Chromium or Google Chrome in Unsafe mode adding these parameters:
 
--allow-file-access-from-files --disable-web-security --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs --user-data-dir=PATH_TO_DATA_DIR
 
For more info about the user data dir, please see [https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md this documentation].
 
NOTE: Since Moodle 3.0 an onwards this shouldn't be necessary for WebService calls since the Web Service layer supports CORS requests, but it could still be needed to download some files.
 
IMPORTANT: I strongly recommend you create a new link or application launch called "Chrome Unsafe" and use it only for testing the app. Better if you only use this browser for development. In Linux and possibly other operating systems the below arguments only work if you don't already have the same browser running without the args.  Hence if you use "google-chrome" as your normal browser then use "chromium-browser" for your cors free debugging and vice versa.
 
How to open the browser from the Command line:
 
"Path to chrome\chrome.exe" --allow-file-access-from-files --disable-web-security --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs  --user-data-dir=PATH_TO_DATA_DIR
 
or in Mac (you can use Automator for creating an app bundle)
 
open -a "Google Chrome" --args --allow-file-access-from-files --disable-web-security --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs  --user-data-dir=PATH_TO_DATA_DIR
 
or for Chromium
 
open -a /Applications/Chromium.app --args --allow-file-access-from-files --disable-web-security --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs  --user-data-dir=PATH_TO_DATA_DIR
 
In Mac you can use Automator for creating a launching app.
 
In Windows you can follow [https://www.chromium.org/developers/how-tos/run-chromium-with-flags this guide] to launch Chrome with those flags without using the command line.
 
Now, you can open the application running:
ionic serve --browser chromium
it should open a new tab in the current Chromium instance with the app, (remember that you have to open first the Chromium so it's started with all the additional arguments).
 
If the ionic serve command open a new browser window, you should copy the URL and then paste it in the Chromium instance you opened using the command shell.
 
== Sites for testing ==
 
You can test the application using existing sites configured to have the Mobile services enabled.
 
Real test site: If you type "student" or "teacher" in the "Site URL" field and then tap on "Add site" the app will connect to https://school.moodledemo.net. This site is reset every hour so you may find unexpected behaviors
 
== Browser Settings ==
[[{{ns:file}}:moodlemobile_developer.png|thumb|300px]]
You should develop always with the "Developer tools" panel open, you can open that panel with F12 (cmd + alt + i in Mac) or "Developer tools" in the Tools menu.
 
[[{{ns:file}}:moodlemobile_options.png|left]]  Once opened, click on the wheel top-right corner to see the General options, you must Disable the cache there (this setting will work only if you have the Developer tools panel open)
 
You can dock or undock into a separate window the developer panel using the "Dock" option just at the right of the settings wheel, you can move the developer panel to your right and resize the main browser window to emulate a Mobile device
 
== Inspecting and changing the DOM ==
 
Using the "Elements" option you can manipulate the DOM:
 
* Add/modify style to elements
* Delete elements
* Move elements
* Inject HTML code
 
You have complete information here: https://developer.chrome.com/devtools/index
 
And also a free interactive course here: https://www.codeschool.com/courses/discover-devtools
 
== Monitor XHR (ajax) requests ==
[[{{ns:file}}:moodlemobile_network.png|thumb|300px]]
 
With the network panel you can check and filter all the HTTP request send from the app to your Moodle server.
 
You can preserve the log in the navigation, filter by type of requests and also see complete information of all the requests made to the server where is hosted your Moodle installation.
 
You can also identify with resources takes long to load
 
 
 
 
 
 
 
== Console, log and errors ==
[[{{ns:file}}:moodlemobile_log.png|thumb|300px]]
 
The console panel is used for displaying the app log and errors, you can also configure Chrome for displaying there XHR requests


In order to view the app log, you need first to enable Logging in the app (Options / Developers / Enable logging)
== Installation and configuration ==


You can use the console also for executing javascript commands, the console has access to the complete MM global object so you can call from there to core APIs functions of the app
You can install the Chromium browser by downloading it from [https://www.chromium.org/getting-involved/download-chromium the official download page].


== Browsing the data base ==
In order to run the Moodle App, we recommend that you launch the browser with a couple of arguments. These are necessary to disable some of the limitations that don't exist in the native application, and also prepare the development environment:
[[{{ns:file}}:moodlemobile_localstorage.png|thumb|300px]]


The Mobile app stores information in the local HTML5 IndexedDB
<syntaxhighlight lang="bash">
chromium-browser --allow-file-access-from-files --disable-web-security --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs --user-data-dir=~/.chromium-dev-data
</syntaxhighlight>


You can inspect the local database in the Resources tab.
The command above will work on Linux. If you are using other operative system, check out [https://www.chromium.org/developers/how-tos/run-chromium-with-flags how to run chromium with flags] in other environments.


For more info about the user data dir, please read [https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md the official documentation].


=== Creating a shortcut ===


We strongly recommend that you create a new shortcut and use it only for working with the app during development. In Linux, and possibly other operating systems, these arguments only work if you don't already have the same browser running. Hence if you use Google Chrome as your normal browser, you can use Chromium for development and vice versa.


In Linux, you can create such a shortcut by writing a script that is globally available. For example, you can create the following file in <code>/usr/local/bin/unsafe-chromium</code>:


<syntaxhighlight lang="bash">
#!/bin/bash
chromium-browser --allow-file-access-from-files --disable-web-security --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs --user-data-dir=~/.chromium-dev-data $@
</syntaxhighlight>


Also, remember to make this file executable by running <code>sudo chmod +x /usr/local/bin/unsafe-chromium</code>.


For convenience, you can also define an application launch that calls this script.


== File system ==
<p class="alert alert-info">'''Help wanted!''' These instructions have only been tested in Linux. If you are using a different operative system, [[Talk:Using the Moodle App in a browser|let us know]] how it went (or just [https://docs.moodle.org/dev/index.php?title=Using_the_Moodle_App_in_a_browser&veaction=edit edit this page]!).</p>
[[{{ns:file}}:moodlemobile_filesystem.png|thumb|300px]]


The first time you open the browser with the special flags you will see a couple of warnings, one asking you to accept to store information.
=== Configuring the default browser ===


You have to "Accept" that warning because it means that you will be able to emulate the device file system using the browser.
When you launch the application by running <code>npm start</code>, this will open a tab on your default browser. You can close this tab and open the url with your development browser, but if you want to do it automatically you can override the default browser by setting the <code>MOODLE_APP_BROWSER</code> environment variable.


For example, you will be able to download files to the browser storage (like the user profiles images, any resource in the course, etc..)
For example, if you have created a shortcut like we mentioned in the previous section, you can just add the following to your <code>~/.bashrc</code> file:


You can browse the files stored in the application going to Settings > About and clicking the Filesystem root link.
<syntaxhighlight lang="bash">
export MOODLE_APP_BROWSER=unsafe-chromium
</syntaxhighlight>


== Emulating devices ==
<p class="alert alert-info">'''Help wanted!''' These instructions have only been tested in Linux. If you are using a different operative system, [[Talk:Using the Moodle App in a browser|let us know]] how it went (or just [https://docs.moodle.org/dev/index.php?title=Using_the_Moodle_App_in_a_browser&veaction=edit edit this page]!).</p>
[[{{ns:file}}:moodlemobile_emulation.png|thumb|300px]]


You can emulate mobile devices changing orientation, resolution, geo-location, touch events... It's not recommended to use de "Emulation Device" option because it just changes the user-agent and you could get some fails because of the jQuery Swipe library.
== Tips & tricks ==


The best option to test with the browser is just rescaling the window to get a new viewport size or use the screen settings in the "Emulation" screen.
Once you have everything set up, you should be able to develop like you would with any other front-end application. You can learn about the development tools you have available by reading the [https://developer.chrome.com/devtools/index Chrome DevTools documentation].


== See also ==
Here's some things we find useful to work with the Moodle App in particular:


[https://developer.chrome.com/devtools/index Chrome Developer Tools help]
* [https://developer.chrome.com/docs/devtools/device-mode/ Device Mode] — You can use this feature to make the browser behave more like a native device. This will work best if you [https://developer.chrome.com/docs/devtools/customize/placement/ dock the development panel] to one side (left or right), but you may want to do something else in your environment.
* [https://developer.chrome.com/docs/devtools/console/ Console Panel] — This panel is essential for any developer, since it will show you any errors or custom messages that you've written. You can also use the search box to filter messages seeing everything is too overwhelming. You will also see specific logs from the Moodle App, but keep in mind that they are not used in production environment. If you are not running the application yourself, you can inspect the environment by opening the </code>/assets/env.json</code> url.
* [https://developer.chrome.com/docs/devtools/dom/ Elements Panel] — This panel is also essential for any developer, you'll be able to inspect and modify the HTML that is actually being rendered.
* [https://developer.chrome.com/docs/devtools/network/ Network Panel] — This panel can be useful if you are trying to see how the Moodle App communicates with a Moodle site. You may also want to [https://developer.chrome.com/docs/devtools/network/reference/#disable-cache disable the cache] in order to have the same behaviour after each reload. However, keep in mind that this only disables the browser cache, any Web Service calls that are cached by the Moodle App will remain cached.
* [https://developer.chrome.com/docs/devtools/storage/websql/ WebSQL Inspector] — As mentioned before, WebSQL is a non-standard API. But since the Moodle App uses it for development, this inspector may come in handy. Keep in mind that the native application does not use WebSQL, so it is possible that you see some different behaviour in a native device. But it should be reliable for the most part.


[[Category: Mobile]]
[[Category: Mobile]]
[[Category: Moodle App Ionic 5]]

Revision as of 09:23, 2 September 2021

Browsers are not officially supported by the application, but you can use a Chromium-based browser for development if you don’t need any native functionality.

Please notice that it has to be a Chromium-based browser, because the application relies on the Web SQL Database API which isn't supported in most browsers. This is a non-standard API, but that's not a problem because this is only used in the browser. Running on a native environment, the application relies on native APIs that are well supported.


Differences between Chromium and Google Chrome

Google Chrome is the Chromium open source project built, packaged, and distributed by Google. We can say that Chromium is Google Chrome without the "Google" add-ons. For that reason, we recommend using Chromium instead of Google Chrome.

Advantages and disadvantages of using a browser instead of a native device

Main advantages

  • Faster development.
  • DOM inspector.
  • Network monitor.
  • Database inspector.
  • Emulation options.

Disadvantages

  • You can't use native functionality.
  • If you need to use Cordova plugins, you will probably need to provide a way to emulate those APIs in the browser or avoid calling them in the browser environment.
  • You will always need to test in a native device prior to a production release.
  • You will need to verify that your CSS/layout looks the same in native devices.

Installation and configuration

You can install the Chromium browser by downloading it from the official download page.

In order to run the Moodle App, we recommend that you launch the browser with a couple of arguments. These are necessary to disable some of the limitations that don't exist in the native application, and also prepare the development environment:

chromium-browser --allow-file-access-from-files --disable-web-security --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs --user-data-dir=~/.chromium-dev-data

The command above will work on Linux. If you are using other operative system, check out how to run chromium with flags in other environments.

For more info about the user data dir, please read the official documentation.

Creating a shortcut

We strongly recommend that you create a new shortcut and use it only for working with the app during development. In Linux, and possibly other operating systems, these arguments only work if you don't already have the same browser running. Hence if you use Google Chrome as your normal browser, you can use Chromium for development and vice versa.

In Linux, you can create such a shortcut by writing a script that is globally available. For example, you can create the following file in /usr/local/bin/unsafe-chromium:

#!/bin/bash
chromium-browser --allow-file-access-from-files --disable-web-security --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs --user-data-dir=~/.chromium-dev-data $@

Also, remember to make this file executable by running sudo chmod +x /usr/local/bin/unsafe-chromium.

For convenience, you can also define an application launch that calls this script.

Help wanted! These instructions have only been tested in Linux. If you are using a different operative system, let us know how it went (or just edit this page!).

Configuring the default browser

When you launch the application by running npm start, this will open a tab on your default browser. You can close this tab and open the url with your development browser, but if you want to do it automatically you can override the default browser by setting the MOODLE_APP_BROWSER environment variable.

For example, if you have created a shortcut like we mentioned in the previous section, you can just add the following to your ~/.bashrc file:

export MOODLE_APP_BROWSER=unsafe-chromium

Help wanted! These instructions have only been tested in Linux. If you are using a different operative system, let us know how it went (or just edit this page!).

Tips & tricks

Once you have everything set up, you should be able to develop like you would with any other front-end application. You can learn about the development tools you have available by reading the Chrome DevTools documentation.

Here's some things we find useful to work with the Moodle App in particular:

  • Device Mode — You can use this feature to make the browser behave more like a native device. This will work best if you dock the development panel to one side (left or right), but you may want to do something else in your environment.
  • Console Panel — This panel is essential for any developer, since it will show you any errors or custom messages that you've written. You can also use the search box to filter messages seeing everything is too overwhelming. You will also see specific logs from the Moodle App, but keep in mind that they are not used in production environment. If you are not running the application yourself, you can inspect the environment by opening the /assets/env.json url.
  • Elements Panel — This panel is also essential for any developer, you'll be able to inspect and modify the HTML that is actually being rendered.
  • Network Panel — This panel can be useful if you are trying to see how the Moodle App communicates with a Moodle site. You may also want to disable the cache in order to have the same behaviour after each reload. However, keep in mind that this only disables the browser cache, any Web Service calls that are cached by the Moodle App will remain cached.
  • WebSQL Inspector — As mentioned before, WebSQL is a non-standard API. But since the Moodle App uses it for development, this inspector may come in handy. Keep in mind that the native application does not use WebSQL, so it is possible that you see some different behaviour in a native device. But it should be reliable for the most part.