打开phonegap的inAppBrowser中的链接

时间:2016-11-30 04:09:31

标签: javascript cordova inappbrowser

我很难在appBrowser中尝试打开phonegap(cordova' s)中的链接。

我需要能够制作它,以便IAB可以打开本地手机屏幕链接(文件:///android_assets/www/example.html)和在线链接(例如https://www.google.com

到目前为止,我已经通过在index.js中创建一个创建一个启动IAB对象的对象来实现这一目标,然后对于那里的每个调用我都认为我可以打开该链接。

  • 我发现了一些问题,例如当我尝试使用window.location = some_url时,它会将index.html页面更改为some_url,因此我附加的JS不再有效。< / p>

  • 我尝试从我的index.html的JS中注入一个window.location到IAB文档中,但在尝试获取本地文件时出现故障

  • 我当前的方法导致内存泄漏,因为它不断打开IAB对象,因此我很难关闭...

  • 在IAB对象上使用.close()方法显示(根据Chrome的远程设备视图)它实际上并未删除IAB窗口,而是将其转换为:新标签

下面是我在index.js中调用的JS的当前代码(对不起,我知道那里有很多冗余代码!)

var interceptor = {
// library dictionary object of <K,V> pairs in the form of <current page, desired page>
// insert your k,v pairs here:
library: {
    'login/signup.php': 'example.html', //test data
    'calendar/view.php': 'example.html',
    'course/view.php?id=10': 'example.html'
},
// origin dictionary
// dictionary for redirecting from the phonegap page to the moodle page
// must be explicit in your page names and avoid duplication
// e.g. use full path notation -> full/path/to/file.html not file.html
origin: {
    'current/attempt': ['example1.html', 'example2.html', 'course/index.php'] //test data
},
history_stack: [],
// stack of all current windows
browser_windows: [],
is_back: 'false',
windows: 0,
redirect_flag: false,
first: true,
currentWindow: null,
currentLocalFile: null,
get_origin: function() {
    return this.origin;
},
get_library: function() {
    return this.library;
},
// interceptor constructor
initialize: function(old_ref = null, default_url = config.moodleURL, android = true) {
    // interval for checking our back flag
    // the scope changes when you enter the anonymous closure and this changes. this is a hack for that
    var self = this;
    var ref;
    console.log('default url: ' + default_url);
    // check for android
    if ((android) && (self.windows < 1)) {
        // self.browser_windows[0] = cordova.InAppBrowser.open(default_url, '_blank', 'location=no,zoom=no');
        ref = cordova.InAppBrowser.open(default_url, '_blank', 'location=yes,zoom=no');
        self.first = false;
    }
    // otherwise iOS
    else if ((!android) && (self.windows < 1)) {
        // self.browser_windows[0] = cordova.InAppBrowser.open(default_url, '_blank', 'location=no,zoom=no,suppressesIncrementalRendering');
        ref = cordova.InAppBrowser.open(default_url, '_blank', 'location=no,zoom=no,suppressesIncrementalRendering');
        // self.browser_windows.push(ref);
        self.first = false;
    } else {
        old_ref = 
        // old_ref = cordova.InAppBrowser.open(default_url, '_blank', 'location=yes,zoom=no');
        ref = old_ref;
    }
    self.windows++;
    var library_dictionary = this.get_library();
    var origin_dictionary = this.get_origin();
    var redirect_URL;
    ref.addEventListener('loadstart', function(event) {
        // push current page to histoy stack
        self.history_stack.push(event.url);
        // check to see if an element of one of our origin arrays is in the URL
        for (var origin_list in origin_dictionary) {
            if (event.url.includes(origin_list)) self.redirect_flag = true;
            for (var elements in origin_dictionary[origin_list]) {
                // if it is raise a flag and store the key that its array maps to
                if ((event.url.includes(origin_dictionary[origin_list][elements])) && (self.redirect_flag)) {
                    redirect_URL = origin_list;
                    self.redirect_flag = false;
                    var temp_previous_pages = self.history_stack;
                    var temp_element;
                    // pop elements of stack until empty
                    while (temp_previous_pages.length !== 0) {
                        temp_element = temp_previous_pages.pop();
                        // if we find an element in the stack (our URL history) that matches the key that our array mapped to
                        if (temp_element.indexOf(redirect_URL) !== -1) {
                            // redirect and break from loop
                            self.initialize(ref, temp_element, android);
                            break;
                        }
                    }
                }
            }
        }
        for (var key in library_dictionary) {
            if (event.url.includes(key)) {
                self.initialize(ref, library_dictionary[key], android);
                break;
            }
        }
    });
    ref.addEventListener('loadstop', function() {
        // when we've finished loading our page set up an interval every 2.5 seconds to check
        // if we've been signalled to change pages
        console.log('here');
        var is_back_interval = setInterval(function() {
            ref.executeScript({
                code: "localStorage.getItem('is_back')"
            }, function(values) {
                if (values[0] === 'true') {
                    // if we have been signalled then remove that signal from our html or moodle page
                    ref.executeScript({
                        code: "localStorage.setItem('is_back', '')"
                    });
                    // get 3rd last since the last will be the current page as will the 2nd last
                    prev_page = self.history_stack[self.history_stack.length - 3];
                    self.initialize(ref, prev_page, android);
                }
            });
        }, 2500);
    });
}

};

和heres index.js

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {

    if (window.device.platform === "iOS"){
        interceptor.initialize(config.moodleURL, false);
    } else {
        interceptor.initialize();
    }

}

};

example.html:https://gist.github.com/anonymous/28a78ab0879878d7a9dac8eb89544cda

0 个答案:

没有答案