Skip to content

Instantly share code, notes, and snippets.

@balazs
Created January 28, 2015 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balazs/dd2f19a2c660bbc116fd to your computer and use it in GitHub Desktop.
Save balazs/dd2f19a2c660bbc116fd to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<!--
Copyright 2014 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- <link rel="manifest" href="Manifest.json" /> -->
<script>
var SCOPE = {
scope: '/'
};
function setCheckbox(id, checked) {
document.getElementById(id).checked = checked;
}
function appendLog(id, text) {
document.getElementById(id).innerHTML += "<br/>" + text;
}
function registerSW() {
navigator.serviceWorker.register('service-worker.js', SCOPE).then(
function (swReg) {
console.log('SW registered');
setCheckbox("swDisplay", true);
var anySW = swReg.active || swReg.installing || swReg.waiting;
console.log('anySW: ' + JSON.stringify(anySW));
anySW.postMessage('wazzup sw?');
},
function (err) {
console.error('sw reg failed: ' + err);
}
)
// .catch(
// function (err) {
// console.error('smg failed: ' + err);
// }
// );
}
function unregisterSW() {
navigator.serviceWorker.unregister(SCOPE).then(
function() {
console.log('SW unregistered');
setCheckbox("swDisplay", false);
}
);
}
this.onmessage = function(event) {
console.log("msg from SW: " + event.data);
appendLog('swmsg', event.data);
}
function sendMsgToSw() {
msg = 'Tell me something nice, please.';
navigator.serviceWorker.controller.postMessage(msg);
appendLog('toswmsg', msg);
}
</script>
<link rel="prefetch" href="service-worker.js" />
<body>
<p>
<span>Service worker active: </span><input type="checkbox" id="swDisplay" />
</p>
<button onclick="registerSW()">Register SW</button>
<button onclick="unregisterSW()">Unregister SW</button>
<p>
<button onclick="sendMsgToSw()">Send to SW</button>
</p>
<div id="toswmsg">--We say--</div>
<div id="swmsg">--SW says--</div>
</body>
{
"gcm_sender_id": "766134470826"
}
this.onpush = function(event) {
console.log("Push message received");
console.log(event.data);
}
function sendMsg(msg) {
this.clients.getAll().then(
function (clients) {
clients[0].postMessage(msg);
}
);
}
this.onmessage = function(event) {
console.log("msg: " + event.data);
if (event.data.startsWith('wazzup')) {
sendMsg('Greetings Master!');
} else {
sendMsg("Luke, I am your SERVICE WORKER!");
}
}
this.oninstall = function(event) {
console.log("dude, I'm installed");
}
this.onactivate = function(event) {
console.log("dude, I'm activated");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment