1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // A guest that logs console messages.
6 // Note that the embedder has to initiate a postMessage first so that
7 // the guest has a reference to the embedder's window.
8 window.console.log('Guest script loading.');
10 // The window reference of the embedder to send post message reply.
11 var embedderWindowChannel = null;
13 var notifyEmbedder = function(msg_array) {
14 embedderWindowChannel.postMessage(JSON.stringify(msg_array), '*');
17 var onPostMessageReceived = function(e) {
18 embedderWindowChannel = e.source;
19 var data = JSON.parse(e.data);
20 if (data[0] == 'create-channel') {
21 window.console.log('aaa. guest: create-channel');
22 notifyEmbedder(['channel-created']);
27 // These logs trigger event listeners in the embedder.
30 window.console.log('log-one'); // LevelLog = 0.
33 window.console.warn('log-two'); // LevelWarning = 1.
36 window.console.error('log-three'); // LevelDebug = 2.
39 window.console.debug('log-four'); // LevelError = -1.
42 throw Error('log-five');
47 window.addEventListener('message', onPostMessageReceived, false);