-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
310 lines (253 loc) · 10.8 KB
/
Copy pathcontent.js
File metadata and controls
310 lines (253 loc) · 10.8 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
let clickableElements = [];
let currentStepIndex = 0; // Default to 0
function traverseShadowDOM(node, callback) {
if (node.shadowRoot) {
node.shadowRoot.childNodes.forEach((child) => traverseShadowDOM(child, callback));
}
callback(node);
}
// Function to check if an element's innerHTML contains any clickable element
function containsClickableElement(element) {
return clickableElements.some((clickable) =>
element.innerHTML === clickable || element.title.includes(clickable)
);
}
function removeHighlights() {
document.querySelectorAll("button, span, h4").forEach((element) => {
traverseShadowDOM(element, (node) => {
if (containsClickableElement(node) && node.style.border === "2px solid red") {
node.style.removeProperty("border");
}
});
});
}
// Listen for storage changes
chrome.storage.onChanged.addListener((changes, namespace) => {
if (clickableElements.length > 0) return;
if (changes.stepInstructions) {
const stepInstructions = changes.stepInstructions.newValue;
console.log("Updated instructions received:", stepInstructions);
updateUI(stepInstructions);
}
if (changes.stepInstructions) {
console.log("stepInstructions updated, refreshing content...");
loadInstructions(); // ✅ Update content immediately when storage changes
}
});
// Listen for messages
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "updateInstructions") {
removeHighlights();
const stepInstructions = message.stepInstructions;
console.log("Instructions received via message:", stepInstructions);
chrome.storage.local.set({ "stepInstructions": stepInstructions }, () => {
console.log("Instructions saved to storage.");
});
}
});
// Initial load
chrome.storage.local.get(["clickableElements", "stepInstructions"], (data) => {
if (data.clickableElements) {
clickableElements = data.clickableElements;
console.log("Initial clickableElements loaded:", clickableElements);
highlightButtonsAndSpans();
}
// if (data.stepInstructions) {
// stepsInstructions = data.stepInstructions;
// console.log("Loaded stepInstructions:", stepsInstructions);
// if (stepsInstructions.Instructions && Array.isArray(stepsInstructions.Instructions) && stepsInstructions.Instructions.length > 0) {
// stepsDiv.innerHTML = stepsInstructions.Instructions[0].content;
// } else {
// console.log("Instructions array is missing or empty.");
// stepsDiv.innerHTML = "<p>No instructions available</p>";
// }
// } else {
// console.log("No stepInstructions found in storage.");
// stepsDiv.innerHTML = "<p>No instructions available</p>";
// }
});
// Function to check if an element's innerHTML contains any clickable element
function containsClickableElement(element) {
return clickableElements.find((clickable) =>
element.innerHTML.trim() === clickable || element.title.includes(clickable)
);
}
function removeHighlightsOnClick(element, clickable) {
element.style.removeProperty("border");
console.log("Border removed from clicked element");
// Remove from memory first
clickableElements = clickableElements.filter(
(click) => click !== clickable
);
console.log("Updated clickableElements:", clickableElements);
// Update UI immediately
highlightButtonsAndSpans();
// Save updated list back to storage
chrome.storage.local.set({ "clickableElements": clickableElements }, () => {
console.log("Updated clickableElements saved to storage.");
});
}
// function highlightShadowElements() {
// traverseShadowDOM(document.body, (element) => {
// const clickable = containsClickableElement(element);
// if (clickable) {
// element.style.setProperty("border", "2px solid red", "important");
// console.log("Shadow DOM Border added to:", clickable, element);
// element.addEventListener("click", () => removeHighlightsOnClick(element, clickable));
// }
// });
// }
function highlightButtonsAndSpans() {
document.querySelectorAll("button, span, h4").forEach((element) => {
if (containsClickableElement(element)) {
element.style.setProperty("border", "2px solid red", "important");
const clickable = containsClickableElement(element);
console.log("Border added to element:", clickable);
element.addEventListener("click", () => removeHighlightsOnClick(element, clickable));
}
});
}
function updateUI(stepInstructions) {
console.log("Updating UI with instructions:", stepInstructions);
const regex = /\((.*?)\)/g;
stepInstructions.Instructions.forEach((instruction) => {
const content = instruction.content;
let match;
while ((match = regex.exec(content)) !== null) {
clickableElements.push(match[1]);
}
});
clickableElements = [...new Set(clickableElements)]; // Remove duplicates
console.log("Updated clickable elements:", clickableElements);
chrome.storage.local.set({ "clickableElements": clickableElements }, () => {
console.log("clickableElements saved to storage.");
highlightButtonsAndSpans(); // Ensure UI updates after storage update
});
// MutationObserver to reapply styles dynamically
const observer = new MutationObserver((mutationsList) => {
let shouldHighlight = false;
mutationsList.forEach((mutation) => {
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
shouldHighlight = true;
}
});
if (shouldHighlight) {
highlightButtonsAndSpans();
}
});
observer.observe(document.body, { childList: true, subtree: true });
// Initial run to highlight existing elements
highlightButtonsAndSpans();
}
const persistentPopupdiv = document.createElement('div');
persistentPopupdiv.id = 'persistent-popup';
persistentPopupdiv.style.position = 'fixed';
persistentPopupdiv.style.bottom = '60px';
persistentPopupdiv.style.width = '25px';
persistentPopupdiv.style.height = '25px';
persistentPopupdiv.style.right = '20px';
persistentPopupdiv.style.padding = '4px';
persistentPopupdiv.style.borderRadius = '50%';
persistentPopupdiv.style.backgroundColor = '#007bff';
persistentPopupdiv.style.cursor = 'pointer';
document.body.appendChild(persistentPopupdiv);
const stepsDiv = document.createElement('div');
stepsDiv.id = 'steps';
stepsDiv.style.position = 'fixed';
stepsDiv.style.bottom = '120px';
stepsDiv.style.right = '20px';
stepsDiv.style.padding = '8px';
stepsDiv.style.borderRadius = '5px';
stepsDiv.style.backgroundColor = '#fff';
stepsDiv.style.border = '1px solid #ccc';
stepsDiv.style.boxShadow = '0 2px 5px rgba(0, 0, 0, 0.2)';
stepsDiv.style.zIndex = '1000';
// ✅ Separate div for dynamic content
const contentDiv = document.createElement('div');
contentDiv.id = 'steps-content';
contentDiv.innerHTML = "<p>Loading instructions...</p>"; // Default text
const navigationNextButton = document.createElement('button');
navigationNextButton.id = 'navigation-next';
navigationNextButton.innerHTML = `Next`;
const navigationPrevButton = document.createElement('button');
navigationPrevButton.id = 'navigation-previous';
navigationPrevButton.innerHTML = `Previous`;
const navDiv = document.createElement('div');
navDiv.style.display = 'flex';
navDiv.style.marginTop = '8px';
navDiv.style.justifyContent = 'space-around';
navDiv.appendChild(navigationPrevButton);
navDiv.appendChild(navigationNextButton);
stepsDiv.appendChild(contentDiv);
stepsDiv.appendChild(navDiv);
document.body.appendChild(stepsDiv);
// 🔹 Function to Load Instructions
function loadInstructions() {
chrome.storage.local.get(["stepInstructions", "currentStepIndex"], (data) => {
console.log("Retrieved data from storage:", data);
if (data.currentStepIndex !== undefined) {
currentStepIndex = data.currentStepIndex; // Restore last viewed step
}
if (data.stepInstructions) {
let stepsInstructions = data.stepInstructions;
if (stepsInstructions.Instructions && stepsInstructions.Instructions.length > 0) {
contentDiv.innerHTML = stepsInstructions.Instructions[currentStepIndex]?.content || "<p>No instructions available</p>";
navDiv.style.display = 'flex';
} else {
contentDiv.innerHTML = "<p>No instructions available</p>";
navDiv.style.display = 'none';
}
} else {
contentDiv.innerHTML = "<p>No instructions available</p>";
navDiv.style.display = 'none';
}
});
}
// 🔹 Load Instructions Initially
loadInstructions();
navigationNextButton.addEventListener("click", () => {
chrome.storage.local.get(["stepInstructions"], (data) => {
if (data.stepInstructions && data.stepInstructions.Instructions) {
if (currentStepIndex === 0) {
navigationNextButton.innerHTML = 'Start';
}
if (currentStepIndex === data.stepInstructions.Instructions.length - 1) {
navigationNextButton.innerHTML = "Finish";
navigationNextButton.addEventListener("click", () => {
chrome.storage.local.remove(["stepInstructions", "currentStepIndex", "clickableElements"], () => {
stepsDiv.style.display = 'none';
})
})
} else {
navigationNextButton.innerHTML = 'Next';
currentStepIndex++; // Move to next step
updateStepContent();
chrome.storage.local.set({ currentStepIndex }); // Save index
}
togglePrevButtonVisibility(); // Update visibility
}
});
});
function togglePrevButtonVisibility() {
navigationPrevButton.style.display = currentStepIndex === 0 ? 'none' : 'block';
}
navigationPrevButton.addEventListener("click", () => {
if (currentStepIndex > 0) {
currentStepIndex--; // Move to previous step
updateStepContent();
chrome.storage.local.set({ currentStepIndex }); // Save index
}
togglePrevButtonVisibility(); // Update visibility
});
togglePrevButtonVisibility();
function updateStepContent() {
chrome.storage.local.get(["stepInstructions"], (data) => {
if (data.stepInstructions && data.stepInstructions.Instructions) {
contentDiv.innerHTML = data.stepInstructions.Instructions[currentStepIndex]?.content || "<p>No instructions available</p>";
}
});
}
// Toggle visibility on click
persistentPopupdiv.addEventListener('click', () => {
stepsDiv.style.display = stepsDiv.style.display === 'none' ? 'block' : 'none';
});