UNPKG

3.29 kBJavaScriptView Raw
1/***************************************************************************************
2 * (c) 2017 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 ****************************************************************************************/
12
13/**
14 * Loads the child portion of extension bridge. This loader is intended to be hosted on a CDN
15 * which extensions will then load. The loader file then loads in the child for that
16 * particular environment by incorporating document.referrer into the script URL (see below).
17 * The reason this is done is as follows: Assume parent v1 and child v1 are both on production.
18 * We then want to release parent v2 and child v2. Assume that parent v2 is incompatible with
19 * child v1 and likewise child v2 is incompatible with parent v1. By incompatible, we don't mean
20 * that the API the extension is consuming has changed, but rather the ways the parent and child
21 * communicate has changed. If we wanted to test an extension in QE using parent v2 and child v2,
22 * we would be unable to do so without this loader because the extension would be always be
23 * loading child v1 while Lens would be loading parent v2. By using this loader, the extension
24 * will load child v2 in qe and we can appropriately test.
25 */
26// Prevent double-loading of extension bridge.
27if (!window.extensionBridge) {
28 const childPath = '/extensionbridge/extensionbridge-child.js';
29 const bridge = window.extensionBridge = {
30 _callQueue: []
31 };
32 const anchor = document.createElement('a');
33 anchor.href = document.referrer;
34 const childURL = anchor.protocol + '//' + anchor.hostname + (anchor.port ? ':' + anchor.port : '') + childPath;
35 ['openCodeEditor', 'openDataElementSelector', 'openRegexTester', 'register', 'setDebug'].forEach(methodName => {
36 // We'll return a promise immediately for every method. Some of the underlying methods don't
37 // actually return anything, but it's simplest to just return a promise for all methods here.
38 // Once the extension bridge child is loaded, it will process everything in the _callQueue
39 // and resolve/reject the promises that we've returned to the user here.
40 bridge[methodName] = function () {
41 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
42 args[_key] = arguments[_key];
43 }
44
45 return new Promise((resolve, reject) => {
46 bridge._callQueue.push({
47 methodName,
48 args,
49 resolve,
50 reject
51 });
52 });
53 };
54 });
55 const script = document.createElement('script');
56 script.async = true;
57 script.src = childURL;
58 const firstDocScript = document.getElementsByTagName('script')[0];
59 firstDocScript.parentNode.insertBefore(script, firstDocScript);
60}
\No newline at end of file