-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasyButtonCode.js
More file actions
67 lines (63 loc) · 2.65 KB
/
Copy pathEasyButtonCode.js
File metadata and controls
67 lines (63 loc) · 2.65 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
Qualtrics.SurveyEngine.addOnload(function () {
// console.log("logging new save");
var questionContainer = this.getQuestionContainer();
// Setting Anchor tags (links) to redirect to new tab
var anchorTags = questionContainer.querySelectorAll('a');
anchorTags.forEach(function(anchorTag) {
anchorTag.setAttribute('target', '_blank');
});
// Reset the button onClick to deep-link from csv
var applyButtons = questionContainer.querySelectorAll("#deeplink");
applyButtons.forEach(function(button) {
const buttonText = button.textContent.trim();
// console.log(buttonText);
fetch("https://newdev.sjsu.edu/education/easybutton/test.csv")
.then(response => {
if (!response.ok) {
throw new Error("Unable to connect to test.csv ensure that test.csv is located at https://newdev.sjsu.edu/education/easybutton/test.csv");
}
return response.text();
}).then(data => {
// console.log("CSV Data:", data); // Debug: Log the CSV data
return data.split(/[,\n]/);
})
.then(data => {
for (var i = 0; i < data.length; i = i + 3) {
// console.log(data[i].trim());
if (data[i].trim() === buttonText) { // Compare button text with data[i]
var dateString = data[i + 2];
var parts = dateString.split('/');
var formattedDate = new Date(parts[2], parts[0] - 1, parts[1]).getTime();
var currentDate = Date.now() ;
if (formattedDate > currentDate) {
//console.log("DateString:" ,dateString);
//console.log("ButtonText:", buttonText);
const newContent = buttonText + ": Opens " + dateString;
//console.log(newContent);
button.textContent = newContent;
} else {
console.log(data[i+1]);
button.onclick = () => {
window.open(data[i + 1], '_blank');
}
}
//console.log(currentDate);
//console.log(formattedDate);
return;
}
}
throw new Error("Button text not mapped to endpoint in test.csv");
})
.catch(error => {
const message = "EasyButton Error:" + error.message
console.log(message);
button.onclick = () => {
window.open("https://www.calstate.edu/apply", '_blank');
}
});
});
});
Qualtrics.SurveyEngine.addOnReady(function()
{
this.hideNextButton();
});