TruckMovers/cordova-plugin-remote-injection

How to open a URL by using this plugin

Closed this issue · 1 comments

In the readme file, it just mentions how to configure the plugin. Still confuse how to use it to open a url like "http://192.168.1.100:8100"

In the config.xml, I put the following line:

<preference name="CRIInjectFirstFiles" value="www/js/openURL.js" />

The openURL.js file opens an url like the following:

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    onDeviceReady: function() {
         this.receivedEvent('deviceready');
         window.open('http://192.168.1.100:8100', '_self ', 'location=yes');
     },

     receivedEvent: function(id) {
         var parentElement = document.getElementById(id);
         var listeningElement = parentElement.querySelector('.listening');
         var receivedElement = parentElement.querySelector('.received');
 
         listeningElement.setAttribute('style', 'display:none;');
         receivedElement.setAttribute('style', 'display:block;');
 
         console.log('Received Event: ' + id);
     }
 };

 app.initialize();

'http://192.168.1.100:8100' appoints to an remote ionic/cordova app which gets involved by 'ionic serve --address 192.168.1.100'. Now, I can visit the link from another cordova app on my cell phone. The link will be opened within the cordova app on my cell phone.

The reason for using this plugin:

How to let the remote ionic/cordova app considers itself running on a cell phone instead of a browser. Therefore, the remote ionic/cordova app can use the camera or other equipments of my local cell phone.

This plugin will only inject Cordova into the document loaded into the main web view. window.open() will attempt to open the page in a new browser window which the plugin won't have any visibility into. If you want to update the URL of the main web view to have cordova injected then you'll need to use something like window.location.

https://developer.mozilla.org/en-US/docs/Web/API/Window/location