-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
335 lines (273 loc) · 13.8 KB
/
popup.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
async function initialize() {
// console.log(localStorage);
// if (Number(localStorage.getItem("master")) != 1) {
// localStorage.setItem("saturation", 10);
// }
for (let mode of modes) {
let setting = Number(localStorage.getItem(mode));
let val = (setting != null && setting != "null" && setting != "0" && setting != 0);
localStorage.setItem(mode, (val) ? setting : 0)
if (val) toggleMode(mode, true);
}
let saturation = Number(localStorage.getItem("saturation"));
let contrast = Number(localStorage.getItem("contrast"));
if ((Number(localStorage.getItem("master")) != 1) || saturation == "null" || saturation == null) saturation = 10;
if ((Number(localStorage.getItem("master")) != 1) || contrast == "null" || contrast == null) contrast = 10;
localStorage.setItem("saturation", saturation);
localStorage.setItem("contrast", contrast);
await setStyles();
await setAltText();
}
async function setStyles() {
chrome.tabs.query({ active: true, currentWindow: true }, async function (tabs) {
var currentTab = tabs[0];
var tabId = currentTab.id;
const states = await getStates();
const result = await getStyles(states);
// console.log("Result:", result);
if (!chrome.runtime.lastError) {
await chrome.scripting.insertCSS({
target: { tabId },
css: result
});
if (states.colourblind) await styleColour();
} else {
console.error('Error executing script:', chrome.runtime.lastError);
}
});
}
async function getStates() {
let states;
if (Number(localStorage.getItem("master")) != 1) {
states = {"dark": 0, "night": 0, "saturation": 1, "contrast": 1, "colourblind": 0};
} else {
states = {
"dark": Number(localStorage.getItem("darkMode")),
"night": Number(localStorage.getItem("nightMode")),
"saturation": Number(localStorage.getItem("saturation"))/10, // 0 to 2 (default is 1)
"contrast": Number(localStorage.getItem("contrast"))/10, // 0 to 2 (default is 1)
"colourblind": Number(localStorage.getItem("colourblind")) // red
}
}
// console.log(states);
return states;
}
async function getStyles(states) {
let css = [];
css = [
`html { filter: invert(${states.dark}) hue-rotate(${180*states.dark}deg) saturate(${100*states.saturation}%) contrast(${100*states.contrast}%) !important; }`,
`img, picture, video { filter: invert(${states.dark}) hue-rotate(${180*states.dark}deg) saturate(100%) contrast(100%) !important; }`,
];
if (states.night) css.push("html::before { display: block; background: #ffffbcbf; mix-blend-mode: multiply; content: ' '; position: fixed; top: 0; left: 0; width: 100%; height: 100vh; pointer-events: none; z-index: 999; }");
else css.push("html::before { display: none; }");
let res = css.join(' ')
return res;
}
async function styleColour() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var currentTab = tabs[0];
var tabId = currentTab.id;
chrome.scripting.executeScript({
target: { tabId },
files: [ "stylePage.js" ]
});
});
}
let altTexted = false;
async function setAltText() {
let enableAltText = Number(localStorage.getItem("enableAltText"));
let displayAltText = Number(localStorage.getItem("displayAltText"));
if (enableAltText != "null" && enableAltText != null && enableAltText != 0) {
if (!altTexted) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: async () => {
// Array of all images on page
const images = Array.from(document.querySelectorAll("img"))
// console.log(images)
// Iterate through all images, make alt text
for (const image in images) {
const imageSelector = images[image]
// console.log(imageSelector)
// console.log(imageSelector.src)
// setTimeout(function() {
// asticaVision("2.0_full", image.src, 'Description', asticaCallback); //with options:
// }, 2000);
// Astica API endpoint
const apiUrl = 'https://vision.astica.ai/describe';
const apiKey = 'INSERT YOUR API KEY HERE!!';
const modelVersion = '2.0_full';
// Data to send to API
const imageUrl = images[image].src;
const visionParams = 'description';
// JSON payload
const payload = {
tkn: apiKey,
modelVersion: modelVersion,
visionParams: visionParams,
input: imageUrl,
};
if (imageSelector.alt == "") {
// Send a POST request to the Astica API
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
.then((response) => response.json())
.then((data) => {
// Log the API response in the console
// console.log(data)
// console.log(data["caption"]["text"]);
imageSelector.alt = data["caption"]["text"]
})
.catch((error) => {
// Handle any errors
console.error('Error:', error);
});
}
}
}
});
})
}
altTexted = true
}
if (displayAltText != "null" && displayAltText != null && displayAltText != 0 && enableAltText != "null" && enableAltText != null && enableAltText != 0) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: async () => {
// Array of all images on page
const images = Array.from(document.querySelectorAll("img"))
// console.log(images)
images.forEach((image) => {
// Create a new paragraph element
const altTextParagraph = document.createElement("p");
altTextParagraph.classList.add("altElement")
altTextParagraph.textContent = image.alt;
image.insertAdjacentElement("afterend", altTextParagraph);
});
}
})
});
} else if (displayAltText == "null") {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: async () => {
document.querySelectorAll('.altElement').forEach(e => e.remove());
}
})
});
}
}
// JS For Frontend
const modes = ["master", "darkMode", "nightMode", "enableAltText", "displayAltText", "colourblind"];
const toggles = {"c1": "off", "c2": "on"};
const body = document.querySelector('body');
// below is testing auto-enable extension when switching tabs... only does it for the first 2 tabs tho
// chrome.tabs.onActivated.addListener(simulateInitialize);
// async function simulateInitialize() {
// await initialize();
// await chrome.tabs.removeEventListener(simulateInitialize);
// await chrome.tabs.addListener(simulateInitialize);
// await chrome.tabs.reload();
// await initialize();
// }
async function inaccessible() {
localStorage.setItem("master", 0);
document.querySelector(".switch-box").style.cursor = "default";
document.querySelectorAll(".slider-bar").forEach((slider) => { slider.disabled = true; });
return console.log("Cannot run on chrome:// tabs")
}
chrome.tabs.onUpdated.addListener(async function (tabId, info) { // for reloading colourblind mode
await chrome.tabs.query({ active: true, currentWindow: true }, async function (tabs) {
if (tabs[0].url == undefined) return;
if (tabs[0].url.startsWith("chrome://")) return inaccessible();
await initialize();
});
});
document.addEventListener('DOMContentLoaded', async function () {
await chrome.tabs.query({ active: true, currentWindow: true }, async function (tabs) {
if (tabs[0].url == undefined) return;
if (tabs[0].url.startsWith("chrome://")) return inaccessible();
document.querySelectorAll(".switch-box").forEach((toggle, i) => {
toggle.addEventListener('click', async () => {
if ((Number(localStorage.getItem("master")) == "null" || Number(localStorage.getItem("master")) == null || Number(localStorage.getItem("master")) == 0) && (modes[i] != "master")) return;
await toggleMode(modes[i], !(body.classList.contains(modes[i])));
if (modes[i] == "colourblind" && Number(localStorage.getItem("colourblind")) != 1.0) {
await chrome.tabs.reload().then(async () => {
// chrome.tabs.onUpdated.addListener(simulateReload);
// async function simulateReload() {
// await initialize();
// // chrome.tabs.onUpdated.removeListener(simulateReload);
// }
})
// document.dispatchEvent(new Event("DOMContentLoaded"));
// if (Number(localStorage.getItem("master")) != 1) document.querySelectorAll(".slider-bar").forEach((slider) => { slider.disabled = true; });
// else document.querySelectorAll(".slider-bar").forEach((slider) => { slider.disabled = false; });
// await initialize();
// for (let mode of modes) {
// let setting = Number(localStorage.getItem(mode));
// let val = (setting != null && setting != "null" && setting != "0" && setting != 0);
// if (val) toggleMode(mode, true);
// }
}
await setStyles();
await setAltText();
});
});
if (Number(localStorage.getItem("master")) != 1) document.querySelectorAll(".slider-bar").forEach((slider) => { slider.disabled = true; });
else document.querySelectorAll(".slider-bar").forEach((slider) => { slider.disabled = false; });
await initialize();
for (let mode of modes) {
let setting = Number(localStorage.getItem(mode));
let val = (setting != null && setting != "null" && setting != "0" && setting != 0);
if (val) toggleMode(mode, true);
}
let saturation = localStorage.getItem("saturation");
let contrast = localStorage.getItem("contrast");
var saturation_output = document.getElementById("saturation");
saturation_output.innerHTML = `${saturation*10}%`;
var saturation_slider = document.getElementById("saturation_slider");
saturation_slider.value = saturation;
saturation_slider.addEventListener('input', function() {
if (Number(localStorage.getItem("master")) != 1) return;
saturation_output.innerHTML = `${saturation_slider.value*10}%`;
localStorage.setItem("saturation", saturation_slider.value);
setStyles();
});
var contrast_output = document.getElementById("contrast");
contrast_output.innerHTML = `${contrast*10}%`;
var contrast_slider = document.getElementById("contrast_slider");
contrast_slider.value = contrast;
contrast_slider.addEventListener('input', function() {
if (Number(localStorage.getItem("master")) != 1) return;
contrast_output.innerHTML = `${contrast_slider.value*10}%`;
localStorage.setItem("contrast", contrast_slider.value);
setStyles();
});
});
});
async function toggleMode(mode, val) {
if (val) body.classList.add(mode);
else body.classList.remove(mode);
localStorage.setItem(mode, (val) ? 1 : 0);
if (mode == "master") {
if (val == false) {
for (const mode of modes.slice(1)) {
toggleMode(mode, false);
}
document.querySelectorAll(".slider-bar").forEach((slider) => { slider.disabled = true; });
} else {
document.querySelectorAll(".slider-bar").forEach((slider) => { slider.disabled = false; });
}
} /* else if (mode == "enableAltText" && val == false) {
// toggleMode("displayAltText", false);
}*/
}
// Add click listeners