-
Notifications
You must be signed in to change notification settings - Fork 43
/
content-script.js
77 lines (54 loc) · 1.59 KB
/
content-script.js
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
72
73
74
75
76
77
var verbose = true;
function log() {
if( !verbose ) return;
console.log.apply(
console, [
`%c WebVREmu `,
'background: #007AA3; color: #ffffff; text-shadow: 0 -1px #000; padding: 4px 0 4px 0; line-height: 0',
...arguments
]
);
}
log( 'Polyfill', window.location.toString() );
var port = chrome.runtime.connect( { name: 'contentScript' } );
function post( msg ) {
port.postMessage( msg );
}
post( { action: 'script-ready' } );
var cloneInto = cloneInto || null;
function createCustomEvent(type, detail) {
// Use cloneInto on Firefox (which prevents the security
// sandbox from raising exception when the javascript
// code in the webpage content is going to access to the
// event properties).
detail = cloneInto ? cloneInto(detail, window) : detail;
return new CustomEvent( type, { detail });
}
port.onMessage.addListener( function( msg ) {
switch( msg.action ) {
case 'pose':
var e = createCustomEvent( 'webvr-pose', {
position: msg.position,
rotation: msg.rotation
} );
window.dispatchEvent( e );
break;
case 'hmd-activate':
var e = createCustomEvent( 'webvr-hmd-activate', {
state: msg.value
} );
window.dispatchEvent( e );
break;
}
} );
window.addEventListener( 'webvr-ready', function() {
post( { action: 'page-ready' } );
} );
window.addEventListener( 'webvr-resetpose', function() {
post( { action: 'reset-pose' } );
} );
var source = '(' + injectedScript + ')();';
var script = document.createElement('script');
script.textContent = source;
(document.head||document.documentElement).appendChild(script);
script.parentNode.removeChild(script);