-
Notifications
You must be signed in to change notification settings - Fork 2
/
inject-notifications.js
181 lines (153 loc) · 5.43 KB
/
inject-notifications.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
/* jshint browser: true, devel: true */
// 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.__hipchatNotificationBootstrap__) {
return;
}
window.__hipchatNotificationBootstrap__ = true;
console.log('EMBEDDING NOTIFICATION');
/////////////////////////////////////////
// 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;
}
}
// 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);
/////////////////////////////////////////
// Overload the Notification object
/////////////////////////////////////////
// just in case, save the original
var OriginalNotification = window.Notification;
var Notification = function(title, options) {
options = options || {};
var id = (Math.floor(Math.random() * 9007199254740992) + 1).toString();
console.log('notification: ' + title, options);
Object.defineProperties(this, {
'title': {
value: title,
writable: false,
enumerable: true
},
'dir': {
value: options.dir || 'auto', // ltr or rtl
writable: false,
enumerable: true
},
'lang': {
value: options.lang || 'en-US',
writable: false,
enumerable: true
},
'body': {
value: options.body || '',
writable: false,
enumerable: true
},
'tag': {
value: options.tag || id,
writable: false,
enumerable: true
},
'icon': {
value: options.icon || undefined,
writable: false,
enumerable: true
},
'data': {
value: options.data || undefined,
writable: false,
enumerable: true
},
'silent': {
value: options.silent || false,
writable: false,
enumerable: true
},
});
notify({
title: title,
body: options.body
});
};
// static methods and members
Object.defineProperties(Notification, {
'permission': {
value: 'granted',
writable: false
}
});
Notification.requestPermission = function requestPermission(callback) {
console.log('notification permission request');
setTimeout(function(){
callback(Notification.permission);
}, 0);
};
// use original request for permission
// Notification.requestPermission = OriginalNotification.requestPermission;
// instance methods
// TODO do I need to implement these?
Notification.prototype.close = function close() { };
Notification.prototype.onclick = function onclick() { };
Notification.prototype.onerror = function onerror() { };
// overload the window notification with this new one
window.Notification = Notification;
/////////////////////////////////////////
// Watch for the connection lost message
/////////////////////////////////////////
// Note: HipChat removed this... it seems like the connection is more stable now
// var connectionMessage = document.querySelector('.hc-connection-notification');
//
// console.log('connection box is', connectionMessage);
//
// var observer = new MutationObserver(function(mutations) {
// if (connectionMessage.classList.contains('transparent')) {
// console.log('connection is fine');
// } else {
// console.log('connection is lost');
//
// return send({
// type: 'connection',
// state: 'lost'
// });
// }
// });
//
// observer.observe(connectionMessage, {
// attributes: true,
// attributeFilter: ['class']
// });
} + '();';
// inject the script into the webview document
(document.head || document.documentElement).appendChild(script);