-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
60 lines (51 loc) · 2.4 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Sender</title>
</head>
<body>
<h1>Sender Window</h1>
<button id="send-message">Send Message</button>
<button id="post-msg">Post Message</button>
<div id="response"></div>
<iframe id="receiver-iframe" src="/receiver.html" style="width:300px; height:100px;"></iframe>
<script src="https://cdn.jsdelivr.net/npm/window-post-message-proxy@0.2.6/dist/windowPostMessageProxy.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/http-post-message@0.2.3/dist/httpPostMessage.min.js"></script>
<script>
const hpm = window['http-post-message'];
const wpmp = window['window-post-message-proxy'];
// 配置 window-post-message-proxy
const windowPostMessageProxy = new wpmp.WindowPostMessageProxy({
processTrackingProperties: {
addTrackingProperties: hpm.HttpPostMessage.addTrackingProperties,
getTrackingProperties: hpm.HttpPostMessage.getTrackingProperties,
},
isErrorMessage: hpm.HttpPostMessage.isErrorMessage
});
const receiverIframe = document.getElementById('receiver-iframe').contentWindow;
// 创建 HttpPostMessage 实例
const httpPostMessage = new hpm.HttpPostMessage(windowPostMessageProxy, {}, receiverIframe);
document.getElementById('send-message').addEventListener('click', () => {
// 发送 GET 请求
httpPostMessage.get('http://example.com/api/data', { data: 'request data' })
.then(response => {
document.getElementById('response').innerText = 'Response received: ' + JSON.stringify(response);
})
.catch(error => {
document.getElementById('response').innerText = 'Error occurred: ' + error;
});
});
// postq
document.getElementById('post-msg').addEventListener('click', () => {
// 发送 POST 请求
httpPostMessage.post('http://example.com/api/data', { data: 'request data' })
.then(response => {
document.getElementById('response').innerText = 'Response received: ' + JSON.stringify(response);
})
.catch(error => {
document.getElementById('response').innerText = 'Error occurred: ' + error;
});
});
</script>
</body>
</html>