-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontentScript.js
More file actions
231 lines (195 loc) · 7.28 KB
/
Copy pathcontentScript.js
File metadata and controls
231 lines (195 loc) · 7.28 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
$(document).ready(function () {
console.log("--- Disassist Extension Loaded ---");
chrome.storage.sync.get(null, (items) => {
var styles = `
@font-face {
font-family: 'font-regular';
src: url('${chrome.runtime.getURL(
"fonts/regular.woff"
)}') format('woff');
}
.text-dialog {
position: absolute;
background-color: #333;
color: #fff;
border: 1px solid #555;
padding: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
z-index: 9999;
max-width: 500px;
}
`;
// Append style to the head of the current document
$("head").append("<style>" + styles + "</style>");
if (items?.font) {
$("body").attr(
"style",
"font-family: 'font-regular', sans-serif !important;"
);
}
chrome.storage.sync.onChanged.addListener(function (item) {
// Used to connect settings changes with page
});
});
});
chrome.runtime.onMessage.addListener(async function (
request,
sender,
sendResponse
) {
console.log(request);
if (request.action === "disassist") {
await disAssist();
}
});
async function disAssist() {
const selection = window.getSelection();
const selectedText = selection.toString().trim();
const noImage = selectedText.split(" ").length > 2;
const loading = document.createElement("div");
loading.className = "text-dialog";
// Calculate the position for the dialog
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
// Calculate the position relative to the entire document
const documentRect = document.body.getBoundingClientRect();
const dialogTop = rect.top - documentRect.top - loading.offsetHeight - 10;
loading.style.position = "absolute";
loading.style.top = dialogTop + "px";
loading.style.left = rect.left + rect.width / 2 - loading.offsetWidth + "px";
// Append the dialog to the body
loading.innerHTML = "Loading Smart Text...";
document.body.appendChild(loading);
// Manipulate the selected text
const text = await chrome.runtime.sendMessage({
action: "fetchGPT",
content: selectedText,
});
let image;
if (!noImage) {
loading.innerHTML = "Loading AI Image...";
image = await chrome.runtime.sendMessage({
action: "genImage",
content: selectedText,
});
}
document.body.removeChild(loading);
const manipulatedText = text.replace(/\*\*(.*?)\*\*/g, "<i><b>$1</b></i>");
// Create a div for the dialog
const dialog = document.createElement("div");
dialog.className = "text-dialog";
dialog.innerHTML =
manipulatedText +
"<br>" +
(noImage
? ""
: `<img src="${image}" alt="${selectedText}" width="256" height="256">`);
dialog.style.position = "absolute";
dialog.style.top = dialogTop + "px";
dialog.style.left = rect.left + rect.width / 2 - dialog.offsetWidth + "px";
// Append the dialog to the body
document.body.appendChild(dialog);
// Add a click event listener to remove the dialog on clicks outside of it
document.addEventListener("click", handleClickOutside);
// Function to handle click events
function handleClickOutside(event) {
if (!dialog.contains(event.target)) {
// Click outside the dialog, remove the dialog and remove the event listener
document.body.removeChild(dialog);
document.removeEventListener("click", handleClickOutside);
}
}
}
async function manipulateText() {
const selection = window.getSelection();
const selectedText = selection.toString().trim();
const loading = document.createElement("div");
loading.className = "text-dialog";
loading.innerHTML = "Loading Contents...";
// Calculate the position for the dialog
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
// Calculate the position relative to the entire document
const documentRect = document.body.getBoundingClientRect();
const dialogTop = rect.top - documentRect.top - loading.offsetHeight - 10;
loading.style.position = "absolute";
loading.style.top = dialogTop + "px";
loading.style.left = rect.left + rect.width / 2 - loading.offsetWidth + "px";
// Append the dialog to the body
document.body.appendChild(loading);
// Manipulate the selected text
const response = await chrome.runtime.sendMessage({
action: "fetchGPT",
content: selectedText,
});
document.body.removeChild(loading);
console.log(response);
const manipulatedText = response.replace(
/\*\*(.*?)\*\*/g,
"<strong>$1</strong>"
);
// Create a div for the dialog
const dialog = document.createElement("div");
dialog.className = "text-dialog";
dialog.innerHTML = "Manipulated Text: " + manipulatedText;
dialog.style.position = "absolute";
dialog.style.top = dialogTop + "px";
dialog.style.left = rect.left + rect.width / 2 - dialog.offsetWidth + "px";
// Append the dialog to the body
document.body.appendChild(dialog);
// Add a click event listener to remove the dialog on clicks outside of it
document.addEventListener("click", handleClickOutside);
// Function to handle click events
function handleClickOutside(event) {
if (!dialog.contains(event.target)) {
// Click outside the dialog, remove the dialog and remove the event listener
document.body.removeChild(dialog);
document.removeEventListener("click", handleClickOutside);
}
}
}
async function generateImage() {
const selection = window.getSelection();
const selectedText = selection.toString().trim();
const loading = document.createElement("div");
loading.className = "text-dialog";
loading.innerHTML = "Loading Image...";
// Calculate the position for the dialog
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
// Calculate the position relative to the entire document
const documentRect = document.body.getBoundingClientRect();
const dialogTop = rect.top - documentRect.top - loading.offsetHeight - 10;
loading.style.position = "absolute";
loading.style.top = dialogTop + "px";
loading.style.left = rect.left + rect.width / 2 - loading.offsetWidth + "px";
// Append the dialog to the body
document.body.appendChild(loading);
// Manipulate the selected text
const response = await chrome.runtime.sendMessage({
action: "genImage",
content: selectedText,
});
document.body.removeChild(loading);
console.log(response);
const image = response;
// Create a div for the dialog
const dialog = document.createElement("div");
dialog.className = "text-dialog";
dialog.innerHTML = `<img src="${image}" alt="${selectedText}" width="256" height="256">`;
dialog.style.position = "absolute";
dialog.style.top = dialogTop + "px";
dialog.style.left = rect.left + rect.width / 2 - dialog.offsetWidth + "px";
// Append the dialog to the body
document.body.appendChild(dialog);
// Add a click event listener to remove the dialog on clicks outside of it
document.addEventListener("click", handleClickOutside);
// Function to handle click events
function handleClickOutside(event) {
if (!dialog.contains(event.target)) {
// Click outside the dialog, remove the dialog and remove the event listener
document.body.removeChild(dialog);
document.removeEventListener("click", handleClickOutside);
}
}
}