forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
71 lines (60 loc) · 5.72 KB
/
background.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
// 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;'
// Define globals.
// This DataURI image will allow us to demonstrate injecting non-html content
// into a guest.
var chromiumLogoDataURI = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABHNCSVQICAgIfAhkiAAACmlJREFUaIG9mmtsXMd1x39n5i53yeVDlESRlERaVEXKqmq7iZW6lZyoTtzYQhuraGK7RSCnTZAoMGw0Sd0U/UQILfqhddqkeRQN0CA2EjT1AwjSvFzYUOyEslzZkS1XimW2jiTqRVkiKT72de/M6Ye7S652l8sl7fgAF7t75szM/3/mzMyZuSu8DTI8fLzpDaKhqWl3y5wGv0UgN0XebiJyqwCsmEnv3Rk1/mizFF5MJd0LZ58+9tpLL+0P32rfstKKu4cPBmuijp1Xr6b+IkqYuwqhLhRqtX2lKhEI1uW/m05Of+kHX3j6OTjgV4Jj2QSGh483/Twb7btaCP4xikz7InirlLVstKgMrL/anJj7y86x9COPP769sBw8yyLwh391bM+VTPDvTqVjKXDLIVASQzjREczs+/E/7/pho5gaIrDvH15JX7iQeCIbmjvjjqvhVGkaCKPKZkq/kzbzVI//v488/rV7ZpfCtiSBP/mbE4Nnx8PDXpKrFzr61RIAsBpeSSXP7Tr4pT0n6+GrS2Dv8NGdk5cTP3UmMFVgKnp/uwkAOHFujZ5733/9yx2HFsO4KIG9w6/unJwMRryC1jB7JwgAiFFaE+d3PvPF25+vhbMmgbs/f2zrhRk9oUHCQJXz35EQWqijROLd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vBXg1Re2ivch3gvqHZ1pZeO61bXrL9F+oRD+x/4PrnmhXFeVfARtV/f5MGw4wSs/Q9cUr/EI+AjnHdcPdFQnbQ2Ad4XwwtmJU/dV6qtaeuTPBnJ5M7vDF/KusqwcbF3Q8/aK1zjPV1X6uxKs6+yotlsCvM/nwqlc7j0H7ql+AVgz/fvO/RvGLHM3e+8bBluTgFecd/EIhCFDm1Y1dP95DXj1hC7zrs/9wZpztcoXzV+/8eDGVySau1ELuaqRWBI44Oe9H4GP2HZdC+lU6hqbRjwfZqd+41N39hxfzKZuAv7NB7tfzQfZgTCTv9gI6HJQWryh9j6iOaH0bei4xnYpCbPhhcl8OFAPPCxBAOJwmggub8rn5p5cDHAlIC3GvfOKho5t/c0kg0RDXldVMpm5x8auntq0WNiUy7JedN/38Nj7okT6yUQytXjKoeC9w0ceFxVY3Zzllu3dVN691pK5fP5Nn5ve+8CHNtR8oVdLlnWGe/ShvufCnz3dk81M3xuFmYlaNqrxiuOdw4V5BvvblgRfyMxezs5MfXj89tae5YCHt/BnD1TlY1848+7QJv/cJ+xHk0HaqCrqPN55fJinf62yfXNnzbDJZGYV9FthPvdPD961/mURWdFlxcoJlMnwsJrRVWc24e3NKsEtuSi7Q1xu867NbetaOlsiCnrBWn/OGfO8cf7lQjT335c/dN3p8pRgpfL/IYD7eLJbZAcAAAAASUVORK5CYII=';
var iframeDataURL =
'data:text/html,' +
'<html>' +
' <head>' +
' <script>' +
' document.addEventListener("DOMContentLoaded", function(){' +
' document.getElementById("target").innerText = "Fred.";' +
' });' +
' </script>' +
' </head>' +
' <body>' +
' <h1>Data URL IFrame</h1>' +
' <img src="' + chromiumLogoDataURI + '"><br>' +
' <p>A data-url based iframe that has scripts.</p>' +
' <p id="target"></p>' +
' </body>' +
'</html>';
let handlers = {};
handlers['magnify'] =
'els = document.getElementsByTagName(\'div\'); ' +
'for (i = 0; i < els.length; i++) {' +
' els[i].style.fontSize = "200%";' +
'}'
handlers['setBackground'] =
'document.body.style.backgroundColor=\'pink\';';
handlers['addDiv'] =
'el = document.createElement(\'div\'); ' +
'el.innerText = \'Greetings from the extension!\'; ' +
'document.body.appendChild(el);';
handlers['iFrameDataURL'] =
'el = document.createElement(\'iframe\');\n' +
'document.body.appendChild(el);\n' +
'el.src = \'' + iframeDataURL + '\';';
handlers['getInitScripts'] =
'window.addEventListener(\'keypress\', function(e){\n' +
' console.log(\'keypress!!\');\n' +
' if (!embedder)\n' +
' return;\n' +
' if (e.keyCode == 109)\n' +
' embedder.postMessage(JSON.stringify({\n' +
' \'extensionId\' : \'' + chrome.runtime.id + '\',\n' +
' \'request\' : \'magnify\'}), \'*\');\n' +
'});';
chrome.runtime.onConnectExternal.addListener(function(port) {
port.onMessage.addListener((msg) => {
if (!handlers.hasOwnProperty(msg.request)) {
console.error('Unknown request: ' + msg.request);
return;
}
port.postMessage({code: handlers[msg.request], name: 'ext_' + msg.request});
});
});