├── .gitignore
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── helper-server
├── README.md
├── app.yaml
└── server.go
├── step3-manifest
├── background.jpeg
├── clear.html
├── construction.gif
├── crypto.js
├── dragon.html
├── dragon.js
├── dragon.png
├── faq.html
├── icon-192.png
├── index.html
├── logo.png
├── manifest.json
├── site.js
└── styles.css
├── step4-sw
├── background.jpeg
├── clear.html
├── construction.gif
├── crypto.js
├── dragon.html
├── dragon.js
├── dragon.png
├── faq.html
├── icon-192.png
├── index.html
├── logo.png
├── manifest.json
├── site.js
├── styles.css
└── sw.js
├── step5-push
├── background.jpeg
├── clear.html
├── construction.gif
├── crypto.js
├── dragon.html
├── dragon.js
├── dragon.png
├── faq.html
├── icon-192.png
├── index.html
├── logo.png
├── manifest.json
├── site.js
├── styles.css
└── sw.js
├── step6-cache
├── background.jpeg
├── clear.html
├── construction.gif
├── crypto.js
├── dragon.html
├── dragon.js
├── dragon.png
├── faq.html
├── icon-192.png
├── index.html
├── logo.png
├── manifest.json
├── site.js
├── styles.css
└── sw.js
├── step7-imperative
├── background.jpeg
├── clear.html
├── construction.gif
├── crypto.js
├── dragon.html
├── dragon.js
├── dragon.png
├── faq.html
├── icon-192.png
├── index.html
├── logo.png
├── manifest.json
├── site.js
├── styles.css
└── sw.js
└── work
├── background.jpeg
├── clear.html
├── construction.gif
├── crypto.js
├── dragon.html
├── dragon.js
├── dragon.png
├── faq.html
├── icon-192.png
├── index.html
├── logo.png
├── site.js
└── styles.css
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to become a contributor and submit your own code
2 |
3 | ## Contributor License Agreements
4 |
5 | We'd love to accept your patches! Before we can take them, we
6 | have to jump a couple of legal hurdles.
7 |
8 | ### Before you contribute
9 | Before we can use your code, you must sign the
10 | [Google Individual Contributor License Agreement](https://cla.developers.google.com/about/google-individual)
11 | (CLA), which you can do online. The CLA is necessary mainly because you own the
12 | copyright to your changes, even after your contribution becomes part of our
13 | codebase, so we need your permission to use and distribute your code. We also
14 | need to be sure of various other things—for instance that you'll tell us if you
15 | know that your code infringes on other people's patents. You don't have to sign
16 | the CLA until after you've submitted your code for review and a member has
17 | approved it, but you must do it before we can put your code into our codebase.
18 | Before you start working on a larger contribution, you should get in touch with
19 | us first through the issue tracker with your idea so that we can help out and
20 | possibly guide you. Coordinating up front makes it much easier to avoid
21 | frustration later on.
22 |
23 | ### Code reviews
24 | All submissions, including submissions by project members, require review. We
25 | use Github pull requests for this purpose.
26 |
27 | ### The small print
28 | Contributions made by corporations are covered by a different agreement than
29 | the one above, the
30 | [Software Grant and Corporate Contributor License Agreement](https://cla.developers.google.com/about/google-corporate).
31 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Migrate to Progressive Web Apps Codelab
2 |
3 | This folder contains the source code for a Google I/O 2016 codelab.
4 |
5 | You can find the [codelab on Google Developers](https://codelabs.developers.google.com/codelabs/migrate-to-progressive-web-apps/)!
6 |
7 | ### License
8 |
9 | ```
10 | Copyright 2016 Google, Inc.
11 |
12 | Licensed to the Apache Software Foundation (ASF) under one or more contributor
13 | license agreements. See the NOTICE file distributed with this work for
14 | additional information regarding copyright ownership. The ASF licenses this
15 | file to you under the Apache License, Version 2.0 (the "License"); you may not
16 | use this file except in compliance with the License. You may obtain a copy of
17 | the License at
18 |
19 | http://www.apache.org/licenses/LICENSE-2.0
20 |
21 | Unless required by applicable law or agreed to in writing, software
22 | distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23 | WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
24 | License for the specific language governing permissions and limitations under
25 | the License.
26 | ```
27 |
--------------------------------------------------------------------------------
/helper-server/README.md:
--------------------------------------------------------------------------------
1 | This is the helper server deployed to `dragon-server.appspot.com`. It's not part of the codelab.
2 |
--------------------------------------------------------------------------------
/helper-server/app.yaml:
--------------------------------------------------------------------------------
1 | application: dragon-server
2 | version: main
3 | runtime: go
4 | api_version: go1
5 |
6 | handlers:
7 | - url: /
8 | script: _go_app
9 |
--------------------------------------------------------------------------------
/helper-server/server.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "encoding/json"
5 | "net/http"
6 | )
7 |
8 | func init() {
9 | http.HandleFunc("/", httpDragonServer)
10 | }
11 |
12 | type Action struct {
13 | Name string
14 | ID string
15 | }
16 |
17 | type State struct {
18 | Gold int
19 | Size int
20 | Other map[string]interface{}
21 | Actions []Action
22 | }
23 |
24 | func httpDragonServer(w http.ResponseWriter, r *http.Request) {
25 | // TODO: load profile
26 |
27 | s := State{
28 | Gold: 100,
29 | Size: 50,
30 | Other: make(map[string]interface{}),
31 | Actions: []Action{
32 | {Name: "Feed dragon", ID: "feed"},
33 | {Name: "Pillage villages", ID: "pillage"},
34 | {Name: "Sleep", ID: "sleep"},
35 | },
36 | }
37 |
38 | switch r.Method {
39 | case "GET":
40 | // do nothing
41 | case "POST":
42 | action := r.FormValue("action")
43 |
44 | switch action {
45 | case "feed":
46 | case "pillage":
47 | case "sleep":
48 | default:
49 | http.Error(w, "", http.StatusBadRequest)
50 | return
51 | }
52 | default:
53 | http.Error(w, "", http.StatusMethodNotAllowed)
54 | return
55 | }
56 |
57 | w.Header().Set("Content-Type", "application/json")
58 | w.Header().Set("Access-Control-Allow-Origin", "*")
59 | json.NewEncoder(w).Encode(&s)
60 | }
61 |
--------------------------------------------------------------------------------
/step3-manifest/background.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step3-manifest/background.jpeg
--------------------------------------------------------------------------------
/step3-manifest/clear.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Clear Cache
21 |
22 |
23 |
24 | Clear
25 |
26 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/step3-manifest/construction.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step3-manifest/construction.gif
--------------------------------------------------------------------------------
/step3-manifest/crypto.js:
--------------------------------------------------------------------------------
1 | // Copyright 2017 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function urlB64ToUint8Array(base64String) {
16 | const suffixLength = 4 - base64String.length % 4;
17 | const base64 = (base64String + '='.repeat(suffixLength))
18 | .replace(/\-/g, '+')
19 | .replace(/_/g, '/');
20 |
21 | const raw = window.atob(base64);
22 |
23 | const output = new Uint8Array(raw.length);
24 | Array.from(raw).forEach((c, i) => output[i] = c.charCodeAt(0));
25 | return output;
26 | }
27 |
28 | function stringToBase64Url(s) {
29 | const base64 = window.btoa(s);
30 | return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
31 | }
32 |
33 | function JSONToBase64Url(data) {
34 | const s = JSON.stringify(data);
35 | return stringToBase64Url(s);
36 | }
37 |
38 | function uint8ArrayToBase64Url(array) {
39 | const s = String.fromCodePoint(...array);
40 | return stringToBase64Url(s);
41 | }
42 |
43 | /**
44 | * @param {string} publicKey public key in URL-safe base64
45 | * @param {string} privateKey private key in URL-safe base64
46 | * @param {string} endpoint from PushSubscription
47 | * @param {string} sender either "mailto:" or a web address
48 | * @return {!Promise} the Authorization header
49 | */
50 | function prepareAuthorization(publicKey, privateKey, endpoint, sender) {
51 | const origin = new URL(endpoint).origin;
52 | const defaultExpiration = Math.floor(Date.now() / 1000) + 43200; // 12 hours in future
53 |
54 | const header = {
55 | typ: 'JWT',
56 | alg: 'ES256'
57 | };
58 | const jwtPayload = {
59 | aud: origin,
60 | exp: defaultExpiration,
61 | sub: sender,
62 | };
63 |
64 | // unsignedToken is the URL-safe base64 encoded JSON header and body joined by a dot.
65 | const unsignedToken = JSONToBase64Url(header) + '.' + JSONToBase64Url(jwtPayload);
66 |
67 | // Sign unsignedToken using ES256 (SHA-256 over ECDSA). This requires the private key.
68 | const publicKeyArray = urlB64ToUint8Array(publicKey);
69 | const key = {
70 | kty: 'EC',
71 | crv: 'P-256',
72 | x: uint8ArrayToBase64Url(publicKeyArray.subarray(1, 33)),
73 | y: uint8ArrayToBase64Url(publicKeyArray.subarray(33, 65)),
74 | d: privateKey,
75 | };
76 |
77 | // Perform the signing. importKey returns a Promise, so wait for it to finish.
78 | const args = {name: 'ECDSA', namedCurve: 'P-256'};
79 | return crypto.subtle.importKey('jwk', key, args, true, ['sign'])
80 | .then(key => {
81 | return crypto.subtle.sign({
82 | name: 'ECDSA',
83 | hash: {
84 | name: 'SHA-256',
85 | },
86 | }, key, (new TextEncoder('utf-8')).encode(unsignedToken));
87 | })
88 | .then(buffer => new Uint8Array(buffer))
89 | .then(signature => {
90 | return 'WebPush ' + unsignedToken + '.' + uint8ArrayToBase64Url(signature);
91 | });
92 | }
--------------------------------------------------------------------------------
/step3-manifest/dragon.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | Dragotchi
34 |
35 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
56 |
58 |
59 |
70 |
71 |
72 |
73 |
74 |
75 |
80 |
81 |
82 |
--------------------------------------------------------------------------------
/step3-manifest/dragon.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function refresh(action, callback) {
16 | var x = new XMLHttpRequest();
17 | x.onload = function() {
18 | var out = x.response;
19 | if (typeof out == 'string') {
20 | out = JSON.parse(out);
21 | }
22 | callback(out);
23 | };
24 | x.onerror = function() {
25 | alert('couldn\'t fetch dragon status');
26 | };
27 |
28 | if (action) {
29 | x.open('POST', 'https://dragon-server.appspot.com/?action=' + action);
30 | } else {
31 | x.open('GET', 'https://dragon-server.appspot.com/');
32 | }
33 |
34 | x.send();
35 | }
36 |
37 | window.addEventListener('load', function() {
38 | function createDataRow(name, value) {
39 | var row = document.createElement('tr');
40 |
41 | var th = document.createElement('th');
42 | th.textContent = name;
43 | row.appendChild(th);
44 |
45 | var td = document.createElement('td');
46 | td.textContent = value;
47 | row.appendChild(td);
48 |
49 | return row;
50 | }
51 |
52 | var timeout;
53 | (function work(action) {
54 | window.clearTimeout(timeout);
55 | timeout = window.setTimeout(work, 60 * 1000); // 60s
56 |
57 | if (action) { // clicked action, clear future actions
58 | actions.textContent = '';
59 | }
60 | refresh(action, function(status) {
61 |
62 | gold.textContent = '';
63 | for (var i = 0; i < status.Gold; ++i) {
64 | var coin = document.createElement('span');
65 | coin.className = 'coin';
66 | coin.style.top = Math.random() * 100 + '%';
67 | coin.style.left = Math.random() * 100 + '%';
68 | gold.appendChild(coin);
69 | }
70 |
71 | data.textContent = '';
72 | data.appendChild(createDataRow('Gold', status.Gold));
73 | data.appendChild(createDataRow('Size', status.Size + 'kg'));
74 |
75 | // TODO: update size
76 |
77 | actions.textContent = '';
78 | status.Actions.forEach(function(action) {
79 | var button = document.createElement('button');
80 | button.addEventListener('click', function(ev) {
81 | ev.preventDefault();
82 | work(action.ID);
83 | });
84 | button.textContent = action.Name;
85 | actions.appendChild(button);
86 | });
87 |
88 | });
89 |
90 | }());
91 | });
--------------------------------------------------------------------------------
/step3-manifest/dragon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step3-manifest/dragon.png
--------------------------------------------------------------------------------
/step3-manifest/faq.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Dragotchi
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | FAQ
47 |
48 |
49 | Are you serious?
50 | Yes.
51 | How cool are dragons?
52 | Well, it's not clear whether they are cold- or warm-blooded.
53 | Why does this exist?
54 | This site has been built as an example for Google Codelabs .
55 | Where are the images from?
56 | The logo is from Google Maps 8-bit , the "Under Construction" gif is generally available online, some assets from openclipart , other assets were built for this site.
57 |
58 |
59 |
60 |
61 |
62 |
63 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/step3-manifest/icon-192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step3-manifest/icon-192.png
--------------------------------------------------------------------------------
/step3-manifest/index.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Dragotchi
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | Home
47 |
48 |
49 | Click and play with your very own Dragon! Feed it gold and watch it grow over time.
50 |
51 |
52 | Soon it will be an excited little dragon eating villagers and burning houses down.
53 |
54 |
55 | To get started, click on the Dragotchi link !
56 |
57 |
58 |
59 |
60 |
61 | Dragotchi is always Under Construction !!!
62 |
63 |
64 |
65 |
66 |
67 |
68 |
73 |
74 |
75 | 0
76 | 0
77 | 0
78 | 2
79 | 0
80 | 1
81 | 6
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/step3-manifest/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step3-manifest/logo.png
--------------------------------------------------------------------------------
/step3-manifest/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "The Most Awesome Dragon Site",
3 | "short_name": "🐉🐉🐉",
4 | "display": "minimal-ui",
5 | "start_url": "/",
6 | "theme_color": "#673ab6",
7 | "background_color": "#111111",
8 | "icons": [
9 | {
10 | "src": "icon-192.png",
11 | "sizes": "192x192",
12 | "type": "image/png"
13 | }
14 | ]
15 | }
--------------------------------------------------------------------------------
/step3-manifest/site.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | /** Intentionally empty */
16 |
--------------------------------------------------------------------------------
/step3-manifest/styles.css:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | body {
19 | background: black url(background.jpeg) repeat-x center top;
20 | background-size: 200px;
21 | color: white;
22 | text-align: center;
23 | }
24 |
25 | a {
26 | color: #ff9249;
27 | }
28 |
29 | /** I'm so sorry. */
30 | blink {
31 | animation: blink 1s step-end infinite;
32 | }
33 | @keyframes blink {
34 | 67% { opacity: 0 }
35 | }
36 |
37 | header .logo {
38 | width: 100px;
39 | }
40 | header ul {
41 | list-style: none;
42 | padding: 0;
43 | color: #555;
44 | }
45 | header li {
46 | display: inline-block;
47 | }
48 | header li::before {
49 | content: " — ";
50 | }
51 | header li:first-child::before {
52 | content: "";
53 | }
54 |
55 | .counter {
56 | display: inline-flex;
57 | margin: 20px;
58 | }
59 |
60 | .counter span {
61 | border: 1px solid #333;
62 | width: 12px;
63 | font-family: Monospace;
64 | background: linear-gradient(to top, red, blue);
65 | }
66 |
67 | dl {
68 | width: 400px;
69 | margin: 0 auto;
70 | text-align: left;
71 | }
72 |
73 | dd {
74 | margin-bottom: 1em;
75 | }
76 |
77 | section table {
78 | margin: 0 auto;
79 | text-align: left;
80 | }
81 | section table td,
82 | section table th {
83 | min-width: 50px;
84 | }
85 |
86 | .dragon {
87 | box-sizing: border-box;
88 | padding: 20px;
89 | width: 280px;
90 | height: 280px;
91 | margin: 12px auto;
92 | border: 2px solid rgba(255, 255, 255, 0.125);
93 | background: #111;
94 | border-radius: 2px;
95 | position: relative;
96 | overflow: hidden;
97 | }
98 | .dragon #guy {
99 | position: absolute;
100 | width: 100px;
101 | margin-left: -50px;
102 | margin-top: -50px;
103 | z-index: 1000;
104 | transition: transform 1s ease-in-out;
105 | }
106 | .dragon img {
107 | width: 100%;
108 | cursor: pointer;
109 | transition: transform 0.25s;
110 | }
111 | .dragon img:hover {
112 | transform: scale(1.45) rotate(10deg);
113 | }
114 |
115 | .coin {
116 | position: absolute;
117 | opacity: 0.5;
118 | width: 10px;
119 | height: 5px;
120 | margin-left: -5px;
121 | margin-top: -2px;
122 | background: yellow;
123 | border-radius: 100%; /** oval */
124 | border-bottom: 1px solid rgba(0, 0, 0, 0.5);
125 | border-top: 1px solid rgba(255, 255, 255, 0.5);
126 | box-shadow: 0 1px 0 rgba(0, 0, 0, 0.75), 0 -1px 0 rgba(0, 0, 0, 0.25);
127 | }
128 |
129 | .actions {
130 | width: 280px;
131 | height: 80px;
132 | box-sizing: border-box;
133 | border: 2px solid rgba(255, 255, 255, 0.125);
134 | background: #211;
135 | border-radius: 2px;
136 | margin: 12px auto;
137 | display: flex;
138 | padding: 2px;
139 | }
140 |
141 | .actions button {
142 | border: 0;
143 | flex-grow: 1;
144 | width: 300%; /** forces equal size */
145 | margin-left: 2px;
146 | background: rgba(255, 255, 255, 0.125);
147 | color: white;
148 | font-size: 16px;
149 | padding: 12px;
150 | opacity: 0.8;
151 | }
152 | .actions button:first-child {
153 | margin-left: 0;
154 | }
155 | .actions button:disabled {
156 | text-decoration: line-through;
157 | }
158 | .actions button:hover:not(:disabled) {
159 | opacity: 1.0;
160 | cursor: pointer;
161 | }
--------------------------------------------------------------------------------
/step4-sw/background.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step4-sw/background.jpeg
--------------------------------------------------------------------------------
/step4-sw/clear.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Clear Cache
21 |
22 |
23 |
24 | Clear
25 |
26 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/step4-sw/construction.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step4-sw/construction.gif
--------------------------------------------------------------------------------
/step4-sw/crypto.js:
--------------------------------------------------------------------------------
1 | // Copyright 2017 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function urlB64ToUint8Array(base64String) {
16 | const suffixLength = 4 - base64String.length % 4;
17 | const base64 = (base64String + '='.repeat(suffixLength))
18 | .replace(/\-/g, '+')
19 | .replace(/_/g, '/');
20 |
21 | const raw = window.atob(base64);
22 |
23 | const output = new Uint8Array(raw.length);
24 | Array.from(raw).forEach((c, i) => output[i] = c.charCodeAt(0));
25 | return output;
26 | }
27 |
28 | function stringToBase64Url(s) {
29 | const base64 = window.btoa(s);
30 | return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
31 | }
32 |
33 | function JSONToBase64Url(data) {
34 | const s = JSON.stringify(data);
35 | return stringToBase64Url(s);
36 | }
37 |
38 | function uint8ArrayToBase64Url(array) {
39 | const s = String.fromCodePoint(...array);
40 | return stringToBase64Url(s);
41 | }
42 |
43 | /**
44 | * @param {string} publicKey public key in URL-safe base64
45 | * @param {string} privateKey private key in URL-safe base64
46 | * @param {string} endpoint from PushSubscription
47 | * @param {string} sender either "mailto:" or a web address
48 | * @return {!Promise} the Authorization header
49 | */
50 | function prepareAuthorization(publicKey, privateKey, endpoint, sender) {
51 | const origin = new URL(endpoint).origin;
52 | const defaultExpiration = Math.floor(Date.now() / 1000) + 43200; // 12 hours in future
53 |
54 | const header = {
55 | typ: 'JWT',
56 | alg: 'ES256'
57 | };
58 | const jwtPayload = {
59 | aud: origin,
60 | exp: defaultExpiration,
61 | sub: sender,
62 | };
63 |
64 | // unsignedToken is the URL-safe base64 encoded JSON header and body joined by a dot.
65 | const unsignedToken = JSONToBase64Url(header) + '.' + JSONToBase64Url(jwtPayload);
66 |
67 | // Sign unsignedToken using ES256 (SHA-256 over ECDSA). This requires the private key.
68 | const publicKeyArray = urlB64ToUint8Array(publicKey);
69 | const key = {
70 | kty: 'EC',
71 | crv: 'P-256',
72 | x: uint8ArrayToBase64Url(publicKeyArray.subarray(1, 33)),
73 | y: uint8ArrayToBase64Url(publicKeyArray.subarray(33, 65)),
74 | d: privateKey,
75 | };
76 |
77 | // Perform the signing. importKey returns a Promise, so wait for it to finish.
78 | const args = {name: 'ECDSA', namedCurve: 'P-256'};
79 | return crypto.subtle.importKey('jwk', key, args, true, ['sign'])
80 | .then(key => {
81 | return crypto.subtle.sign({
82 | name: 'ECDSA',
83 | hash: {
84 | name: 'SHA-256',
85 | },
86 | }, key, (new TextEncoder('utf-8')).encode(unsignedToken));
87 | })
88 | .then(buffer => new Uint8Array(buffer))
89 | .then(signature => {
90 | return 'WebPush ' + unsignedToken + '.' + uint8ArrayToBase64Url(signature);
91 | });
92 | }
--------------------------------------------------------------------------------
/step4-sw/dragon.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | Dragotchi
34 |
35 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
56 |
58 |
59 |
70 |
71 |
72 |
73 |
74 |
75 |
80 |
81 |
82 |
--------------------------------------------------------------------------------
/step4-sw/dragon.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function refresh(action, callback) {
16 | var x = new XMLHttpRequest();
17 | x.onload = function() {
18 | var out = x.response;
19 | if (typeof out == 'string') {
20 | out = JSON.parse(out);
21 | }
22 | callback(out);
23 | };
24 | x.onerror = function() {
25 | alert('couldn\'t fetch dragon status');
26 | };
27 |
28 | if (action) {
29 | x.open('POST', 'https://dragon-server.appspot.com/?action=' + action);
30 | } else {
31 | x.open('GET', 'https://dragon-server.appspot.com/');
32 | }
33 |
34 | x.send();
35 | }
36 |
37 | window.addEventListener('load', function() {
38 | function createDataRow(name, value) {
39 | var row = document.createElement('tr');
40 |
41 | var th = document.createElement('th');
42 | th.textContent = name;
43 | row.appendChild(th);
44 |
45 | var td = document.createElement('td');
46 | td.textContent = value;
47 | row.appendChild(td);
48 |
49 | return row;
50 | }
51 |
52 | var timeout;
53 | (function work(action) {
54 | window.clearTimeout(timeout);
55 | timeout = window.setTimeout(work, 60 * 1000); // 60s
56 |
57 | if (action) { // clicked action, clear future actions
58 | actions.textContent = '';
59 | }
60 | refresh(action, function(status) {
61 |
62 | gold.textContent = '';
63 | for (var i = 0; i < status.Gold; ++i) {
64 | var coin = document.createElement('span');
65 | coin.className = 'coin';
66 | coin.style.top = Math.random() * 100 + '%';
67 | coin.style.left = Math.random() * 100 + '%';
68 | gold.appendChild(coin);
69 | }
70 |
71 | data.textContent = '';
72 | data.appendChild(createDataRow('Gold', status.Gold));
73 | data.appendChild(createDataRow('Size', status.Size + 'kg'));
74 |
75 | // TODO: update size
76 |
77 | actions.textContent = '';
78 | status.Actions.forEach(function(action) {
79 | var button = document.createElement('button');
80 | button.addEventListener('click', function(ev) {
81 | ev.preventDefault();
82 | work(action.ID);
83 | });
84 | button.textContent = action.Name;
85 | actions.appendChild(button);
86 | });
87 |
88 | });
89 |
90 | }());
91 | });
--------------------------------------------------------------------------------
/step4-sw/dragon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step4-sw/dragon.png
--------------------------------------------------------------------------------
/step4-sw/faq.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Dragotchi
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | FAQ
47 |
48 |
49 | Are you serious?
50 | Yes.
51 | How cool are dragons?
52 | Well, it's not clear whether they are cold- or warm-blooded.
53 | Why does this exist?
54 | This site has been built as an example for Google Codelabs .
55 | Where are the images from?
56 | The logo is from Google Maps 8-bit , the "Under Construction" gif is generally available online, some assets from openclipart , other assets were built for this site.
57 |
58 |
59 |
60 |
61 |
62 |
63 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/step4-sw/icon-192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step4-sw/icon-192.png
--------------------------------------------------------------------------------
/step4-sw/index.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Dragotchi
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | Home
47 |
48 |
49 | Click and play with your very own Dragon! Feed it gold and watch it grow over time.
50 |
51 |
52 | Soon it will be an excited little dragon eating villagers and burning houses down.
53 |
54 |
55 | To get started, click on the Dragotchi link !
56 |
57 |
58 |
59 |
60 |
61 | Dragotchi is always Under Construction !!!
62 |
63 |
64 |
65 |
66 |
67 |
68 |
73 |
74 |
75 | 0
76 | 0
77 | 0
78 | 2
79 | 0
80 | 1
81 | 6
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/step4-sw/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step4-sw/logo.png
--------------------------------------------------------------------------------
/step4-sw/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "The Most Awesome Dragon Site",
3 | "short_name": "🐉🐉🐉",
4 | "display": "minimal-ui",
5 | "start_url": "/",
6 | "theme_color": "#673ab6",
7 | "background_color": "#111111",
8 | "icons": [
9 | {
10 | "src": "icon-192.png",
11 | "sizes": "192x192",
12 | "type": "image/png"
13 | }
14 | ]
15 | }
--------------------------------------------------------------------------------
/step4-sw/site.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | navigator.serviceWorker && navigator.serviceWorker.register('./sw.js').then(function(registration) {
16 | console.log('Excellent, registered with scope: ', registration.scope);
17 | });
--------------------------------------------------------------------------------
/step4-sw/styles.css:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | body {
19 | background: black url(background.jpeg) repeat-x center top;
20 | background-size: 200px;
21 | color: white;
22 | text-align: center;
23 | }
24 |
25 | a {
26 | color: #ff9249;
27 | }
28 |
29 | /** I'm so sorry. */
30 | blink {
31 | animation: blink 1s step-end infinite;
32 | }
33 | @keyframes blink {
34 | 67% { opacity: 0 }
35 | }
36 |
37 | header .logo {
38 | width: 100px;
39 | }
40 | header ul {
41 | list-style: none;
42 | padding: 0;
43 | color: #555;
44 | }
45 | header li {
46 | display: inline-block;
47 | }
48 | header li::before {
49 | content: " — ";
50 | }
51 | header li:first-child::before {
52 | content: "";
53 | }
54 |
55 | .counter {
56 | display: inline-flex;
57 | margin: 20px;
58 | }
59 |
60 | .counter span {
61 | border: 1px solid #333;
62 | width: 12px;
63 | font-family: Monospace;
64 | background: linear-gradient(to top, red, blue);
65 | }
66 |
67 | dl {
68 | width: 400px;
69 | margin: 0 auto;
70 | text-align: left;
71 | }
72 |
73 | dd {
74 | margin-bottom: 1em;
75 | }
76 |
77 | section table {
78 | margin: 0 auto;
79 | text-align: left;
80 | }
81 | section table td,
82 | section table th {
83 | min-width: 50px;
84 | }
85 |
86 | .dragon {
87 | box-sizing: border-box;
88 | padding: 20px;
89 | width: 280px;
90 | height: 280px;
91 | margin: 12px auto;
92 | border: 2px solid rgba(255, 255, 255, 0.125);
93 | background: #111;
94 | border-radius: 2px;
95 | position: relative;
96 | overflow: hidden;
97 | }
98 | .dragon #guy {
99 | position: absolute;
100 | width: 100px;
101 | margin-left: -50px;
102 | margin-top: -50px;
103 | z-index: 1000;
104 | transition: transform 1s ease-in-out;
105 | }
106 | .dragon img {
107 | width: 100%;
108 | cursor: pointer;
109 | transition: transform 0.25s;
110 | }
111 | .dragon img:hover {
112 | transform: scale(1.45) rotate(10deg);
113 | }
114 |
115 | .coin {
116 | position: absolute;
117 | opacity: 0.5;
118 | width: 10px;
119 | height: 5px;
120 | margin-left: -5px;
121 | margin-top: -2px;
122 | background: yellow;
123 | border-radius: 100%; /** oval */
124 | border-bottom: 1px solid rgba(0, 0, 0, 0.5);
125 | border-top: 1px solid rgba(255, 255, 255, 0.5);
126 | box-shadow: 0 1px 0 rgba(0, 0, 0, 0.75), 0 -1px 0 rgba(0, 0, 0, 0.25);
127 | }
128 |
129 | .actions {
130 | width: 280px;
131 | height: 80px;
132 | box-sizing: border-box;
133 | border: 2px solid rgba(255, 255, 255, 0.125);
134 | background: #211;
135 | border-radius: 2px;
136 | margin: 12px auto;
137 | display: flex;
138 | padding: 2px;
139 | }
140 |
141 | .actions button {
142 | border: 0;
143 | flex-grow: 1;
144 | width: 300%; /** forces equal size */
145 | margin-left: 2px;
146 | background: rgba(255, 255, 255, 0.125);
147 | color: white;
148 | font-size: 16px;
149 | padding: 12px;
150 | opacity: 0.8;
151 | }
152 | .actions button:first-child {
153 | margin-left: 0;
154 | }
155 | .actions button:disabled {
156 | text-decoration: line-through;
157 | }
158 | .actions button:hover:not(:disabled) {
159 | opacity: 1.0;
160 | cursor: pointer;
161 | }
--------------------------------------------------------------------------------
/step4-sw/sw.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | /** An empty service worker! */
16 |
17 | self.addEventListener('fetch', function(event) {
18 | /** An empty fetch handler! */
19 | });
20 |
--------------------------------------------------------------------------------
/step5-push/background.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step5-push/background.jpeg
--------------------------------------------------------------------------------
/step5-push/clear.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Clear Cache
21 |
22 |
23 |
24 | Clear
25 |
26 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/step5-push/construction.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step5-push/construction.gif
--------------------------------------------------------------------------------
/step5-push/crypto.js:
--------------------------------------------------------------------------------
1 | // Copyright 2017 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function urlB64ToUint8Array(base64String) {
16 | const suffixLength = 4 - base64String.length % 4;
17 | const base64 = (base64String + '='.repeat(suffixLength))
18 | .replace(/\-/g, '+')
19 | .replace(/_/g, '/');
20 |
21 | const raw = window.atob(base64);
22 |
23 | const output = new Uint8Array(raw.length);
24 | Array.from(raw).forEach((c, i) => output[i] = c.charCodeAt(0));
25 | return output;
26 | }
27 |
28 | function stringToBase64Url(s) {
29 | const base64 = window.btoa(s);
30 | return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
31 | }
32 |
33 | function JSONToBase64Url(data) {
34 | const s = JSON.stringify(data);
35 | return stringToBase64Url(s);
36 | }
37 |
38 | function uint8ArrayToBase64Url(array) {
39 | const s = String.fromCodePoint(...array);
40 | return stringToBase64Url(s);
41 | }
42 |
43 | /**
44 | * @param {string} publicKey public key in URL-safe base64
45 | * @param {string} privateKey private key in URL-safe base64
46 | * @param {string} endpoint from PushSubscription
47 | * @param {string} sender either "mailto:" or a web address
48 | * @return {!Promise} the Authorization header
49 | */
50 | function prepareAuthorization(publicKey, privateKey, endpoint, sender) {
51 | const origin = new URL(endpoint).origin;
52 | const defaultExpiration = Math.floor(Date.now() / 1000) + 43200; // 12 hours in future
53 |
54 | const header = {
55 | typ: 'JWT',
56 | alg: 'ES256'
57 | };
58 | const jwtPayload = {
59 | aud: origin,
60 | exp: defaultExpiration,
61 | sub: sender,
62 | };
63 |
64 | // unsignedToken is the URL-safe base64 encoded JSON header and body joined by a dot.
65 | const unsignedToken = JSONToBase64Url(header) + '.' + JSONToBase64Url(jwtPayload);
66 |
67 | // Sign unsignedToken using ES256 (SHA-256 over ECDSA). This requires the private key.
68 | const publicKeyArray = urlB64ToUint8Array(publicKey);
69 | const key = {
70 | kty: 'EC',
71 | crv: 'P-256',
72 | x: uint8ArrayToBase64Url(publicKeyArray.subarray(1, 33)),
73 | y: uint8ArrayToBase64Url(publicKeyArray.subarray(33, 65)),
74 | d: privateKey,
75 | };
76 |
77 | // Perform the signing. importKey returns a Promise, so wait for it to finish.
78 | const args = {name: 'ECDSA', namedCurve: 'P-256'};
79 | return crypto.subtle.importKey('jwk', key, args, true, ['sign'])
80 | .then(key => {
81 | return crypto.subtle.sign({
82 | name: 'ECDSA',
83 | hash: {
84 | name: 'SHA-256',
85 | },
86 | }, key, (new TextEncoder('utf-8')).encode(unsignedToken));
87 | })
88 | .then(buffer => new Uint8Array(buffer))
89 | .then(signature => {
90 | return 'WebPush ' + unsignedToken + '.' + uint8ArrayToBase64Url(signature);
91 | });
92 | }
--------------------------------------------------------------------------------
/step5-push/dragon.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | Dragotchi
34 |
35 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
56 |
58 |
59 |
70 |
71 |
72 |
73 |
74 |
75 |
80 |
81 |
82 |
--------------------------------------------------------------------------------
/step5-push/dragon.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function refresh(action, callback) {
16 | var x = new XMLHttpRequest();
17 | x.onload = function() {
18 | var out = x.response;
19 | if (typeof out == 'string') {
20 | out = JSON.parse(out);
21 | }
22 | callback(out);
23 | };
24 | x.onerror = function() {
25 | alert('couldn\'t fetch dragon status');
26 | };
27 |
28 | if (action) {
29 | x.open('POST', 'https://dragon-server.appspot.com/?action=' + action);
30 | } else {
31 | x.open('GET', 'https://dragon-server.appspot.com/');
32 | }
33 |
34 | x.send();
35 | }
36 |
37 | window.addEventListener('load', function() {
38 | function createDataRow(name, value) {
39 | var row = document.createElement('tr');
40 |
41 | var th = document.createElement('th');
42 | th.textContent = name;
43 | row.appendChild(th);
44 |
45 | var td = document.createElement('td');
46 | td.textContent = value;
47 | row.appendChild(td);
48 |
49 | return row;
50 | }
51 |
52 | var timeout;
53 | (function work(action) {
54 | window.clearTimeout(timeout);
55 | timeout = window.setTimeout(work, 60 * 1000); // 60s
56 |
57 | if (action) { // clicked action, clear future actions
58 | actions.textContent = '';
59 | }
60 | refresh(action, function(status) {
61 |
62 | gold.textContent = '';
63 | for (var i = 0; i < status.Gold; ++i) {
64 | var coin = document.createElement('span');
65 | coin.className = 'coin';
66 | coin.style.top = Math.random() * 100 + '%';
67 | coin.style.left = Math.random() * 100 + '%';
68 | gold.appendChild(coin);
69 | }
70 |
71 | data.textContent = '';
72 | data.appendChild(createDataRow('Gold', status.Gold));
73 | data.appendChild(createDataRow('Size', status.Size + 'kg'));
74 |
75 | // TODO: update size
76 |
77 | actions.textContent = '';
78 | status.Actions.forEach(function(action) {
79 | var button = document.createElement('button');
80 | button.addEventListener('click', function(ev) {
81 | ev.preventDefault();
82 | work(action.ID);
83 | });
84 | button.textContent = action.Name;
85 | actions.appendChild(button);
86 | });
87 |
88 | });
89 |
90 | }());
91 | });
--------------------------------------------------------------------------------
/step5-push/dragon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step5-push/dragon.png
--------------------------------------------------------------------------------
/step5-push/faq.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Dragotchi
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | FAQ
47 |
48 |
49 | Are you serious?
50 | Yes.
51 | How cool are dragons?
52 | Well, it's not clear whether they are cold- or warm-blooded.
53 | Why does this exist?
54 | This site has been built as an example for Google Codelabs .
55 | Where are the images from?
56 | The logo is from Google Maps 8-bit , the "Under Construction" gif is generally available online, some assets from openclipart , other assets were built for this site.
57 |
58 |
59 |
60 |
61 |
62 |
63 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/step5-push/icon-192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step5-push/icon-192.png
--------------------------------------------------------------------------------
/step5-push/index.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Dragotchi
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | Home
47 |
48 |
49 | Click and play with your very own Dragon! Feed it gold and watch it grow over time.
50 |
51 |
52 | Soon it will be an excited little dragon eating villagers and burning houses down.
53 |
54 |
55 | To get started, click on the Dragotchi link !
56 |
57 |
58 |
59 |
60 |
61 | Dragotchi is always Under Construction !!!
62 |
63 |
64 |
65 |
66 |
67 |
68 |
73 |
74 |
75 | 0
76 | 0
77 | 0
78 | 2
79 | 0
80 | 1
81 | 6
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/step5-push/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step5-push/logo.png
--------------------------------------------------------------------------------
/step5-push/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "The Most Awesome Dragon Site",
3 | "short_name": "🐉🐉🐉",
4 | "display": "minimal-ui",
5 | "start_url": "/",
6 | "theme_color": "#673ab6",
7 | "background_color": "#111111",
8 | "icons": [
9 | {
10 | "src": "icon-192.png",
11 | "sizes": "192x192",
12 | "type": "image/png"
13 | }
14 | ]
15 | }
--------------------------------------------------------------------------------
/step5-push/site.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | const publicKey = '';
16 |
17 | navigator.serviceWorker && navigator.serviceWorker.register('./sw.js').then(function(registration) {
18 | console.log('Excellent, registered with scope: ', registration.scope);
19 | });
20 |
21 | navigator.serviceWorker && navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
22 | serviceWorkerRegistration.pushManager.getSubscription()
23 | .then(function(subscription) {
24 | // subscription will be null or a PushSubscription
25 | if (subscription) {
26 | console.info('Got existing', subscription);
27 | window.subscription = subscription;
28 | return; // got one, yay
29 | }
30 |
31 | const applicationServerKey = urlB64ToUint8Array(publicKey);
32 | serviceWorkerRegistration.pushManager.subscribe({
33 | userVisibleOnly: true,
34 | applicationServerKey,
35 | })
36 | .then(function(subscription) {
37 | console.info('Newly subscribed to push!', subscription);
38 | window.subscription = subscription;
39 | });
40 | });
41 | });
42 |
--------------------------------------------------------------------------------
/step5-push/styles.css:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | body {
19 | background: black url(background.jpeg) repeat-x center top;
20 | background-size: 200px;
21 | color: white;
22 | text-align: center;
23 | }
24 |
25 | a {
26 | color: #ff9249;
27 | }
28 |
29 | /** I'm so sorry. */
30 | blink {
31 | animation: blink 1s step-end infinite;
32 | }
33 | @keyframes blink {
34 | 67% { opacity: 0 }
35 | }
36 |
37 | header .logo {
38 | width: 100px;
39 | }
40 | header ul {
41 | list-style: none;
42 | padding: 0;
43 | color: #555;
44 | }
45 | header li {
46 | display: inline-block;
47 | }
48 | header li::before {
49 | content: " — ";
50 | }
51 | header li:first-child::before {
52 | content: "";
53 | }
54 |
55 | .counter {
56 | display: inline-flex;
57 | margin: 20px;
58 | }
59 |
60 | .counter span {
61 | border: 1px solid #333;
62 | width: 12px;
63 | font-family: Monospace;
64 | background: linear-gradient(to top, red, blue);
65 | }
66 |
67 | dl {
68 | width: 400px;
69 | margin: 0 auto;
70 | text-align: left;
71 | }
72 |
73 | dd {
74 | margin-bottom: 1em;
75 | }
76 |
77 | section table {
78 | margin: 0 auto;
79 | text-align: left;
80 | }
81 | section table td,
82 | section table th {
83 | min-width: 50px;
84 | }
85 |
86 | .dragon {
87 | box-sizing: border-box;
88 | padding: 20px;
89 | width: 280px;
90 | height: 280px;
91 | margin: 12px auto;
92 | border: 2px solid rgba(255, 255, 255, 0.125);
93 | background: #111;
94 | border-radius: 2px;
95 | position: relative;
96 | overflow: hidden;
97 | }
98 | .dragon #guy {
99 | position: absolute;
100 | width: 100px;
101 | margin-left: -50px;
102 | margin-top: -50px;
103 | z-index: 1000;
104 | transition: transform 1s ease-in-out;
105 | }
106 | .dragon img {
107 | width: 100%;
108 | cursor: pointer;
109 | transition: transform 0.25s;
110 | }
111 | .dragon img:hover {
112 | transform: scale(1.45) rotate(10deg);
113 | }
114 |
115 | .coin {
116 | position: absolute;
117 | opacity: 0.5;
118 | width: 10px;
119 | height: 5px;
120 | margin-left: -5px;
121 | margin-top: -2px;
122 | background: yellow;
123 | border-radius: 100%; /** oval */
124 | border-bottom: 1px solid rgba(0, 0, 0, 0.5);
125 | border-top: 1px solid rgba(255, 255, 255, 0.5);
126 | box-shadow: 0 1px 0 rgba(0, 0, 0, 0.75), 0 -1px 0 rgba(0, 0, 0, 0.25);
127 | }
128 |
129 | .actions {
130 | width: 280px;
131 | height: 80px;
132 | box-sizing: border-box;
133 | border: 2px solid rgba(255, 255, 255, 0.125);
134 | background: #211;
135 | border-radius: 2px;
136 | margin: 12px auto;
137 | display: flex;
138 | padding: 2px;
139 | }
140 |
141 | .actions button {
142 | border: 0;
143 | flex-grow: 1;
144 | width: 300%; /** forces equal size */
145 | margin-left: 2px;
146 | background: rgba(255, 255, 255, 0.125);
147 | color: white;
148 | font-size: 16px;
149 | padding: 12px;
150 | opacity: 0.8;
151 | }
152 | .actions button:first-child {
153 | margin-left: 0;
154 | }
155 | .actions button:disabled {
156 | text-decoration: line-through;
157 | }
158 | .actions button:hover:not(:disabled) {
159 | opacity: 1.0;
160 | cursor: pointer;
161 | }
--------------------------------------------------------------------------------
/step5-push/sw.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | /** An empty service worker! */
16 |
17 | self.addEventListener('fetch', function(event) {
18 | /** An empty fetch handler! */
19 | });
20 |
21 | self.addEventListener('push', function(event) {
22 | event.waitUntil(
23 | self.registration.showNotification('Got Push?', {
24 | body: 'Push Message received'
25 | }));
26 | });
27 |
--------------------------------------------------------------------------------
/step6-cache/background.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step6-cache/background.jpeg
--------------------------------------------------------------------------------
/step6-cache/clear.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Clear Cache
21 |
22 |
23 |
24 | Clear
25 |
26 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/step6-cache/construction.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step6-cache/construction.gif
--------------------------------------------------------------------------------
/step6-cache/crypto.js:
--------------------------------------------------------------------------------
1 | // Copyright 2017 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function urlB64ToUint8Array(base64String) {
16 | const suffixLength = 4 - base64String.length % 4;
17 | const base64 = (base64String + '='.repeat(suffixLength))
18 | .replace(/\-/g, '+')
19 | .replace(/_/g, '/');
20 |
21 | const raw = window.atob(base64);
22 |
23 | const output = new Uint8Array(raw.length);
24 | Array.from(raw).forEach((c, i) => output[i] = c.charCodeAt(0));
25 | return output;
26 | }
27 |
28 | function stringToBase64Url(s) {
29 | const base64 = window.btoa(s);
30 | return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
31 | }
32 |
33 | function JSONToBase64Url(data) {
34 | const s = JSON.stringify(data);
35 | return stringToBase64Url(s);
36 | }
37 |
38 | function uint8ArrayToBase64Url(array) {
39 | const s = String.fromCodePoint(...array);
40 | return stringToBase64Url(s);
41 | }
42 |
43 | /**
44 | * @param {string} publicKey public key in URL-safe base64
45 | * @param {string} privateKey private key in URL-safe base64
46 | * @param {string} endpoint from PushSubscription
47 | * @param {string} sender either "mailto:" or a web address
48 | * @return {!Promise} the Authorization header
49 | */
50 | function prepareAuthorization(publicKey, privateKey, endpoint, sender) {
51 | const origin = new URL(endpoint).origin;
52 | const defaultExpiration = Math.floor(Date.now() / 1000) + 43200; // 12 hours in future
53 |
54 | const header = {
55 | typ: 'JWT',
56 | alg: 'ES256'
57 | };
58 | const jwtPayload = {
59 | aud: origin,
60 | exp: defaultExpiration,
61 | sub: sender,
62 | };
63 |
64 | // unsignedToken is the URL-safe base64 encoded JSON header and body joined by a dot.
65 | const unsignedToken = JSONToBase64Url(header) + '.' + JSONToBase64Url(jwtPayload);
66 |
67 | // Sign unsignedToken using ES256 (SHA-256 over ECDSA). This requires the private key.
68 | const publicKeyArray = urlB64ToUint8Array(publicKey);
69 | const key = {
70 | kty: 'EC',
71 | crv: 'P-256',
72 | x: uint8ArrayToBase64Url(publicKeyArray.subarray(1, 33)),
73 | y: uint8ArrayToBase64Url(publicKeyArray.subarray(33, 65)),
74 | d: privateKey,
75 | };
76 |
77 | // Perform the signing. importKey returns a Promise, so wait for it to finish.
78 | const args = {name: 'ECDSA', namedCurve: 'P-256'};
79 | return crypto.subtle.importKey('jwk', key, args, true, ['sign'])
80 | .then(key => {
81 | return crypto.subtle.sign({
82 | name: 'ECDSA',
83 | hash: {
84 | name: 'SHA-256',
85 | },
86 | }, key, (new TextEncoder('utf-8')).encode(unsignedToken));
87 | })
88 | .then(buffer => new Uint8Array(buffer))
89 | .then(signature => {
90 | return 'WebPush ' + unsignedToken + '.' + uint8ArrayToBase64Url(signature);
91 | });
92 | }
--------------------------------------------------------------------------------
/step6-cache/dragon.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | Dragotchi
34 |
35 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
56 |
58 |
59 |
70 |
71 |
72 |
73 |
74 |
75 |
80 |
81 |
82 |
--------------------------------------------------------------------------------
/step6-cache/dragon.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function refresh(action, callback) {
16 | var x = new XMLHttpRequest();
17 | x.onload = function() {
18 | var out = x.response;
19 | if (typeof out == 'string') {
20 | out = JSON.parse(out);
21 | }
22 | callback(out);
23 | };
24 | x.onerror = function() {
25 | alert('couldn\'t fetch dragon status');
26 | };
27 |
28 | if (action) {
29 | x.open('POST', 'https://dragon-server.appspot.com/?action=' + action);
30 | } else {
31 | x.open('GET', 'https://dragon-server.appspot.com/');
32 | }
33 |
34 | x.send();
35 | }
36 |
37 | window.addEventListener('load', function() {
38 | function createDataRow(name, value) {
39 | var row = document.createElement('tr');
40 |
41 | var th = document.createElement('th');
42 | th.textContent = name;
43 | row.appendChild(th);
44 |
45 | var td = document.createElement('td');
46 | td.textContent = value;
47 | row.appendChild(td);
48 |
49 | return row;
50 | }
51 |
52 | var timeout;
53 | (function work(action) {
54 | window.clearTimeout(timeout);
55 | timeout = window.setTimeout(work, 60 * 1000); // 60s
56 |
57 | if (action) { // clicked action, clear future actions
58 | actions.textContent = '';
59 | }
60 | refresh(action, function(status) {
61 |
62 | gold.textContent = '';
63 | for (var i = 0; i < status.Gold; ++i) {
64 | var coin = document.createElement('span');
65 | coin.className = 'coin';
66 | coin.style.top = Math.random() * 100 + '%';
67 | coin.style.left = Math.random() * 100 + '%';
68 | gold.appendChild(coin);
69 | }
70 |
71 | data.textContent = '';
72 | data.appendChild(createDataRow('Gold', status.Gold));
73 | data.appendChild(createDataRow('Size', status.Size + 'kg'));
74 |
75 | // TODO: update size
76 |
77 | actions.textContent = '';
78 | status.Actions.forEach(function(action) {
79 | var button = document.createElement('button');
80 | button.addEventListener('click', function(ev) {
81 | ev.preventDefault();
82 | work(action.ID);
83 | });
84 | button.textContent = action.Name;
85 | actions.appendChild(button);
86 | });
87 |
88 | });
89 |
90 | }());
91 | });
--------------------------------------------------------------------------------
/step6-cache/dragon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step6-cache/dragon.png
--------------------------------------------------------------------------------
/step6-cache/faq.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Dragotchi
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | FAQ
47 |
48 |
49 | Are you serious?
50 | Yes.
51 | How cool are dragons?
52 | Well, it's not clear whether they are cold- or warm-blooded.
53 | Why does this exist?
54 | This site has been built as an example for Google Codelabs .
55 | Where are the images from?
56 | The logo is from Google Maps 8-bit , the "Under Construction" gif is generally available online, some assets from openclipart , other assets were built for this site.
57 |
58 |
59 |
60 |
61 |
62 |
63 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/step6-cache/icon-192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step6-cache/icon-192.png
--------------------------------------------------------------------------------
/step6-cache/index.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Dragotchi
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | Home
47 |
48 |
49 | Click and play with your very own Dragon! Feed it gold and watch it grow over time.
50 |
51 |
52 | Soon it will be an excited little dragon eating villagers and burning houses down.
53 |
54 |
55 | To get started, click on the Dragotchi link !
56 |
57 |
58 |
59 |
60 |
61 | Dragotchi is always Under Construction !!!
62 |
63 |
64 |
65 |
66 |
67 |
68 |
73 |
74 |
75 | 0
76 | 0
77 | 0
78 | 2
79 | 0
80 | 1
81 | 6
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/step6-cache/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step6-cache/logo.png
--------------------------------------------------------------------------------
/step6-cache/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "The Most Awesome Dragon Site",
3 | "short_name": "🐉🐉🐉",
4 | "display": "minimal-ui",
5 | "start_url": "/",
6 | "theme_color": "#673ab6",
7 | "background_color": "#111111",
8 | "icons": [
9 | {
10 | "src": "icon-192.png",
11 | "sizes": "192x192",
12 | "type": "image/png"
13 | }
14 | ]
15 | }
--------------------------------------------------------------------------------
/step6-cache/site.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | navigator.serviceWorker && navigator.serviceWorker.register('./sw.js').then(function(registration) {
16 | console.log('Excellent, registered with scope: ', registration.scope);
17 | });
--------------------------------------------------------------------------------
/step6-cache/styles.css:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | body {
19 | background: black url(background.jpeg) repeat-x center top;
20 | background-size: 200px;
21 | color: white;
22 | text-align: center;
23 | }
24 |
25 | a {
26 | color: #ff9249;
27 | }
28 |
29 | /** I'm so sorry. */
30 | blink {
31 | animation: blink 1s step-end infinite;
32 | }
33 | @keyframes blink {
34 | 67% { opacity: 0 }
35 | }
36 |
37 | header .logo {
38 | width: 100px;
39 | }
40 | header ul {
41 | list-style: none;
42 | padding: 0;
43 | color: #555;
44 | }
45 | header li {
46 | display: inline-block;
47 | }
48 | header li::before {
49 | content: " — ";
50 | }
51 | header li:first-child::before {
52 | content: "";
53 | }
54 |
55 | .counter {
56 | display: inline-flex;
57 | margin: 20px;
58 | }
59 |
60 | .counter span {
61 | border: 1px solid #333;
62 | width: 12px;
63 | font-family: Monospace;
64 | background: linear-gradient(to top, red, blue);
65 | }
66 |
67 | dl {
68 | width: 400px;
69 | margin: 0 auto;
70 | text-align: left;
71 | }
72 |
73 | dd {
74 | margin-bottom: 1em;
75 | }
76 |
77 | section table {
78 | margin: 0 auto;
79 | text-align: left;
80 | }
81 | section table td,
82 | section table th {
83 | min-width: 50px;
84 | }
85 |
86 | .dragon {
87 | box-sizing: border-box;
88 | padding: 20px;
89 | width: 280px;
90 | height: 280px;
91 | margin: 12px auto;
92 | border: 2px solid rgba(255, 255, 255, 0.125);
93 | background: #111;
94 | border-radius: 2px;
95 | position: relative;
96 | overflow: hidden;
97 | }
98 | .dragon #guy {
99 | position: absolute;
100 | width: 100px;
101 | margin-left: -50px;
102 | margin-top: -50px;
103 | z-index: 1000;
104 | transition: transform 1s ease-in-out;
105 | }
106 | .dragon img {
107 | width: 100%;
108 | cursor: pointer;
109 | transition: transform 0.25s;
110 | }
111 | .dragon img:hover {
112 | transform: scale(1.45) rotate(10deg);
113 | }
114 |
115 | .coin {
116 | position: absolute;
117 | opacity: 0.5;
118 | width: 10px;
119 | height: 5px;
120 | margin-left: -5px;
121 | margin-top: -2px;
122 | background: yellow;
123 | border-radius: 100%; /** oval */
124 | border-bottom: 1px solid rgba(0, 0, 0, 0.5);
125 | border-top: 1px solid rgba(255, 255, 255, 0.5);
126 | box-shadow: 0 1px 0 rgba(0, 0, 0, 0.75), 0 -1px 0 rgba(0, 0, 0, 0.25);
127 | }
128 |
129 | .actions {
130 | width: 280px;
131 | height: 80px;
132 | box-sizing: border-box;
133 | border: 2px solid rgba(255, 255, 255, 0.125);
134 | background: #211;
135 | border-radius: 2px;
136 | margin: 12px auto;
137 | display: flex;
138 | padding: 2px;
139 | }
140 |
141 | .actions button {
142 | border: 0;
143 | flex-grow: 1;
144 | width: 300%; /** forces equal size */
145 | margin-left: 2px;
146 | background: rgba(255, 255, 255, 0.125);
147 | color: white;
148 | font-size: 16px;
149 | padding: 12px;
150 | opacity: 0.8;
151 | }
152 | .actions button:first-child {
153 | margin-left: 0;
154 | }
155 | .actions button:disabled {
156 | text-decoration: line-through;
157 | }
158 | .actions button:hover:not(:disabled) {
159 | opacity: 1.0;
160 | cursor: pointer;
161 | }
--------------------------------------------------------------------------------
/step6-cache/sw.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | self.addEventListener('install', function(e) {
16 | e.waitUntil(
17 | caches.open('your-magic-cache').then(function(cache) {
18 | return cache.addAll([
19 | '/',
20 | '/index.html',
21 | '/dragon.html',
22 | '/faq.html',
23 | '/manifest.json',
24 | '/background.jpeg',
25 | '/construction.gif',
26 | '/dragon.png',
27 | '/logo.png',
28 | '/site.js',
29 | '/dragon.js',
30 | '/styles.css',
31 | ]);
32 | })
33 | );
34 | });
35 |
36 | self.addEventListener('fetch', function(event) {
37 | event.respondWith(
38 | caches.match(event.request).then(function(response) {
39 | return response || fetch(event.request);
40 | })
41 | );
42 | });
--------------------------------------------------------------------------------
/step7-imperative/background.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step7-imperative/background.jpeg
--------------------------------------------------------------------------------
/step7-imperative/clear.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Clear Cache
21 |
22 |
23 |
24 | Clear
25 |
26 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/step7-imperative/construction.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step7-imperative/construction.gif
--------------------------------------------------------------------------------
/step7-imperative/crypto.js:
--------------------------------------------------------------------------------
1 | // Copyright 2017 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function urlB64ToUint8Array(base64String) {
16 | const suffixLength = 4 - base64String.length % 4;
17 | const base64 = (base64String + '='.repeat(suffixLength))
18 | .replace(/\-/g, '+')
19 | .replace(/_/g, '/');
20 |
21 | const raw = window.atob(base64);
22 |
23 | const output = new Uint8Array(raw.length);
24 | Array.from(raw).forEach((c, i) => output[i] = c.charCodeAt(0));
25 | return output;
26 | }
27 |
28 | function stringToBase64Url(s) {
29 | const base64 = window.btoa(s);
30 | return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
31 | }
32 |
33 | function JSONToBase64Url(data) {
34 | const s = JSON.stringify(data);
35 | return stringToBase64Url(s);
36 | }
37 |
38 | function uint8ArrayToBase64Url(array) {
39 | const s = String.fromCodePoint(...array);
40 | return stringToBase64Url(s);
41 | }
42 |
43 | /**
44 | * @param {string} publicKey public key in URL-safe base64
45 | * @param {string} privateKey private key in URL-safe base64
46 | * @param {string} endpoint from PushSubscription
47 | * @param {string} sender either "mailto:" or a web address
48 | * @return {!Promise} the Authorization header
49 | */
50 | function prepareAuthorization(publicKey, privateKey, endpoint, sender) {
51 | const origin = new URL(endpoint).origin;
52 | const defaultExpiration = Math.floor(Date.now() / 1000) + 43200; // 12 hours in future
53 |
54 | const header = {
55 | typ: 'JWT',
56 | alg: 'ES256'
57 | };
58 | const jwtPayload = {
59 | aud: origin,
60 | exp: defaultExpiration,
61 | sub: sender,
62 | };
63 |
64 | // unsignedToken is the URL-safe base64 encoded JSON header and body joined by a dot.
65 | const unsignedToken = JSONToBase64Url(header) + '.' + JSONToBase64Url(jwtPayload);
66 |
67 | // Sign unsignedToken using ES256 (SHA-256 over ECDSA). This requires the private key.
68 | const publicKeyArray = urlB64ToUint8Array(publicKey);
69 | const key = {
70 | kty: 'EC',
71 | crv: 'P-256',
72 | x: uint8ArrayToBase64Url(publicKeyArray.subarray(1, 33)),
73 | y: uint8ArrayToBase64Url(publicKeyArray.subarray(33, 65)),
74 | d: privateKey,
75 | };
76 |
77 | // Perform the signing. importKey returns a Promise, so wait for it to finish.
78 | const args = {name: 'ECDSA', namedCurve: 'P-256'};
79 | return crypto.subtle.importKey('jwk', key, args, true, ['sign'])
80 | .then(key => {
81 | return crypto.subtle.sign({
82 | name: 'ECDSA',
83 | hash: {
84 | name: 'SHA-256',
85 | },
86 | }, key, (new TextEncoder('utf-8')).encode(unsignedToken));
87 | })
88 | .then(buffer => new Uint8Array(buffer))
89 | .then(signature => {
90 | return 'WebPush ' + unsignedToken + '.' + uint8ArrayToBase64Url(signature);
91 | });
92 | }
--------------------------------------------------------------------------------
/step7-imperative/dragon.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | Dragotchi
34 |
35 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
56 |
58 |
59 |
70 |
71 |
72 |
73 |
74 |
75 |
80 |
81 |
82 |
--------------------------------------------------------------------------------
/step7-imperative/dragon.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function refresh(action, callback) {
16 | var x = new XMLHttpRequest();
17 | x.onload = function() {
18 | var out = x.response;
19 | if (typeof out == 'string') {
20 | out = JSON.parse(out);
21 | }
22 | callback(out);
23 | };
24 | x.onerror = function() {
25 | alert('couldn\'t fetch dragon status');
26 | };
27 |
28 | if (action) {
29 | x.open('POST', 'https://dragon-server.appspot.com/?action=' + action);
30 | } else {
31 | x.open('GET', 'https://dragon-server.appspot.com/');
32 | }
33 |
34 | x.send();
35 | }
36 |
37 | window.addEventListener('load', function() {
38 | function createDataRow(name, value) {
39 | var row = document.createElement('tr');
40 |
41 | var th = document.createElement('th');
42 | th.textContent = name;
43 | row.appendChild(th);
44 |
45 | var td = document.createElement('td');
46 | td.textContent = value;
47 | row.appendChild(td);
48 |
49 | return row;
50 | }
51 |
52 | var timeout;
53 | (function work(action) {
54 | window.clearTimeout(timeout);
55 | timeout = window.setTimeout(work, 60 * 1000); // 60s
56 |
57 | if (action) { // clicked action, clear future actions
58 | actions.textContent = '';
59 | }
60 | refresh(action, function(status) {
61 |
62 | gold.textContent = '';
63 | for (var i = 0; i < status.Gold; ++i) {
64 | var coin = document.createElement('span');
65 | coin.className = 'coin';
66 | coin.style.top = Math.random() * 100 + '%';
67 | coin.style.left = Math.random() * 100 + '%';
68 | gold.appendChild(coin);
69 | }
70 |
71 | data.textContent = '';
72 | data.appendChild(createDataRow('Gold', status.Gold));
73 | data.appendChild(createDataRow('Size', status.Size + 'kg'));
74 |
75 | // TODO: update size
76 |
77 | actions.textContent = '';
78 | status.Actions.forEach(function(action) {
79 | var button = document.createElement('button');
80 | button.addEventListener('click', function(ev) {
81 | ev.preventDefault();
82 | work(action.ID);
83 | });
84 | button.textContent = action.Name;
85 | actions.appendChild(button);
86 | });
87 |
88 | });
89 |
90 | }());
91 | });
--------------------------------------------------------------------------------
/step7-imperative/dragon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step7-imperative/dragon.png
--------------------------------------------------------------------------------
/step7-imperative/faq.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Dragotchi
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | FAQ
47 |
48 |
49 | Are you serious?
50 | Yes.
51 | How cool are dragons?
52 | Well, it's not clear whether they are cold- or warm-blooded.
53 | Why does this exist?
54 | This site has been built as an example for Google Codelabs .
55 | Where are the images from?
56 | The logo is from Google Maps 8-bit , the "Under Construction" gif is generally available online, some assets from openclipart , other assets were built for this site.
57 |
58 |
59 |
60 |
61 |
62 |
63 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/step7-imperative/icon-192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step7-imperative/icon-192.png
--------------------------------------------------------------------------------
/step7-imperative/index.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | Dragotchi
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | Home
47 |
48 |
49 | Click and play with your very own Dragon! Feed it gold and watch it grow over time.
50 |
51 |
52 | Soon it will be an excited little dragon eating villagers and burning houses down.
53 |
54 |
55 | To get started, click on the Dragotchi link !
56 |
57 |
58 |
59 |
60 |
61 | Dragotchi is always Under Construction !!!
62 |
63 |
64 |
65 |
66 |
67 |
68 |
73 |
74 |
75 | 0
76 | 0
77 | 0
78 | 2
79 | 0
80 | 1
81 | 6
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/step7-imperative/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/step7-imperative/logo.png
--------------------------------------------------------------------------------
/step7-imperative/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "The Most Awesome Dragon Site",
3 | "short_name": "🐉🐉🐉",
4 | "display": "minimal-ui",
5 | "start_url": "/",
6 | "theme_color": "#673ab6",
7 | "background_color": "#111111",
8 | "icons": [
9 | {
10 | "src": "icon-192.png",
11 | "sizes": "192x192",
12 | "type": "image/png"
13 | }
14 | ]
15 | }
--------------------------------------------------------------------------------
/step7-imperative/site.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | navigator.serviceWorker && navigator.serviceWorker.register('./sw.js').then(function(registration) {
16 | console.log('Excellent, registered with scope: ', registration.scope);
17 | });
--------------------------------------------------------------------------------
/step7-imperative/styles.css:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | body {
19 | background: black url(background.jpeg) repeat-x center top;
20 | background-size: 200px;
21 | color: white;
22 | text-align: center;
23 | }
24 |
25 | a {
26 | color: #ff9249;
27 | }
28 |
29 | /** I'm so sorry. */
30 | blink {
31 | animation: blink 1s step-end infinite;
32 | }
33 | @keyframes blink {
34 | 67% { opacity: 0 }
35 | }
36 |
37 | header .logo {
38 | width: 100px;
39 | }
40 | header ul {
41 | list-style: none;
42 | padding: 0;
43 | color: #555;
44 | }
45 | header li {
46 | display: inline-block;
47 | }
48 | header li::before {
49 | content: " — ";
50 | }
51 | header li:first-child::before {
52 | content: "";
53 | }
54 |
55 | .counter {
56 | display: inline-flex;
57 | margin: 20px;
58 | }
59 |
60 | .counter span {
61 | border: 1px solid #333;
62 | width: 12px;
63 | font-family: Monospace;
64 | background: linear-gradient(to top, red, blue);
65 | }
66 |
67 | dl {
68 | width: 400px;
69 | margin: 0 auto;
70 | text-align: left;
71 | }
72 |
73 | dd {
74 | margin-bottom: 1em;
75 | }
76 |
77 | section table {
78 | margin: 0 auto;
79 | text-align: left;
80 | }
81 | section table td,
82 | section table th {
83 | min-width: 50px;
84 | }
85 |
86 | .dragon {
87 | box-sizing: border-box;
88 | padding: 20px;
89 | width: 280px;
90 | height: 280px;
91 | margin: 12px auto;
92 | border: 2px solid rgba(255, 255, 255, 0.125);
93 | background: #111;
94 | border-radius: 2px;
95 | position: relative;
96 | overflow: hidden;
97 | }
98 | .dragon #guy {
99 | position: absolute;
100 | width: 100px;
101 | margin-left: -50px;
102 | margin-top: -50px;
103 | z-index: 1000;
104 | transition: transform 1s ease-in-out;
105 | }
106 | .dragon img {
107 | width: 100%;
108 | cursor: pointer;
109 | transition: transform 0.25s;
110 | }
111 | .dragon img:hover {
112 | transform: scale(1.45) rotate(10deg);
113 | }
114 |
115 | .coin {
116 | position: absolute;
117 | opacity: 0.5;
118 | width: 10px;
119 | height: 5px;
120 | margin-left: -5px;
121 | margin-top: -2px;
122 | background: yellow;
123 | border-radius: 100%; /** oval */
124 | border-bottom: 1px solid rgba(0, 0, 0, 0.5);
125 | border-top: 1px solid rgba(255, 255, 255, 0.5);
126 | box-shadow: 0 1px 0 rgba(0, 0, 0, 0.75), 0 -1px 0 rgba(0, 0, 0, 0.25);
127 | }
128 |
129 | .actions {
130 | width: 280px;
131 | height: 80px;
132 | box-sizing: border-box;
133 | border: 2px solid rgba(255, 255, 255, 0.125);
134 | background: #211;
135 | border-radius: 2px;
136 | margin: 12px auto;
137 | display: flex;
138 | padding: 2px;
139 | }
140 |
141 | .actions button {
142 | border: 0;
143 | flex-grow: 1;
144 | width: 300%; /** forces equal size */
145 | margin-left: 2px;
146 | background: rgba(255, 255, 255, 0.125);
147 | color: white;
148 | font-size: 16px;
149 | padding: 12px;
150 | opacity: 0.8;
151 | }
152 | .actions button:first-child {
153 | margin-left: 0;
154 | }
155 | .actions button:disabled {
156 | text-decoration: line-through;
157 | }
158 | .actions button:hover:not(:disabled) {
159 | opacity: 1.0;
160 | cursor: pointer;
161 | }
--------------------------------------------------------------------------------
/step7-imperative/sw.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | self.addEventListener('install', function(e) {
16 | e.waitUntil(
17 | caches.open('your-magic-cache').then(function(cache) {
18 | return cache.addAll([
19 | '/',
20 | '/index.html',
21 | '/dragon.html',
22 | '/faq.html',
23 | '/manifest.json',
24 | '/background.jpeg',
25 | '/construction.gif',
26 | '/dragon.png',
27 | '/logo.png',
28 | '/site.js',
29 | '/dragon.js',
30 | '/styles.css',
31 | ]);
32 | })
33 | );
34 | });
35 |
36 | self.addEventListener('fetch', function(event) {
37 | if (event.request.url == 'https://dragon-server.appspot.com/') {
38 | console.info('responding to dragon-server fetch with Service Worker! 🤓');
39 | event.respondWith(fetch(event.request).catch(function(e) {
40 | let out = {Gold: 1, Size: -1, Actions: []};
41 | return new Response(JSON.stringify(out));
42 | }));
43 | return;
44 | }
45 |
46 | event.respondWith(
47 | caches.match(event.request).then(function(response) {
48 | return response || fetch(event.request);
49 | })
50 | );
51 | });
--------------------------------------------------------------------------------
/work/background.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/work/background.jpeg
--------------------------------------------------------------------------------
/work/clear.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Clear Cache
21 |
22 |
23 |
24 | Clear
25 |
26 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/work/construction.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/work/construction.gif
--------------------------------------------------------------------------------
/work/crypto.js:
--------------------------------------------------------------------------------
1 | // Copyright 2017 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function urlB64ToUint8Array(base64String) {
16 | const suffixLength = 4 - base64String.length % 4;
17 | const base64 = (base64String + '='.repeat(suffixLength))
18 | .replace(/\-/g, '+')
19 | .replace(/_/g, '/');
20 |
21 | const raw = window.atob(base64);
22 |
23 | const output = new Uint8Array(raw.length);
24 | Array.from(raw).forEach((c, i) => output[i] = c.charCodeAt(0));
25 | return output;
26 | }
27 |
28 | function stringToBase64Url(s) {
29 | const base64 = window.btoa(s);
30 | return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
31 | }
32 |
33 | function JSONToBase64Url(data) {
34 | const s = JSON.stringify(data);
35 | return stringToBase64Url(s);
36 | }
37 |
38 | function uint8ArrayToBase64Url(array) {
39 | const s = String.fromCodePoint(...array);
40 | return stringToBase64Url(s);
41 | }
42 |
43 | /**
44 | * @param {string} publicKey public key in URL-safe base64
45 | * @param {string} privateKey private key in URL-safe base64
46 | * @param {string} endpoint from PushSubscription
47 | * @param {string} sender either "mailto:" or a web address
48 | * @return {!Promise} the Authorization header
49 | */
50 | function prepareAuthorization(publicKey, privateKey, endpoint, sender) {
51 | const origin = new URL(endpoint).origin;
52 | const defaultExpiration = Math.floor(Date.now() / 1000) + 43200; // 12 hours in future
53 |
54 | const header = {
55 | typ: 'JWT',
56 | alg: 'ES256'
57 | };
58 | const jwtPayload = {
59 | aud: origin,
60 | exp: defaultExpiration,
61 | sub: sender,
62 | };
63 |
64 | // unsignedToken is the URL-safe base64 encoded JSON header and body joined by a dot.
65 | const unsignedToken = JSONToBase64Url(header) + '.' + JSONToBase64Url(jwtPayload);
66 |
67 | // Sign unsignedToken using ES256 (SHA-256 over ECDSA). This requires the private key.
68 | const publicKeyArray = urlB64ToUint8Array(publicKey);
69 | const key = {
70 | kty: 'EC',
71 | crv: 'P-256',
72 | x: uint8ArrayToBase64Url(publicKeyArray.subarray(1, 33)),
73 | y: uint8ArrayToBase64Url(publicKeyArray.subarray(33, 65)),
74 | d: privateKey,
75 | };
76 |
77 | // Perform the signing. importKey returns a Promise, so wait for it to finish.
78 | const args = {name: 'ECDSA', namedCurve: 'P-256'};
79 | return crypto.subtle.importKey('jwk', key, args, true, ['sign'])
80 | .then(key => {
81 | return crypto.subtle.sign({
82 | name: 'ECDSA',
83 | hash: {
84 | name: 'SHA-256',
85 | },
86 | }, key, (new TextEncoder('utf-8')).encode(unsignedToken));
87 | })
88 | .then(buffer => new Uint8Array(buffer))
89 | .then(signature => {
90 | return 'WebPush ' + unsignedToken + '.' + uint8ArrayToBase64Url(signature);
91 | });
92 | }
--------------------------------------------------------------------------------
/work/dragon.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 | Dragotchi
32 |
33 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
54 |
56 |
57 |
68 |
69 |
70 |
71 |
72 |
73 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/work/dragon.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | function refresh(action, callback) {
16 | var x = new XMLHttpRequest();
17 | x.onload = function() {
18 | var out = x.response;
19 | if (typeof out == 'string') {
20 | out = JSON.parse(out);
21 | }
22 | callback(out);
23 | };
24 | x.onerror = function() {
25 | alert('couldn\'t fetch dragon status');
26 | };
27 |
28 | if (action) {
29 | x.open('POST', 'https://dragon-server.appspot.com/?action=' + action);
30 | } else {
31 | x.open('GET', 'https://dragon-server.appspot.com/');
32 | }
33 |
34 | x.send();
35 | }
36 |
37 | window.addEventListener('load', function() {
38 | function createDataRow(name, value) {
39 | var row = document.createElement('tr');
40 |
41 | var th = document.createElement('th');
42 | th.textContent = name;
43 | row.appendChild(th);
44 |
45 | var td = document.createElement('td');
46 | td.textContent = value;
47 | row.appendChild(td);
48 |
49 | return row;
50 | }
51 |
52 | var timeout;
53 | (function work(action) {
54 | window.clearTimeout(timeout);
55 | timeout = window.setTimeout(work, 60 * 1000); // 60s
56 |
57 | if (action) { // clicked action, clear future actions
58 | actions.textContent = '';
59 | }
60 | refresh(action, function(status) {
61 |
62 | gold.textContent = '';
63 | for (var i = 0; i < status.Gold; ++i) {
64 | var coin = document.createElement('span');
65 | coin.className = 'coin';
66 | coin.style.top = Math.random() * 100 + '%';
67 | coin.style.left = Math.random() * 100 + '%';
68 | gold.appendChild(coin);
69 | }
70 |
71 | data.textContent = '';
72 | data.appendChild(createDataRow('Gold', status.Gold));
73 | data.appendChild(createDataRow('Size', status.Size + 'kg'));
74 |
75 | // TODO: update size
76 |
77 | actions.textContent = '';
78 | status.Actions.forEach(function(action) {
79 | var button = document.createElement('button');
80 | button.addEventListener('click', function(ev) {
81 | ev.preventDefault();
82 | work(action.ID);
83 | });
84 | button.textContent = action.Name;
85 | actions.appendChild(button);
86 | });
87 |
88 | });
89 |
90 | }());
91 | });
--------------------------------------------------------------------------------
/work/dragon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/work/dragon.png
--------------------------------------------------------------------------------
/work/faq.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | Dragotchi
31 |
32 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 | FAQ
45 |
46 |
47 | Are you serious?
48 | Yes.
49 | How cool are dragons?
50 | Well, it's not clear whether they are cold- or warm-blooded.
51 | Why does this exist?
52 | This site has been built as an example for Google Codelabs .
53 | Where are the images from?
54 | The logo is from Google Maps 8-bit , the "Under Construction" gif is generally available online, some assets from openclipart , other assets were built for this site.
55 |
56 |
57 |
58 |
59 |
60 |
61 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/work/icon-192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/work/icon-192.png
--------------------------------------------------------------------------------
/work/index.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | Dragotchi
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | Dragotchi
31 |
32 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 | Home
45 |
46 |
47 | Click and play with your very own Dragon! Feed it gold and watch it grow over time.
48 |
49 |
50 | Soon it will be an excited little dragon eating villagers and burning houses down.
51 |
52 |
53 | To get started, click on the Dragotchi link !
54 |
55 |
56 |
57 |
58 |
59 | Dragotchi is always Under Construction !!!
60 |
61 |
62 |
63 |
64 |
65 |
66 |
71 |
72 |
73 | 0
74 | 0
75 | 0
76 | 2
77 | 0
78 | 1
79 | 6
80 |
81 |
82 |
83 |
--------------------------------------------------------------------------------
/work/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/googlecodelabs/migrate-to-progressive-web-apps/05cb262d5b591e8a601192eb44f0afe987949140/work/logo.png
--------------------------------------------------------------------------------
/work/site.js:
--------------------------------------------------------------------------------
1 | // Copyright 2016 Google Inc.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | /** Intentionally empty */
16 |
--------------------------------------------------------------------------------
/work/styles.css:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 |
18 | body {
19 | background: black url(background.jpeg) repeat-x center top;
20 | background-size: 200px;
21 | color: white;
22 | text-align: center;
23 | }
24 |
25 | a {
26 | color: #ff9249;
27 | }
28 |
29 | /** I'm so sorry. */
30 | blink {
31 | animation: blink 1s step-end infinite;
32 | }
33 | @keyframes blink {
34 | 67% { opacity: 0 }
35 | }
36 |
37 | header .logo {
38 | width: 100px;
39 | }
40 | header ul {
41 | list-style: none;
42 | padding: 0;
43 | color: #555;
44 | }
45 | header li {
46 | display: inline-block;
47 | }
48 | header li::before {
49 | content: " — ";
50 | }
51 | header li:first-child::before {
52 | content: "";
53 | }
54 |
55 | .counter {
56 | display: inline-flex;
57 | margin: 20px;
58 | }
59 |
60 | .counter span {
61 | border: 1px solid #333;
62 | width: 12px;
63 | font-family: Monospace;
64 | background: linear-gradient(to top, red, blue);
65 | }
66 |
67 | dl {
68 | width: 400px;
69 | margin: 0 auto;
70 | text-align: left;
71 | }
72 |
73 | dd {
74 | margin-bottom: 1em;
75 | }
76 |
77 | section table {
78 | margin: 0 auto;
79 | text-align: left;
80 | }
81 | section table td,
82 | section table th {
83 | min-width: 50px;
84 | }
85 |
86 | .dragon {
87 | box-sizing: border-box;
88 | padding: 20px;
89 | width: 280px;
90 | height: 280px;
91 | margin: 12px auto;
92 | border: 2px solid rgba(255, 255, 255, 0.125);
93 | background: #111;
94 | border-radius: 2px;
95 | position: relative;
96 | overflow: hidden;
97 | }
98 | .dragon #guy {
99 | position: absolute;
100 | width: 100px;
101 | margin-left: -50px;
102 | margin-top: -50px;
103 | z-index: 1000;
104 | transition: transform 1s ease-in-out;
105 | }
106 | .dragon img {
107 | width: 100%;
108 | cursor: pointer;
109 | transition: transform 0.25s;
110 | }
111 | .dragon img:hover {
112 | transform: scale(1.45) rotate(10deg);
113 | }
114 |
115 | .coin {
116 | position: absolute;
117 | opacity: 0.5;
118 | width: 10px;
119 | height: 5px;
120 | margin-left: -5px;
121 | margin-top: -2px;
122 | background: yellow;
123 | border-radius: 100%; /** oval */
124 | border-bottom: 1px solid rgba(0, 0, 0, 0.5);
125 | border-top: 1px solid rgba(255, 255, 255, 0.5);
126 | box-shadow: 0 1px 0 rgba(0, 0, 0, 0.75), 0 -1px 0 rgba(0, 0, 0, 0.25);
127 | }
128 |
129 | .actions {
130 | width: 280px;
131 | height: 80px;
132 | box-sizing: border-box;
133 | border: 2px solid rgba(255, 255, 255, 0.125);
134 | background: #211;
135 | border-radius: 2px;
136 | margin: 12px auto;
137 | display: flex;
138 | padding: 2px;
139 | }
140 |
141 | .actions button {
142 | border: 0;
143 | flex-grow: 1;
144 | width: 300%; /** forces equal size */
145 | margin-left: 2px;
146 | background: rgba(255, 255, 255, 0.125);
147 | color: white;
148 | font-size: 16px;
149 | padding: 12px;
150 | opacity: 0.8;
151 | }
152 | .actions button:first-child {
153 | margin-left: 0;
154 | }
155 | .actions button:disabled {
156 | text-decoration: line-through;
157 | }
158 | .actions button:hover:not(:disabled) {
159 | opacity: 1.0;
160 | cursor: pointer;
161 | }
--------------------------------------------------------------------------------