-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinit.js
188 lines (142 loc) Β· 6.42 KB
/
init.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
(async function () {
console.log('%cCopyGPT (Copy For ChatGPT Extension) Loaded', 'color: green; font-size: 30px');
const currentYear = new Date().getFullYear()
console.log(`
- π Copy Button: Enabled β
- β¨οΈ CMD+K Hotkey: Enabled β
- βοΈ Up Arrow Key: Enabled β
- π§Ό Anti-Formatter: Enabled β
- β¨ AI plagiarism checker: Enabled β
Copyright (c) ${currentYear} Sethu Senthil
Version: 0.3.11
https://copygpt.sethusenthil.com
https://sethusenthil.com
`)
const CHAT_TEXT_SELECTOR = '.markdown';
const CLIPBOARD_CLASS_NAME = 'copy-to-clipboard';
const showSnackbar = (message, position) => {
const snack = document.createElement('div');
snack.classList.add('snackbar');
if (position) {
//default position is bottom
snack.classList.add(position);
} else {
snack.classList.add('bottom');
}
snack.innerText = message;
document.body.appendChild(snack);
snack.classList.add('show');
setTimeout(function () {
snack.className = snack.className.replace("show", "");
snack.remove();
}, 3000);
};
chrome.runtime.onMessage.addListener(function (responses, sender, sendResponse) {
//console.log('response', responses);
let avgScore = 0;
responses.message.forEach((response) => {
avgScore += response[0].score;
});
avgScore = avgScore / responses.length;
if (avgScore <= 0.50) {
showSnackbar('This text was flagged as AI generated by plagiarism detectors, make some changes before submitting! π¨', 'top');
} else {
showSnackbar('Your text was not flagged by AI detectors! β
', 'top');
}
});
const copyToClipboard = (str) => {
//send to AI plagiarism detector in background.js
chrome.runtime.sendMessage({ message: str });
navigator.clipboard.writeText(str).then(function () {
//console.log('Async: Copying to clipboard was successful!');
}, function (err) {
console.error('Async: Could not copy text: ', err);
});
//copy notification
showSnackbar('Copied to clipboard π');
};
document.addEventListener('copy', (event) => {
//get copied selection and sterilize
const selection = document.getSelection().toString();
//send to AI plagiarism detector in background.js
chrome.runtime.sendMessage({ message: selection });
//copy it to clipboard
event.clipboardData.setData('text/plain', selection);
//prevent default copy behavior, without this line it will not work!
event.preventDefault();
//copy notification
showSnackbar('Copied to clipboard π');
});
const intervalId = window.setInterval(async function () {
//GPT Credits Setter
if (!document.querySelector('#copygpt-credits')) {
var textCenterElement = document.querySelector('.text-center');
textCenterElement = textCenterElement ?? document.querySelector('.text-center.text-xs');
textCenterElement.insertAdjacentHTML('beforeend', ' β¨ <a class="underline" id="copygpt-credits" target="_blank" href="https://copygpt.sethusenthil.com/?ref=gptFooter"> Enhanced by CopyGPT</a>')
}
const chatContainer = document.querySelector('main');
//console.log('probing for new chat bubbles');
const chatbubbles = chatContainer.querySelectorAll('main.w-full .border-b');
//console.log('chatbubbles', chatbubbles);
//check if it is a plus user
var plusUser = (chatbubbles.length % 2 === 0) ? false : true;
chatbubbles.forEach((chatbox, i) => {
//console.log('chatbox', chatbox);
//first chat box needs to be from user, hence all the even chat bubbles are from bot
//plus users will have the first row as model selection
if ((i > 0 && (i % 2 === 0) && plusUser) ||
((i + 1) % 2 === 0 && !plusUser)) {
//it is a chat box from bot
const addAfter = chatbox.querySelector(CHAT_TEXT_SELECTOR).parentElement;
if (chatbox.querySelector(`.${CLIPBOARD_CLASS_NAME}`) === null) {
//TODO: to check if ChatGPT is done typing check .result-streaming class exists in chatbox
addAfter.insertAdjacentHTML('afterend', `<div style="display: flex; align-items: center; color: lightslategray;" class="copy-to-clipboard">
<p>Copy to Clipboard</p> <img src="https://copygpt.sethusenthil.com/cdn/clipboard-emoji.webp" alt="clipboard emoji" class="emoji"/>
</div>`);
chatbox.querySelector(`.${CLIPBOARD_CLASS_NAME}`).addEventListener('click', function () {
const text = chatbox.querySelector(CHAT_TEXT_SELECTOR).innerText;
copyToClipboard(text);
});
}
}
});
if (chatContainer.getAttribute('listener-injected') !== 'true') {
//console.log('setting event listener cause its not already there')
document.addEventListener('keydown', function (event) {
const chatContainer = document.querySelector('.flex .flex-col .items-center');
// check for CTRL+K on Windows or CMD+K on Mac
if ((event.ctrlKey || event.metaKey) && event.keyCode === 75) {
//console.log('Copy Shortcut Pressed');
const chatbubbles = chatContainer.querySelectorAll('main.w-full .border-b');
//check if it is a plus user
var plusUser = (chatbubbles.length % 2 === 0) ? false : true;
if (((chatbubbles.length % 2 === 0) && !plusUser) ||
((chatbubbles.length % 2 === 1) && plusUser)) {
//if last chat is from bot
const lastChatBubble = chatbubbles[chatbubbles.length - 1];
const text = lastChatBubble.querySelector(CHAT_TEXT_SELECTOR).innerText
copyToClipboard(text);
}
}
chatContainer.setAttribute('listener-injected', 'true');
});
const textarea = document.querySelector('textarea');
textarea.addEventListener('keydown', function (event) {
if (event.key === 'ArrowUp') {
if (chatbubbles.length > 0) {
let lastSetIndex = chatContainer.getAttribute('last-set-index') ?? 0;
//console.log('lastSetIndex', lastSetIndex);
if (lastSetIndex >= chatbubbles.length) {
lastSetIndex = 0;
}
lastSetIndex++;
const lastChatBubble = chatbubbles[chatbubbles.length - lastSetIndex];
const text = lastChatBubble.innerText
textarea.value = text;
chatContainer.setAttribute('last-set-index', lastSetIndex);
}
}
});
}
}, 1000);
})();