Cordova inAppBrowser存储并重用凭据

时间:2018-04-03 06:38:21

标签: javascript cordova local-storage inappbrowser

案例: 我有一个简单的cordova应用程序,使用inAppBrowser插件打开(Drupal 7)网站的移动版本。在网站上,用户可以登录以阅读更多内容。此外,安装了应用程序的每个人都可以获得推送通知。

问题:一段时间后,已登录的用户正在注销。它发生在iOS和Android上。

现在我希望应用程序存储用户名和密码的填写值。因此,当用户注销时,它已填写了凭据。是否有人能够提供帮助?

我在网上发现了一些类似的问题,但我似乎无法使用localStorage让它工作。

这是我在this问题中找到的片段。但我无法让它发挥作用。 edit-nameedit-pass是网站上输入字段的ID。

function loadStopped() {
           var username = "";
           if (localStorage.getItem("username") !== null) {
               username = localStorage.getItem("username");
           }
           var password = "";
           if (localStorage.getItem("password") !== null) {
               password = localStorage.getItem("password");
           }
           document.getElementById("edit-name").value = username;
           document.getElementById("edit-pass").value = password;

           document.getElementById("the_form").addEventListener("submit", function() {
               localStorage.setItem("username", document.getElementById("edit-name").value);
               localStorage.setItem("password", document.getElementById("edit-pass").value);
           });
       }

我的完整代码:



var app = {

        // Application Constructor
        browser: null,
        host: 'http://www.somewebsite.com/',

        initialize: function () {
            this.bindEvents();
        },

        // Bind Event Listeners
        bindEvents: function () {
            document.addEventListener('deviceready', this.onDeviceReady, false);

        },

        // deviceready Event Handler

        onDeviceReady: function () {
            app.checkConnection();
            app.initPushwoosh();
        },

        checkConnection: function () {
            var networkState = navigator.connection.type;

            var states = {};
            states[Connection.NONE] = 'no connection';

            //if no connection show popup
            if (networkState === Connection.NONE) {
                navigator.notification.alert(
                    'Please enable your internet connection\nand restart the app',  // message
                    function () {
                    },         // callback
                    'Oops no internet',            // title
                    'Ok'                  // buttonName
                );
                //if network is found, launch inappbrowser
            } else {
                app.openBrowser('/');

            }
        },

        openBrowser: function (url) {
            
           function loadStopped() {
               var username = "";
               if (localStorage.getItem("username") !== null) {
                   username = localStorage.getItem("username");
               }
               var password = "";
               if (localStorage.getItem("password") !== null) {
                   password = localStorage.getItem("password");
               }
               document.getElementById("edit-name").value = username;
               document.getElementById("edit-pass").value = password;
               
               document.getElementById("the_form").addEventListener("submit", function() {
                   localStorage.setItem("username", document.getElementById("edit-name").value);
                   localStorage.setItem("password", document.getElementById("edit-pass").value);
               });
           }

            if (url.indexOf('/') !== 0) {
                url = '/' + url;
            }
            url = this.updateQueryStringParameter(url, 'app_os', 'mobile');

            window.open = cordova.InAppBrowser.open;
            this.browser = cordova.InAppBrowser.open(this.host + url, '_blank', 'location=no,hidden=no');
            
           this.browser.addEventListener('loadstop', loadstopped);
            
            this.browser.addEventListener('loadstop', function(e)){
                var username="";
                if (localStorage.getItem("username") !== null){
                    username = localStorage.getItem("username");
                }
                
                this.browser.executeScript({
                                           code:"document.getElementById('edit-name').value;"
                                           });
            });


             this.browser.addEventListener('exit', function (e) {
                 if (navigator.app) {
                     navigator.app.exitApp();
                 } else if (navigator.device) {
                     navigator.device.exitApp();
                 } else {
                     window.close();
                 }
             });
            
            

        },


        // Update DOM on a Received Event
        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);
        },

        updateQueryStringParameter: function(uri, key, value) {
            // remove the hash part before operating on the uri
            var i = uri.indexOf('#');
            var hash = i === -1 ? ''  : uri.substr(i);
            uri = i === -1 ? uri : uri.substr(0, i);

            var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
            var separator = uri.indexOf('?') !== -1 ? "&" : "?";

            if (!value) {
                // remove key-value pair if value is empty
                uri = uri.replace(new RegExp("([&]?)" + key + "=.*?(&|$)", "i"), '');
                if (uri.slice(-1) === '?') {
                    uri = uri.slice(0, -1);
                }
            } else if (uri.match(re)) {
                uri = uri.replace(re, '$1' + key + "=" + value + '$2');
            } else {
                uri = uri + separator + key + "=" + value;
            }
            return uri + hash;
        }

};

app.initialize();




任何可以告诉我自己做错了什么或者能给我一个详细的指南的人。我怎么能让它发挥作用?

提前多多感谢!

0 个答案:

没有答案