-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinject-logon.js
190 lines (153 loc) · 5.83 KB
/
inject-logon.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* jshint browser: true, devel: true, -W027 */
/* global unescape */
// For some reason, a Chrome Packaged App, using executeScript,
// cannot access or make changes to the window object in the
// guest webview. However, it can inject another script into
// the webview document, in which case, that script will be
// treated like native code, instead of code specifically
// injected by the app. So... let's see how much we can
// exploit that.
var script = document.createElement('script');
script.textContent = '!' + function() {
/////////////////////////////////////////
// This file should only be executed once
/////////////////////////////////////////
// this is particularly an issue when the user has to sign in
// before starting to use the app
if (window.__hipchatXhrBootstrap__) {
return;
}
window.__hipchatXhrBootstrap__ = true;
console.log('EMBEDDING LOGON');
/////////////////////////////////////////
// Register messaging with the parent
/////////////////////////////////////////
var appWindow, appOrigin;
// catch messages from the parent
function onMessage(e) {
if (!appWindow || !appOrigin) {
appWindow = e.source;
appOrigin = e.origin;
}
if (e.data && e.data.type === 'logon') {
attemptLogon(e.data);
}
}
// send messages to the parent
// since this is Chrome, we can use JSON objects
function send(data) {
if (appWindow && appOrigin) {
appWindow.postMessage(data, appOrigin);
}
}
// create a dedicated notify method
function notify(opts) {
return send({
type: 'notification',
title: opts.title,
message: opts.body
});
}
window.addEventListener('message', onMessage);
/////////////////////////////////////////
// Attach password stuff
/////////////////////////////////////////
var SIGNIN_REGEX = /\/sign_in/;
var PASS_REGEX = /\/login_password/;
var query = (function parseQuery(args){
var query = {};
for (var i = args.length; i--;) {
var q = args[i].split('=');
query[q.shift()] = unescape(q.join('='));
}
return query;
}(window.location.search.substring(1).split('&')));
function newSigninEmailWorkflow(opts) {
// in this workflow, we have an email box on
// one page, and then a password box on the next page
// TODO what do I do here? There is nothing to do on the first
// page. I would need to hide the loader, unless we already
// have login data and we need to attempt a login.
}
function attachPasswordWorkflow() {
var passwordField = document.querySelector('#password');
var signInButton = document.querySelector('#signin');
var rememberCheckbox = document.querySelector('#stay_signed_in');
if (!passwordField || !signInButton) {
return;
}
signInButton.addEventListener('click', function() {
// If the checkbox is not found for some reason, or it is not
// checked, do not remember this login.
if (!rememberCheckbox || !rememberCheckbox.checked) {
return;
}
send({
type: 'account',
email: query.email,
password: passwordField.value
});
});
}
function attach() {
var emailField = document.querySelector('#email');
var passwordField = document.querySelector('#password');
var signInButton = document.querySelector('#signin');
var rememberCheckbox = document.querySelector('#stay_signed_in');
if (emailField && signInButton && !passwordField) {
return newSigninEmailWorkflow({
emailField: emailField,
signInButton: signInButton
});
}
if (!emailField || !passwordField || !signInButton) {
return;
}
signInButton.addEventListener('click', function() {
// If the checkbox is not found for some reason, or it is not
// checked, do not remember this login.
if (!rememberCheckbox || !rememberCheckbox.checked) {
return;
}
send({
type: 'account',
email: emailField.value,
password: passwordField.value
});
});
}
function attemptLogon(data) {
console.log('attempt login', data);
var emailField = document.querySelector('#email');
var passwordField = document.querySelector('#password');
var signInButton = document.querySelector('#signin');
if (!signInButton) {
return;
}
// In the new workflow, we get these fields one
// at a time, so fill in as much as are present.
if (emailField) {
emailField.value = data.email;
}
if (passwordField) {
passwordField.value = data.password;
}
signInButton.removeAttribute('disabled');
signInButton.click();
}
if (SIGNIN_REGEX.test(window.location.href)) {
if (document.readyState === 'complete') {
attach();
} else {
window.onload = attach;
}
} else if (PASS_REGEX.test(window.location.href)) {
if (document.readyState === 'complete') {
attachPasswordWorkflow();
} else {
window.onload = attachPasswordWorkflow;
}
}
} + '();';
// inject the script into the webview document
(document.head || document.documentElement).appendChild(script);