-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediator-worker-client.js
72 lines (66 loc) · 1.87 KB
/
mediator-worker-client.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
/*
@license https://github.com/t2ym/reportage/blob/master/LICENSE.md
Copyright (c) 2023 Tetsuya Mori <[email protected]>. All rights reserved.
*/
import {
NAME_MEDIATOR,
NAME_MEDIATOR_BRIDGE,
NAME_REPORTER,
TYPE_TRANSFER_PORT,
TYPE_COVERAGE,
TYPE_CONNECT,
TYPE_DETACH,
} from './constants.js';
const targetAppOrigin = new URL(document.referrer).origin;
try {
const worker = new SharedWorker('./mediator-worker.js', { /* type: 'module',*/ name: NAME_MEDIATOR }); // module worker has not been implemented in Firefox
worker.port.onmessage = (event) => {
const { type, transfer } = event.data;
if (type === TYPE_TRANSFER_PORT) {
window.opener.postMessage({
type: TYPE_TRANSFER_PORT,
source: NAME_MEDIATOR_BRIDGE,
transfer: transfer,
}, targetAppOrigin, transfer);
}
else if (type === TYPE_DETACH) {
console.log(`mediator-worker-client.js: message `, event.data);
window.opener.close();
}
}
worker.port.postMessage({
type: TYPE_CONNECT,
targetAppOrigin: targetAppOrigin,
}, []);
setInterval(() => {
if (!window.opener) {
window.close();
}
}, 1000);
if (globalThis.__coverage__) {
console.log(`${import.meta.url}: globalThis.__coverage__ found`);
window.addEventListener('beforeunload', (event) => {
const target = window.opener;
if (target) {
target.postMessage({
type: TYPE_COVERAGE,
source: NAME_MEDIATOR_BRIDGE,
target: NAME_REPORTER,
__coverage__: [ globalThis.__coverage__ ],
}, targetAppOrigin);
}
else {
worker.port.postMessage({
type: TYPE_COVERAGE,
source: NAME_MEDIATOR_BRIDGE,
target: NAME_REPORTER,
__coverage__: [ globalThis.__coverage__ ],
});
}
});
}
}
catch (e) {
console.error('Fatal Exception:', e);
//window.close();
}