OLD | NEW |
---|---|
(Empty) | |
1 <!doctype html> | |
2 <!-- | |
3 * Copyright 2013 The Chromium Authors. All rights reserved. Use of this | |
4 * source code is governed by a BSD-style license that can be found in the | |
5 * LICENSE file. | |
6 --> | |
7 <html> | |
8 <head> | |
9 <script type="text/javascript"> | |
10 // A guest that stores and deletes cookies. | |
11 // Note that the embedder has to initiate a postMessage first so that | |
12 // the guest has a reference to the embedder's window. | |
13 | |
14 // The window reference of the embedder to send post message reply. | |
15 var embedderWindowChannel = null; | |
16 | |
17 // A value that uniquely identifies the guest sending the messages to the | |
18 // embedder. | |
19 var channelId = 0; | |
20 var notifyEmbedder = function(msg_array) { | |
lazyboy
2017/02/15 21:45:26
msgArray
lfg
2017/02/15 22:44:00
Done.
| |
21 var msg = msg_array.concat([channelId]); | |
22 embedderWindowChannel.postMessage(JSON.stringify(msg), '*'); | |
23 }; | |
24 | |
25 var SPLIT_RE_ = /\s*;\s*/; | |
26 var setCookie = function(name, value) { // Just a random future time. | |
27 var futureDate = new Date((+new Date) + 10000 * 1000); | |
28 document.cookie = | |
29 name + '=' + value + ';expires=' + futureDate.toUTCString(); | |
30 }; | |
31 var setSessionCookie = function (name, value) { // Session cookie. | |
32 document.cookie = name + '=' + value; | |
33 }; | |
34 var getCookie = function (name) { | |
35 var nameEq = name + '='; | |
36 var parts = (document.cookie || '').split(SPLIT_RE_); | |
37 for (var i = 0; i < parts.length; ++i) { | |
38 var part = parts[i]; | |
39 if (part.lastIndexOf(nameEq, 0) == 0) { | |
lazyboy
2017/02/15 21:45:26
part.startsWith(nameEq) will also work.
lfg
2017/02/15 22:44:00
Done.
| |
40 return part.substr(nameEq.length); | |
41 } | |
42 if (part == name) { | |
43 return ''; | |
44 } | |
45 } | |
46 return undefined; | |
47 }; | |
48 | |
49 var addCookies = function() { | |
50 window.console.log('setCookie: foo = fooValue'); | |
51 setCookie("foo", "fooValue"); | |
lazyboy
2017/02/15 21:45:26
nit: here and elsewhere: all strings single quoted
lfg
2017/02/15 22:44:00
Done.
| |
52 window.console.log('setSessionCookie: bar = barValue'); | |
53 setSessionCookie("bar", "barValue"); | |
54 notifyEmbedder(['step2.cookies-added']); | |
55 }; | |
56 | |
57 var onPostMessageReceived = function(e) { | |
58 embedderWindowChannel = e.source; | |
59 var data = JSON.parse(e.data); | |
60 if (data[0] == 'create-channel') { | |
61 window.console.log('guest: create-channel'); | |
62 channelId = data[1]; | |
63 notifyEmbedder(['channel-created']); | |
64 return; | |
65 } | |
66 | |
67 window.console.log('guest.onPostMessageReceived: ' + data[0]); | |
68 // Tests. | |
69 // These logs trigger event listeners in the embedder. | |
70 switch (data[0]) { | |
71 case 'step1.add-cookies': | |
72 window.console.log('guest.' + data[0]); | |
73 addCookies(); | |
74 break; | |
75 case 'step3.get-cookies': | |
76 window.console.log('guest.' + data[0]); | |
77 var retValues = ['step4.got-cookies']; | |
78 var cookieValues = []; | |
79 for (var i = 1; i < data.length; ++i) { | |
80 cookieValues.push(getCookie(data[i])); | |
81 } | |
82 retValues.push(cookieValues); | |
83 notifyEmbedder(retValues); | |
84 break; | |
85 default: | |
86 break; | |
87 } | |
88 }; | |
89 window.addEventListener('message', onPostMessageReceived, false); | |
90 </script> | |
91 </head> | |
92 <body> | |
93 <div>Guest that stores and retrieves certain cookies.</div> | |
94 </body> | |
95 </html> | |
OLD | NEW |