-
Notifications
You must be signed in to change notification settings - Fork 4
/
history-recorder.js
96 lines (91 loc) · 2.71 KB
/
history-recorder.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Save chat history to file.
(() => {
// Get Chat Bing Messages
const messages = [];
const images = [];
const mainElm = document
.querySelector("cib-serp")
.shadowRoot.querySelector("#cib-conversation-main")
.shadowRoot.querySelector("#cib-chat-main");
const turnElms = mainElm.querySelectorAll("cib-chat-turn");
const messageElms = [];
for (let turnElm of turnElms) {
for (let group of turnElm.shadowRoot.querySelectorAll(
"cib-message-group"
)) {
messageElms.push(group);
}
}
for (let messageElm of messageElms) {
const source = messageElm.getAttribute("source");
let msg = "";
switch (source) {
case "user":
const userMessages = messageElm.shadowRoot
.querySelector(".cib-message-main")
.shadowRoot.querySelectorAll(".content");
for (let item of userMessages) {
msg = item.textContent;
}
break;
case "bot":
const botMessages = messageElm.shadowRoot.querySelectorAll(
`.cib-message-main[type="text"]`
);
for (let item of botMessages) {
msg = item.shadowRoot.querySelector(".ac-textBlock").textContent;
}
// Save images
const imgIframes = messageElm.shadowRoot.querySelectorAll(
`.cib-message-main[type="host"]`
);
if (imgIframes && imgIframes.length > 0) {
let pics =
imgIframes[0].shadowRoot.firstElementChild?.contentDocument.querySelectorAll(
".mimg"
);
if (pics) {
for (let img of pics) {
images.push(img.src);
}
}
}
break;
default:
break;
}
messages.push({ source, msg });
}
let result = "";
for (let item of messages) {
result += item.source.toUpperCase() + " : \n" + item.msg + "\n";
}
// Save test text to file.
const a = document.createElement("a");
const file = new Blob([result], { type: "text/plain" });
a.href = URL.createObjectURL(file);
a.download = Math.random().toString(36).substring(2) + ".txt";
document.body.appendChild(a);
a.click();
a.parentNode.removeChild(a);
// Save images to file.
for (let img of images) {
fetch(img, {
headers: new Headers({
Origin: location.origin,
}),
mode: "cors",
})
.then((response) => response.blob())
.then((blob) => {
let blobUrl = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.download = `${Math.random().toString(36).substring(2)}.jpg`;
a.href = blobUrl;
document.body.appendChild(a);
a.click();
a.remove();
})
.catch((e) => console.error(e));
}
})();