-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscores.js
114 lines (105 loc) · 5.08 KB
/
scores.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
async function show_scoresfunc() {
function detectScrolling(colors, dataRowIds) {
const filterlistviewContainer = document.querySelector('.filterlistview-container');
if (!filterlistviewContainer) return;
filterlistviewContainer.addEventListener('scroll', function () {
var isVerticalScrolling = filterlistviewContainer.scrollTop !== 0;
var isHorizontalScrolling = filterlistviewContainer.scrollLeft !== 0;
if (isVerticalScrolling || isHorizontalScrolling) {
const validColors = colors.filter(color => color !== null);
extractDataRowIdsAndAssignColors(validColors, dataRowIds);
}
});
}
async function fetchEvaluationDetails(identifier) {
try {
var currentUrl = window.location.href;
school_name = currentUrl.split("/")[2]
const url = `https://${school_name}/results/api/v1/evaluations/${identifier}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch evaluation details for identifier: ${identifier}`);
}
const data = await response.json();
return data.details.projectGoals[0]?.graphic?.color || null;
} catch (error) {
console.error(`Error fetching evaluation details for identifier ${identifier}:`, error);
return null;
}
}
function extractDataRowIdsAndAssignColors(colors, dataRowIds) {
const elementsWithDataLevelZero = document.querySelectorAll('[data-level="0"]');
elementsWithDataLevelZero.forEach((element, index) => {
const dataRowId = dataRowIds[index]; // Retrieve data-rowid from the array
const color = colors[index] || 'default-color'; // Use default color if not enough colors fetched
const button = element.querySelector('button'); // Find the button element inside the row
if (button) {
// Assign color to the button background
switch (color) {
case 'blue':
button.style.setProperty('background-color', 'var(--color-blue)', 'important');
break;
case 'green':
button.style.setProperty('background-color', 'var(--color-green)', 'important');
break;
case 'orange':
button.style.setProperty('background-color', 'var(--color-orange)', 'important');
break;
case 'red':
button.style.setProperty('background-color', 'var(--color-red)', 'important');
break;
default:
console.error(`Invalid color: ${color}`);
button.style.setProperty('background-color', color, 'important');
break;
}
}
});
}
function extractIdentifiersFromHTML(html) {
const identifierRegex = /"identifier"\s*:\s*"([^"]+)"/g;
const identifiers = [];
let match;
while ((match = identifierRegex.exec(html)) !== null) {
identifiers.push(match[1]);
}
return identifiers;
}
function extractDataRowIds() {
const listviewRows = document.querySelector('.listview__rows');
const dataRowIds = [];
if (listviewRows) {
const elementsWithDataLevelZero = listviewRows.querySelectorAll('[data-level="0"]');
elementsWithDataLevelZero.forEach(element => {
const dataRowId = element.dataset.rowid;
dataRowIds.push(dataRowId);
});
}
return dataRowIds;
}
try {
var currentUrl = window.location.href;
school_name = currentUrl.split("/")[2]
const response = await fetch(`https://${school_name}/results/api/v1/evaluations/?pageNumber=1&itemsOnPage=100`);
if (!response.ok) {
throw new Error('Failed to fetch HTML content');
}
const html = await response.text();
const identifiers = extractIdentifiersFromHTML(html);
const projectIdentifiers = identifiers.filter(identifier => identifier.includes('_project_'));
const fetchPromises = projectIdentifiers.map(identifier => fetchEvaluationDetails(identifier));
const colors = await Promise.all(fetchPromises);
const validColors = colors.filter(color => color !== null);
const dataRowIds = extractDataRowIds();
extractDataRowIdsAndAssignColors(validColors, dataRowIds);
detectScrolling(validColors, dataRowIds);
// Additionally, observe DOM changes to detect dynamically loaded content
const observer = new MutationObserver(() => {
// When new content is added to the DOM, reapply color updates
extractDataRowIdsAndAssignColors(validColors, dataRowIds);
});
observer.observe(document.body, { childList: true, subtree: true });
} catch (error) {
console.error('Error:', error);
}
}