-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
222 lines (187 loc) · 6.67 KB
/
background.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
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
// Guardar un valor en chrome.storage.local
function saveToLocalStorage(key, value) {
let obj = {};
obj[key] = value;
chrome.storage.local.set(obj, function () {});
}
// Recuperar un valor de chrome.storage.local
function getFromLocalStorage(key) {
return new Promise((resolve, reject) => {
chrome.storage.local.get([key], function (result) {
resolve(result[key]);
});
});
}
function clearAllStorage() {
chrome.storage.local.clear(function () {
var error = chrome.runtime.lastError;
if (error) {
console.error(error);
}
});
}
const generateSessionID = () => {
return (
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15)
);
};
const sessionID = generateSessionID();
const key = "data-" + sessionID;
clearAllStorage();
chrome.webRequest.onCompleted.addListener(
async function (details) {
// if (!details.url.includes("data.js")) {
// return;
// }
// if (restrictedFiles.includes(details.url)) {
// return;
// }
// if end with restrictedFiles return
try {
const isActived = await getFromLocalStorage(details.url);
if (isActived) {
return;
}
saveToLocalStorage(details.url, true);
const data = await fetch(details.url).then((response) => response.text());
if (!data.includes("window.globalProvideData('data'")) {
return;
}
let dataJSON = data.match(/window.globalProvideData\('data', '(.*)'\);/);
dataJSON = dataJSON[1];
const cleaned = dataJSON.replace(/\\'/g, "'").replace(/\\\"/g, '"');
const dataParsed = JSON.parse(cleaned);
console.log(dataParsed);
var answersID = [];
// if dataParsed.slideBank is empty Object
if (Object.keys(dataParsed.slideBank).length === 0) {
console.log("No data");
console.log("Formulario no encontrado");
const scenes = dataParsed.scenes;
scenes.forEach((scene) => {
const slides = scene.slides;
slides.forEach((slide) => {
if (!slide) {
return;
}
if (!slide.interactions) {
return;
}
slide.interactions.forEach((interaction) => {
if (interaction.answers) {
interaction.answers.forEach((answer) => {
if (answer.status === "correct") {
// get ID
const statement = answer.evaluate.statements[0];
const choiceId = statement.choiceid;
const answerId = choiceId.split("_")[1];
answersID.push(answerId);
}
});
}
});
});
});
console.log(answersID);
setInterval(function () {
chrome.scripting.executeScript({
target: { tabId: details.tabId, allFrames: true },
func: (answersID) => {
// Aquí puedes escribir el código para modificar el HTML dentro del iframe
// const iframes = document.getElementsByTagName("iframe");
// for (let iframe of iframes) {
// try {
// const doc =
// iframe.contentDocument || iframe.contentWindow.document;
// let styleContent = ``;
// answersID.forEach((answerID) => {
// styleContent += `
// div[data-model-id="${answerID}"] {
// box-shadow: red 0px 54px 55px, red 0px -12px 30px, red 0px 4px 6px, red 0px 12px 13px, red 0px -3px 5px;
// }
// `;
// });
// const style = doc.createElement("style");
// style.textContent = styleContent;
// doc.head.appendChild(style);
// } catch (e) {
// console.error("No se pudo acceder al iframe:", e);
// }
// }
// not frame to implement style
let styleContent = ``;
try {
answersID.forEach((answerID) => {
styleContent += `
div[data-model-id="${answerID}"] {
box-shadow: red 0px 54px 55px, red 0px -12px 30px, red 0px 4px 6px, red 0px 12px 13px, red 0px -3px 5px;
}
`;
});
const style = document.createElement("style");
style.textContent = styleContent;
document.head.appendChild(style);
} catch (e) {
console.error("No se pudo acceder al iframe:", e);
}
},
args: [answersID],
});
}, 1000);
return;
}
const slideBank = dataParsed.slideBank;
const slides = slideBank.slides;
slides.forEach((slide) => {
slide.interactions.forEach((interaction) => {
if (interaction.answers) {
interaction.answers.forEach((answer) => {
if (answer.status === "correct") {
// get ID
const statement = answer.evaluate.statements[0];
const choiceId = statement.choiceid;
const answerId = choiceId.split("_")[1];
answersID.push(answerId);
}
});
}
});
});
console.log(answersID);
setInterval(function () {
chrome.scripting.executeScript({
target: { tabId: details.tabId, allFrames: true },
func: (answersID) => {
// Aquí puedes escribir el código para modificar el HTML dentro del iframe
const iframes = document.getElementsByTagName("iframe");
for (let iframe of iframes) {
try {
const doc =
iframe.contentDocument || iframe.contentWindow.document;
let styleContent = ``;
answersID.forEach((answerID) => {
styleContent += `
div[data-model-id="${answerID}"] {
box-shadow: red 0px 54px 55px, red 0px -12px 30px, red 0px 4px 6px, red 0px 12px 13px, red 0px -3px 5px;
}
`;
});
const style = doc.createElement("style");
style.textContent = styleContent;
doc.head.appendChild(style);
} catch (e) {
console.error("No se pudo acceder al iframe:", e);
}
}
},
args: [answersID],
});
}, 1000);
} catch (ex) {
console.error(ex);
}
// Save data in local storage
},
{ urls: ["<all_urls>"] }
);