forked from MrSwitch/hello.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.phonegap.js
More file actions
97 lines (74 loc) · 2.61 KB
/
hello.phonegap.js
File metadata and controls
97 lines (74 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// 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;
};
})();