Skip to content

Commit

Permalink
[amp-viewer-integration] Don't import from AMP, use regex as constant (
Browse files Browse the repository at this point in the history
…#32109)

* dont import from amp, use regex as constant

* pass regex as param
  • Loading branch information
Enriqe committed Jan 27, 2021
1 parent 8887dff commit 5cbe2a0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
14 changes: 10 additions & 4 deletions extensions/amp-viewer-integration/0.1/messaging/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

import {urls} from '../../../../src/config';

const TAG = 'amp-viewer-messaging';
const CHANNEL_OPEN_MSG = 'channelOpen';
const HANDSHAKE_POLL_MSG = 'handshake-poll';
Expand Down Expand Up @@ -163,17 +161,25 @@ export class Messaging {
* @param {!Window} target - window containing AMP document to perform handshake with (usually contentWindow of iframe)
* @param {string} origin - origin of target window (use "null" if opaque)
* @param {?string=} opt_token - message token to verify on incoming messages (must be provided as viewer parameter)
* @param {?RegExp=} opt_cdnProxyRegex
* @return {!Promise<!Messaging>}
*/
static waitForHandshakeFromDocument(source, target, origin, opt_token) {
static waitForHandshakeFromDocument(
source,
target,
origin,
opt_token,
opt_cdnProxyRegex
) {
return new Promise((resolve) => {
const listener = (event) => {
const message = parseMessage(event.data);
if (!message) {
return;
}
if (
(event.origin == origin || urls.cdnProxyRegex.test(event.origin)) &&
(event.origin == origin ||
(opt_cdnProxyRegex && opt_cdnProxyRegex.test(event.origin))) &&
(!event.source || event.source == target) &&
message.app === APP &&
message.name === CHANNEL_OPEN_MSG
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ampproject/viewer-messaging",
"version": "1.1.1",
"version": "1.1.2",
"description": "Messaging between an AMP Doc and a Viewer",
"author": "The AMP HTML Authors",
"license": "Apache-2.0",
Expand Down
5 changes: 4 additions & 1 deletion src/amp-story-player/amp-story-player-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {getMode} from '../../src/mode';
import {parseJson} from '../json';
import {resetStyles, setStyle, setStyles} from '../style';
import {toArray} from '../types';
import {urls} from '../config';

/** @enum {string} */
const LoadStateClass = {
Expand Down Expand Up @@ -619,7 +620,9 @@ export class AmpStoryPlayer {
return Messaging.waitForHandshakeFromDocument(
this.win_,
iframeEl.contentWindow,
this.getEncodedLocation_(url).origin
this.getEncodedLocation_(url).origin,
/*opt_token*/ null,
urls.cdnProxyRegex
);
});
}
Expand Down

0 comments on commit 5cbe2a0

Please sign in to comment.