document.addEventListener("DOMContentLoaded", function () {
// Configuration constants
const CONFIG = {
mappings: [
{ jsonKey: "qualityScore", qid: 34 },
{ jsonKey: "isFlagged", qid: 35 },
],
api: {
url: "https://surveys.getaftercare.com/api/v1/data-quality",
key: "", // Update to your API key
},
hiddenAnswerQID: 27, // Update to your answer field QID
retryDelay: 200,
buttonClickDelay: 300,
};
// Helper functions
const helpers = {
getElementId: (qid, section = "element") =>
`sgE-${SGAPI.survey.surveyObject.id}-${SGAPI.survey.pageId}-${qid}-${section}`,
getElement: (qid, section = "element") =>
document.getElementById(helpers.getElementId(qid, section)),
getHiddenInputValue: (qid) => {
const input = helpers.getElement(qid);
return input ? input.value : "";
},
hidePageContent: () => {
const content = document.querySelector(".sg-page-content");
if (content) content.style.display = "none";
},
prepopFields: (mappings, data) => {
mappings.forEach(({ jsonKey, qid }) => {
const value = data[jsonKey];
const element = helpers.getElement(qid);
if (element) element.value = value || "";
});
},
clickNextButton: () => {
const nextButton =
document.querySelector("#sg_NextButton") ||
document.querySelector("#sg_SubmitButton");
if (!nextButton) {
return setTimeout(helpers.clickNextButton, CONFIG.retryDelay);
}
nextButton.disabled = false;
nextButton.style.display = "block";
try {
// Try native click first
nextButton.click();
// Fallback to dispatching click event
setTimeout(() => {
const clickEvent = new MouseEvent("click", {
bubbles: true,
cancelable: true,
view: window,
});
nextButton.dispatchEvent(clickEvent);
}, CONFIG.buttonClickDelay);
} catch (error) {
console.error("Failed to click button:", error);
}
},
// Save data to localStorage
saveData: (data) => {
try {
localStorage.setItem(
"aftercareDataQualityResults",
JSON.stringify(data)
);
console.log("Data saved successfully to localStorage");
} catch (error) {
console.error("Failed to save data to localStorage:", error);
}
},
};
// Main function
const main = async () => {
try {
// Get the answer value
const answer = helpers.getHiddenInputValue(CONFIG.hiddenAnswerQID);
// Prepare the API payload
const payload = {
surveyName: "Response Quality Evaluation",
surveyEntries: [
{
question: "Question", // Replace this with the text of the question
answer: answer,
},
],
};
// Hide the page content while processing
helpers.hidePageContent();
try {
// Make the API request
const response = await fetch(CONFIG.api.url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Aftercare-Key": CONFIG.api.key,
},
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error(`API returned status ${response.status}`);
}
const data = await response.json();
// Save the API response data
helpers.saveData(data);
// Populate the form fields with API response
helpers.prepopFields(CONFIG.mappings, data);
// Proceed to next page
setTimeout(helpers.clickNextButton, CONFIG.retryDelay);
} catch (apiError) {
console.error("API request failed:", apiError);
setTimeout(helpers.clickNextButton, CONFIG.retryDelay * 2.5);
}
} catch (error) {
console.error("Main execution error:", error);
// Still try to proceed to next page on error
setTimeout(helpers.clickNextButton, CONFIG.retryDelay);
}
};
// Start execution
main();
});