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 migration status and path)
 
(23 intermediate revisions by 8 users not shown)
Line 1: Line 1:
Chromium or Google Chrome browser are the recommended tools for Moodle Mobile development. But remember that if you are going to use functions provided by Phonegap you should use the Android SDK or iOs developer tools.
{{Template:Migrated|newDocId=/general/app/development/setup/app-in-browser}}
{{Moodle Mobile}}
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.
{{Work in progress}}


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)}}
== 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. 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.


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


See https://code.google.com/p/chromium/wiki/ChromiumBrowserVsGoogleChrome for more information
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 [https://www.chromium.org/getting-involved/download-chromium the official download page].


We recommend to use Chromium instead Google Chrome
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.


== Advantages and disadvantages of using Chromium/Google Chrome ==
=== Linux ===
<syntaxhighlight lang="bash">
chromium-browser --allow-file-access-from-files --disable-web-security --disable-site-isolation-trials --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs --user-data-dir=~/.chromium-dev-data
</syntaxhighlight>


Main advantages
=== Windows ===
* Quick development
<syntaxhighlight lang="bash">
* DOM inspector
start chrome.exe --allow-file-access-from-files --disable-web-security --disable-site-isolation-trials --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs --user-data-dir=~/.chromium-dev-data
* Network monitor
</syntaxhighlight>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.
* Database (local storage) inspector
Depending on the version of your browser, you may get a warning message saying "You are using an unsupported command-line flag". This is expected and can safely be ignored (for development).
* Emulation options


Disadvantages
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].
* You can't test/develop using Phonegap APIs and Plugins
=== Creating a shortcut ===
* If you use custom Phonegap code (including plugins) you will need to edit the mm.cordova.js for "emulate" those APIs in a browser
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.
* You will always need to test in a real device and emulator prior to a production release
* You will need to verify that your CSS/layout works the same in an Android/iOs device


== Installation ==
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 --disable-site-isolation-trials --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs --user-data-dir=/home/{username}/.chromium-dev-data $@
</syntaxhighlight>
Notice that this time we shouldn't use <code>~/.chromium-dev-data</code> to describe the user data dir. That's because this file can be called from a different shell, and <code>~</code> could not be interpreted properly (this may end up creating a folder called "~" in your project folder, and you probably don't want that).


Install the “Chromium” browser just downloading it from https://download-chromium.appspot.com/
Also, remember to make this file executable by running <code>sudo chmod +x /usr/local/bin/unsafe-chromium</code>.


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 this param:
For convenience, you can also define an application launch that calls this script.
<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>
=== Configuring the default 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.


--allow-file-access-from-files --disable-web-security --user-data-dir  --allow-running-insecure-content
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:
 
<syntaxhighlight lang="bash">
NOTE: Since Moodle 3.0 an onwards this shouldn't be necessary since the Web Service layer supports CORS requests.
export MOODLE_APP_BROWSER=unsafe-chromium
 
</syntaxhighlight>
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
<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>
 
== Tips & tricks ==
How to open the browser:
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].
 
"Path to chrome\chrome.exe" --allow-file-access-from-files --disable-web-security --user-data-dir  --allow-running-insecure-content
 
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 --user-data-dir  --allow-running-insecure-content
 
or for Chromium
 
open -a /Applications/Chromium.app --args --allow-file-access-from-files --disable-web-security --user-data-dir  --allow-running-insecure-content
 
In Mac you can use Automator for creating a launching app
 
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 http://school.demo.moodle.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)
 
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
 
== Browsing the data base ==
[[{{ns:file}}:moodlemobile_localstorage.png|thumb|300px]]
 
The Mobile app stores information in the local HTML5 IndexedDB
 
You can inspect the local database in the Resources tab.
 
 
 
 
 
 
 
 
== File system ==
[[{{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.
 
You have to "Accept" that warning because it means that you will be able to emulate the device file system using the browser.
 
For example, you will be able to download files to the browser storage (like the user profiles images, any resource in the course, etc..)
 
You can browse the files stored in the application going to Settings > About and clicking the Filesystem root link.
 
== Emulating devices ==
[[{{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.
 
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.
 
== See also ==
 
[https://developer.chrome.com/devtools/index Chrome Developer Tools help]


Here's some things we find useful to work with the Moodle App in particular:
* [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 /assets/env.json 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. You can learn more about network requests in the [[Debugging network requests in the Moodle App]] page.
* [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]]

Latest revision as of 13:04, 14 July 2022

Important:

This content of this page has been updated and migrated to the new Moodle Developer Resources. The information contained on the page should no longer be seen up-to-date.

Why not view this page on the new site and help us to migrate more content to the new site!

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.

Linux

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

Windows

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

If you are using other operative system, check out how to run chromium with flags in other environments.

Depending on the version of your browser, you may get a warning message saying "You are using an unsupported command-line flag". This is expected and can safely be ignored (for development).

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 --disable-site-isolation-trials --allow-running-insecure-content --no-referrers --unlimited-storage --auto-open-devtools-for-tabs --user-data-dir=/home/{username}/.chromium-dev-data $@

Notice that this time we shouldn't use ~/.chromium-dev-data to describe the user data dir. That's because this file can be called from a different shell, and ~ could not be interpreted properly (this may end up creating a folder called "~" in your project folder, and you probably don't want that).

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. You can learn more about network requests in the Debugging network requests in the Moodle App page.
  • 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.