-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclick.js
144 lines (121 loc) · 4.74 KB
/
click.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
// refresh the whole thing
let localkey = "miles2";
function getConversationData(user, arg) {
const data = localStorage.getItem("miles");
if (data) {
try {
const parsedData = JSON.parse(data);
const conversationData = parsedData.conversation_data[user];
if (conversationData && conversationData.length > 0) {
if (arg === "goal") {
return conversationData[0].trim();
} else if (arg === "preamble") {
return conversationData[1].trim();
}
}
} catch (error) {
console.error("Error parsing data from localStorage:", error);
return "";
}
}
return "";
}
/* Save the "self" user name */
function saveUserName(self_user) {
const existingData = JSON.parse(localStorage.getItem(localkey)) || {
conversation_data: {
"test": ["chhatri leni hai", "chats", [0, 20, 40, 50, 60, 70, 80], ""],
},
toggleState: "",
user_name: "",
};
existingData.user_name = self_user;
localStorage.setItem(localkey, JSON.stringify(existingData));
}
/* Get the "self" user name */
function getUserName() {
const data = localStorage.getItem(localkey);
if (data) {
try {
const parsedData = JSON.parse(data);
return parsedData.user_name;
} catch (error) {
console.error("Error parsing data from localStorage:", error);
return "";
}
}
return "";
}
// refresh things
async function refreshClick(data) {
const response = await fetch("https://miles.gamhcrew.repl.co/refresh", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const result = await response.json();
return result.status;
}
/* main */
document.addEventListener('click', function () {
// Add a small delay to ensure that the DOM has been updated
setTimeout(function () {
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 = getConversationData(otherUser, "goal");
// 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
}
*/
// Send a message to the background.js
const status = refreshClick({
thisUser: thisUser,
otherUser: otherUser,
isGroup: isGroup,
goal: goal,
chats: chats,
userID: userID,
otherUserID: otherUserID
})
}, 1000); // 1000ms delay, you can adjust this value as needed
});