-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcacbridge.js
More file actions
77 lines (66 loc) · 1.92 KB
/
Copy pathcacbridge.js
File metadata and controls
77 lines (66 loc) · 1.92 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
72
73
74
75
76
77
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.CAC = factory();
}
}(this, function () {
var callbacks = [];
var isActive = false;
function sign(str, success, fail) {
if (!isActive) {
fail();
return;
}
window.postMessage({
type: "ChromeCAC_sign",
payload: str,
_id: callbacks.length
}, "*"); // TODO dont use "*"
callbacks.push({
success: success,
fail: fail
});
}
window.addEventListener("message", function (event) {
console.log("[Webpage] Recv'd message:");
console.log(event);
if (event.source != window || !event.isTrusted || (!'type' in event.data))
return;
if (event.data.type == "ChromeCAC_setactive") {
isActive = true;
console.log("ChromeCAC is active.")
} else if (event.data.type == "ChromeCAC_signed") {
var cb = callbacks[event.data.data._id];
if (event.data.success) {
cb.success(event.data.signature);
}
else {
cb.fail({
data: event.data
});
}
}
})
function init() {
window.postMessage({
type: "ChromeCAC_init"
}, "*");
}
if (document.readyState != 'loading') {
init();
}
else {
window.addEventListener("load", init);
}
return {
sign: sign
};
}));