hellojs-xiaotian
Version:
A clientside Javascript library for standardizing requests to OAuth2 web services (and OAuth1 - with a shim)
98 lines (74 loc) • 2.67 kB
JavaScript
// Phonegap override for hello.phonegap.js
(function() {
// Is this a phonegap implementation?
if (!(/^file:\/{3}[^\/]/.test(window.location.href) && window.cordova)) {
// Cordova is not included.
return;
}
// Augment the hidden iframe method
hello.utils.iframe = function(url, redirectUri) {
hello.utils.popup(url, redirectUri, {hidden: 'yes'});
};
// Augment the popup
var utilPopup = hello.utils.popup;
// Replace popup
hello.utils.popup = function(url, redirectUri, options) {
// Run the standard
var popup = utilPopup.call(this, url, redirectUri, options);
// Create a function for reopening the popup, and assigning events to the new popup object
// PhoneGap support
// Add an event listener to listen to the change in the popup windows URL
// This must appear before popup.focus();
try {
if (popup && popup.addEventListener) {
// Get the origin of the redirect URI
var a = hello.utils.url(redirectUri);
var redirectUriOrigin = a.origin || (a.protocol + '//' + a.hostname);
// Listen to changes in the InAppBrowser window
popup.addEventListener('loadstart', function(e) {
var url = e.url;
// Is this the path, as given by the redirectUri?
// Check the new URL agains the redirectUriOrigin.
// According to #63 a user could click 'cancel' in some dialog boxes ....
// The popup redirects to another page with the same origin, yet we still wish it to close.
if (url.indexOf(redirectUriOrigin) !== 0) {
return;
}
// Split appart the URL
var a = hello.utils.url(url);
// We dont have window operations on the popup so lets create some
// The location can be augmented in to a location object like so...
var _popup = {
location: {
// Change the location of the popup
assign: function(location) {
// Unfourtunatly an app is may not change the location of a InAppBrowser window.
// So to shim this, just open a new one.
popup.executeScript({code: 'window.location.href = "' + location + ';"'});
},
search: a.search,
hash: a.hash,
href: a.href
},
close: function() {
if (popup.close) {
popup.close();
try {
popup.closed = true;
}
catch (_e) {}
}
}
};
// Then this URL contains information which HelloJS must process
// URL string
// Window - any action such as window relocation goes here
// Opener - the parent window which opened this, aka this script
hello.utils.responseHandler(_popup, window);
});
}
}
catch (e) {}
return popup;
};
})();