Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (57 loc) · 1.77 KB
/
Copy pathindex.js
File metadata and controls
71 lines (57 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(function () {
"use strict";
var worker = new SharedWorker('shared-worker.js', 'my-shared-scope')
, log = document.getElementById('log')
, $ = require('ender')
, window = require('window')
, addWindow
, sendMessage
, Notifications = window.webkitNotifications || window.mozNotifications
;
worker.port.addEventListener('message', function (e) {
log.textContent += '\n' + e.data.msg;
}, false);
worker.port.start();
worker.port.postMessage({ msg: 'ping' });
function addIframe() {
console.log('adding another iframe');
$('body').append($("<iframe src='inner.html'>"));
}
sendMessage = (function () {
var count = 0
;
return function (ev) {
ev.preventDefault();
var msg = $(this).find('.js-message').val()
;
console.log('going to send message:', msg);
worker.port.postMessage({ msg: '[' + count + ']' + msg });
count += 1;
};
}());
addWindow = (function () {
var count = 0
;
return function () {
window.open('inner.html', 'win' + count);
count += 1;
};
}());
function askPermissionForNotifications() {
console.log('Receiver asking permission to use remote');
Notifications.requestPermission(function () {
if (0 !== Notifications.checkPermission()) {
return;
}
// Browser supports it :)
Notifications.createHTMLNotification('inner.html').show();
});
}
function attachHandlers() {
$('body').delegate('.js-add-iframe', 'click', addIframe);
$('body').delegate('.js-add-window', 'click', addWindow);
$('body').delegate('.js-add-notifications', 'click', askPermissionForNotifications);
$('body').delegate('form.js-messenger', 'submit', sendMessage);
}
$.domReady(attachHandlers);
}());