forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
165 lines (145 loc) · 5.03 KB
/
app.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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict;'
const kWhitelistedExtensionId = 'dpicdiinminmigempanoghnpckmkfepi';
const kExtensionIds = [kWhitelistedExtensionId];
let portMap = {};
let webview = null;
let initScripts = [];
function createWebView(initScripts) {
let container = document.getElementById('webview-container');
webview = document.createElement('webview');
webview.partition = 'partition';
webview.style = 'width: 640px; height: 400px';
webview.addContentScripts([{
// The value of |embedder| in the script below will persist, and can be
// used by content script injected into the guest any time to send data
// back to us.
name: 'embedderVar',
matches: ['<all_urls>'],
js: {code: 'var embedder = null;\n'},
all_frames: true,
run_at: 'document_start'
}]);
webview.addContentScripts(initScripts);
webview.addEventListener('contentload', function() {
webview.executeScript({
code: 'window.addEventListener(\'message\', function(e){\n' +
' if (e.data != \'connect\')\n' +
' return;\n' +
' console.log(\'msg = \' + e.data);\n' +
' embedder = e.source;\n' +
' embedder.postMessage(JSON.stringify({\n' +
' \'ext_id\' : \'0\',\n' +
' \'msg\' : \'Hello from guest!\'}), \'*\');\n' +
'});'
});
// Here we aren't enforcing any particular origin on the guest, but we
// could if security needs required it.
webview.contentWindow.postMessage('connect', '*');
});
addEventListener('message', function(e) {
if (e.source != webview.contentWindow)
return;
let data;
try {
data = JSON.parse(e.data);
} catch (err) {
console.warn('invalid JSON format for incoming message: ' + err.message);
}
if (!data) {
console.warn('Malformed message: ' + e.data);
return;
}
if (data.extensionId) {
if (!data.extensionId in portMap) {
console.warn('Message for unknown extension: ' + data.extensionId);
return;
}
if (!data.request) {
console.warn('malformed messgae (no request):' + e.data);
return;
}
// Relay this to the appropriate extension.
// If the app wants to monitor messages from injected code, this is one
// place to do it.
console.log('Request: ' + data.request + ' => ' + data.extensionId);
portMap[data.extensionId].postMessage({request: data.request});
} else {
if (!data.msg) {
console.warn('malformed message (no msg): ' + e.data);
}
console.log('Message for us: ' + data.msg);
}
});
webview.src = 'http://example.com';
container.appendChild(webview);
}
function connectToExtension(extensionId) {
let port;
try {
port = chrome.runtime.connect(extensionId);
} catch (e) {
console.error('Could not connect to extension: ' + e.message);
return;
}
// Save port in map.
portMap[extensionId] = port;
port.onDisconnect.addListener(() => {
delete portMap[extensionId];
});
let initPromise = new Promise((resolve, reject) => {
port.onMessage.addListener(function(msg) {
// Perhaps check here to make sure |msg.code| exists.
console.log('Incoming from extension: ' + msg.name + ' -> ' + msg.code);
if (msg.name == 'ext_getInitScripts') {
initScripts.push({
name: 'initScripts-' + extensionId,
matches: ['<all_urls>'],
js: {code: msg.code},
run_at: 'document_start'
});
resolve();
} else {
webview.executeScript({code: msg.code});
}
});
setTimeout(function() {
reject('Timeout waiting for initScripts from ' + extensionId);
}, 5000);
});
port.postMessage({request: 'getInitScripts'});
return initPromise;
}
function setUpExtensionHandlers() {
let port = portMap[kWhitelistedExtensionId];
// Some examples of UI in the app requesting services from the guest.
// The replies from the extension are injected into the guest and executed
// via the port.onMessage handler declared above.
document.getElementById('magnify_button')
.addEventListener('click', function() {
port.postMessage({request: 'magnify'});
});
document.getElementById('background_button')
.addEventListener('click', function() {
port.postMessage({request: 'setBackground'});
});
document.getElementById('add_div_button')
.addEventListener('click', function() {
port.postMessage({request: 'addDiv'});
});
document.getElementById('iframe_dataurl_button')
.addEventListener('click', function() {
port.postMessage({request: 'iFrameDataURL'});
});
}
document.addEventListener('DOMContentLoaded', function() {
promises = [];
for (let extensionId of kExtensionIds)
promises.push(connectToExtension(extensionId));
setUpExtensionHandlers();
Promise.all(promises).then(function() {
createWebView(initScripts);
});
});