Capacitor Integration With PWA

Hi guys,
My name is Reid and I am new to capacitor.
I have a PDA app I have integrated with Capacitor but with a number of challenges.
I need help to convert the following Capacitor codes to JS versions:

  1. THE Back Key
    addListener(‘backButton’, …)
    How do I implement this please?

  2. $(’#divname’).load(filename);
    What is wrong with this code. It works fine but loads blank when Capacitor was implemented.

  3. localStorage.getItem(key))
    I want the capacitor version for this please.

  4. localStorage.setItem(key, data);
    I want the capacitor version for this.

  5. Set orientation of device to Portrait

  6. Hook the BackKey(false); //hook the back key

  7. Popup Message
    I need code for this please.

  8. openUrl(…)
    How do I implement this?

  9. mkdir(…)
    How do I implement this?

  10. rmdir(…)
    How do I implement this?

  11. readdir(…)
    How do I implement this?

  12. vibrate(…)
    How do I implement this?

  13. Toast
    I need code for this please.

Thank you in advance.
-Reid

  1. Not possible in a PWA, only possible with the @capacitor/app plugin on Android.
  2. Impossible to tell without your code.
  3. You can use LocalStorage with Capacitor.
  4. You can use LocalStorage with Capacitor.
  5. You can’t lock rotation in a PWA, but you can kind of fake it: javascript - Force “landscape” orientation mode - Stack Overflow
  6. Same as 1, Not possible in a PWA
  7. You can use a <dialog>, or any other custom UI on your web app. This is unrelated to Capacitor.
  8. The @capacitor/app-launcher plugin: Capacitor - build cross platform apps with the web
  9. The @capacitor/filesystem plugin: Capacitor - build cross platform apps with the web
  10. The @capacitor/filesystem plugin: Capacitor - build cross platform apps with the web
  11. The @capacitor/filesystem plugin: Capacitor - build cross platform apps with the web
  12. The @capacitor/haptics plugin: Capacitor - build cross platform apps with the web
  13. The @capacitor/toast plugin: Capacitor - build cross platform apps with the web

Thanks Thomasvidas for your response.
Note: Source of codes: capacitor-site/index.md at 585ff272c6cf18649364717f3eb18ff9d8936c90 · ionic-team/capacitor-site · GitHub
My replies as follows:

  1. Not possible in a PWA, only possible with the @capacitor/app plugin on Android.
    ANSWER:
    Please give me some codes to try.

  2. Impossible to tell without your code.
    ANSWER:
    Please look inside the data.js file.
    Its inside the function showebook(filename, svolume, fromlocalstorage).
    On line 1786.

  3. You can use LocalStorage with Capacitor.
    ANSWER:
    I wrote the follow code but they generate error:
    async function retrieveValue(skey) {
    //const checkName = async () => {
    var sdata = await Storage.get({ key: skey });
    return sdata;
    //};
    };
    I call the function this way:
    alert(retrieveValue(“k1”));

  4. You can use LocalStorage with Capacitor.
    ANSWER:
    I wrote the follow code but they generate error:
    async function storeValue(skey, sdata) {
    //const setName = async () => {
    await Storage.set({ key:skey, value:sdata });
    //};
    };
    I call the function this way:
    storeValue(“k1”, “Some Data”);

  5. You can’t lock rotation in a PWA, but you can kind of fake it:
    ANSWER:
    I tried the following code but no good results. Please open the data.js file.
    Its inside the Onstart() function in data.js. Line 2133
    screen.orientation.lock(“portrait”);

  6. Same as 1, Not possible in a PWA

  7. You can use a , or any other custom UI on your web app. This is unrelated to Capacitor.
    ANSWER:
    I have decided to use a snakbar/toast instead. See the capacitorcodes.js file. Plase how do I implement the ff code?
    async function showPopup() {
    const showHelloToast = async () => {
    await Toast.show({
    text: ‘Hello!’,
    });
    };
    }

  8. The @capacitor/app-launcher plugin: Capacitor - build cross platform apps with the web.
    ANSWER:
    I have the following code but generates error:
    async function openURL(){
    try {
    ret = await App.openUrl({
    //url: ‘com.getcapacitor.myapp://page?id=ionicframework’,
    url: ‘www.google.com’,
    });
    alert('Open url response: '+ ret);
    } catch(e) {
    alert('Open url error: '+ e);
    }
    }

  9. The @capacitor/filesystem plugin: Capacitor - build cross platform apps with the web.
    ANSWER:
    I have written the following code in capacitorcodes.js. When called as mkdir() from
    windows.onload it tells me that the directory already exist but I cant find it on my PC.
    Please look inside the capacitorcodes.js file
    async function mkdir(){
    try {
    let ret = await Filesystem.mkdir({
    path: ‘secrets’,
    directory: Directory.Documents,
    recursive: false // like mkdir -p
    });
    } catch(e) {
    //alert(Directory.Documents);
    alert(‘Unable to make directory’+ e);
    }
    }

  10. The @capacitor/filesystem plugin: Capacitor - build cross platform apps with the web.
    ANSWER:
    I have written the following code in capacitorcodes.js. When called as mkdir() from
    windows.onload it tells me that the directory already exist but I cant find it on my PC.
    Please look inside the capacitorcodes.js file
    async function rmdir() {
    try {
    let ret = await Filesystem.rmdir({
    path: ‘secrets’,
    directory: Directory.Documents,
    recursive: false,
    });
    } catch(e) {
    console.error(‘Unable to remove directory’, e);
    }
    }

  11. The @capacitor/filesystem plugin: Capacitor - build cross platform apps with the web.
    ANSWER:
    I have written the following code in capacitorcodes.js. When called as mkdir() from
    windows.onload it tells me that the directory already exist but I cant find it on my PC.
    Please look inside the capacitorcodes.js file
    async function readdir() {
    try {
    let ret = await Filesystem.readdir({
    path: ‘secrets’,
    directory: Directory.Documents
    });
    } catch(e) {
    console.error(‘Unable to read dir’, e);
    }
    }

  12. The @capacitor/haptics plugin: Capacitor - build cross platform apps with the web
    ANSWER:
    I have the following codes. Please look in data.js line 3832:
    function vibrateDevice(vibratetype){
    if (window.navigator && window.navigator.vibrate) {
    if(vibratetype.toUpperCase()==“SHORT”){
    navigator.vibrate(1000);
    }else if (vibratetype.toUpperCase()==“LONG”){
    navigator.vibrate([1000, 500, 1000, 500, 2000]);
    }
    } //else {}
    }
    I call as follows:
    vibrateDevice(“long”);

  13. The @capacitor/toast plugin: Capacitor - build cross platform apps with the web
    ANSWER:
    I have the following codes in my capacitorcodes.js file.
    async function showPopup() {
    const showHelloToast = async () => {
    await Toast.show({
    text: ‘Hello!’,
    });
    };
    }


My folder structure

testapp
package-lock.json
package.json
capacitor.config.json
webpack.config.js
.gitignore
README.md
ios
android
node_modules
src
index.js
www
index.html
manifest.json
css
font-awesome
Img
js
data.js
capacitorcodes.js
main.js

I have attached my project for perusal. Kindly take a look

Here is a link to the project please: