-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-sdc.js
61 lines (52 loc) · 1.65 KB
/
js-sdc.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
/**
* js-sdc
* a simple html same domain communication by localStorage storage event
*/
//prifix random num
var SDCFingerPrinter = "SDC_prefix_rdm_7591_";
//store channle and hanle
var SDCHandleMap = [];
/**
* init addEventListener 'storage' event
*/
function init() {
window.addEventListener("storage", function (e) {
for (kh of SDCHandleMap) {
if (kh.key === e.key) {
if (e.newValue === SDCFingerPrinter) {
continue;
}
kh.handle(JSON.parse(e.newValue));
window.localStorage.setItem(kh.key, SDCFingerPrinter);
}
}
});
}
/**
* minitor channel message
* @param channel
* @param handle
*/
onSDCMessage: function (channel, handle) {
if (channel === '' || typeof handle !== 'function') {
console.error('SDCMessage failure: channel is "" or handle is not a function');
return;
}
window.localStorage.setItem(SDCFingerPrinter + channel, SDCFingerPrinter);
SDCHandleMap.push({ key: SDCFingerPrinter + channel, handle: handle });
}
/**
* send messsage to channle, if onSDCMessage and sendSDCMessage is on same Page, isSamePage need to be true
* @param channel
* @param message
*/
sendSDCMessage: function (channel, message, isSamePage) {
window.localStorage.setItem(SDCFingerPrinter + channel, JSON.stringify(message));
if (isSamePage === true) {
var event = new Event("storage");
event.key = SDCFingerPrinter + channel;
event.newValue = JSON.stringify(message);
window.dispatchEvent(event);
}
}
init();