-
Notifications
You must be signed in to change notification settings - Fork 1
/
reply.js
161 lines (137 loc) · 5.23 KB
/
reply.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// this file accomplishes the task of 'content.js' from version 0.2 (miles-frontend-v0.2)
/* Save the "self" user name */
function saveUserName(self_user) {
const existingData = JSON.parse(localStorage.getItem("miles2")) || {
conversation_data: {
"test": ["chhatri leni hai", "chats", [0, 20, 40, 50, 60, 70, 80], ""],
},
toggleState: "",
user_name: "",
};
existingData.user_name = self_user;
localStorage.setItem("miles2", JSON.stringify(existingData));
}
/* Get the "self" user name */
function getUserName() {
const data = localStorage.getItem("miles2");
if (data) {
try {
const parsedData = JSON.parse(data);
return parsedData.user_name;
} catch (error) {
console.error("Error parsing data from localStorage:", error);
return "";
}
}
return "";
}
// get context from conversation
function fetchMessages(sentgoal) {
const chatElements = document.querySelectorAll(".copyable-text");
const otherUser = document.querySelector("._3W2ap")
? document.querySelector("._3W2ap").innerText
: "";
let thisUser = getUserName() ? getUserName() : "";
const usernames = new Set();
const chats = Array.from(chatElements)
.filter((element) => element.getAttribute("data-pre-plain-text")) // Filter out elements with no 'data-pre-plain-text' attribute
.map((element) => {
const info = element.getAttribute("data-pre-plain-text");
// const username = info.trim().slice(20, -1);
const usernameStartIndex = info.indexOf("] ") + 2;
const usernameEndIndex = info.lastIndexOf(":");
const username = info.slice(usernameStartIndex, usernameEndIndex);
usernames.add(username); // Add username to a set
const messageElement = element.querySelector(".selectable-text");
const messageText = messageElement ? messageElement.innerText : "";
// If the message is an outgoing message (from "You:"), then set the thisUser
if (element.closest(".message-out")) {
thisUser = username;
saveUserName(thisUser);
}
return {
sender: username,
message: messageText,
};
});
// If there are more than two users, including "thisUser", then it's a group chat
const isGroup = usernames.size > 2;
// Sending goal for this conversation (if any)
const goal = sentgoal;
// WA number
const userID = localStorage.getItem('last-wid-md') ? localStorage.getItem('last-wid-md').match(/"(\d+):/)[1] : "";
const otherUserID = document.querySelector('[data-id]').getAttribute('data-id').match(/_(\d+)@/)[1];
/*
{
thisUser: string,
otherUser: string,
isGroup: boolean,
goal: string,
chats: array,
userID: string,
otherUserID: string
}
*/
return {
thisUser: thisUser,
otherUser: otherUser,
isGroup: isGroup,
goal: goal,
chats: chats,
userID: userID,
otherUserID: otherUserID
}
}
// fetch reply from server
async function get_repy_from_server(data) {
console.log("generating reply...");
const response = await fetch("https://a26018af-2571-4e26-814a-d76d486b7bcf-00-38iqwn06wxrli.asia-a.replit.dev/get_reply", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const result = await response.json();
return result.reply;
}
// display the generated reply
function placeSuggestedReply(reply) {
const inputBar = document.querySelector("footer");
const replyElement = document.createElement("div");
replyElement.style.backgroundColor = "#1E1E26";
replyElement.style.opacity = "100%";
replyElement.style.borderRadius = "5px";
replyElement.style.padding = "8px";
replyElement.style.width = "60%";
replyElement.style.height = "auto";
replyElement.style.margin = "20px";
replyElement.style.cursor = "pointer";
replyElement.style.border = "1px solid rgba(255, 255, 255, 0.8)";
replyElement.style.boxShadow = "0px 0px 10px rgba(214, 73, 107, 0.7)";
replyElement.innerText = reply;
inputBar.insertBefore(replyElement, inputBar.firstChild);
replyElement.addEventListener("click", () => {
navigator.clipboard.writeText(reply).then(
function () {
console.log("Copying to clipboard was successful!");
},
function (err) {
console.error("Could not copy text: ", err);
}
);
replyElement.remove();
});
}
// main
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.backup) {
(async () => {
const data = fetchMessages(message.goal);
sendResponse(data);
const reply = await get_repy_from_server(data);
console.log('generated successfully');
placeSuggestedReply(reply);
})();
}
});