We have hera at school an older learning platform with low usability. I try to improve it by writing an add-on for me and my collegues. I got som stuff working but i am stuck trying to submit several forms att once. The task is the following: On the webpage is a list of all my students. There is one textarea for each one for assesment and a link “Save” to update and set the current date and time as timestamp for the text.
The link triggers a method which copies some parameters to a form and submits it. it is know a Asp.net Postback Mechanism(doPostBack function).
I would like to trigger that in a loop for all student so every assesment, even empty once, gets a new timestamp just to confirm and older assesment as still valid.
I got a functioning loop but only the first submit works. Thats dues to the fact that a subimt triggers a full request and it reload the page –> The loop stops.
Workaround: the script copies the form to an iframe the prevent reoload. Works, but the problem is now that only the last student in the loop is uppdated.
I guessed it was about timing, the requests are coming to fast for the server. So i slowed it down to 5sec wait between request but only the last student shows an updated timestamp.
I need another idea to fix this. Here is my code so far. count and if(count<3) i only for debugging purposes:
document.getElementById('saveAll').addEventListener('click', () => {
addText = document.getElementById('add2BoxText').value;
chrome.tabs.query({
active: true,
currentWindow: true
}, (tabs) => {
chrome.scripting.executeScript({
target: {
tabId: tabs[0].id
},
function: saveAllBoxes
});
});
});
function saveAllBoxes() {
var c = 0;
var theForm = document.forms['aspnetForm'];
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
const formClone = theForm.cloneNode(true);
iframe.contentDocument.body.appendChild(formClone);
//document.body.removeChild(theForm);
if (!formClone) {
formClone = document.aspnetForm;
}
async function __doPostBack(eventTarget, eventArgument) {
if (!formClone.onsubmit || (formClone.onsubmit() != false)) {
formClone.__EVENTTARGET.value = eventTarget;
formClone.__EVENTARGUMENT.value = eventArgument;
formClone.submit();
console.log(formClone.__EVENTTARGET.value);
c++;
}
}
count = 0;
var all_td_a = document.querySelectorAll('td a');
all_td_a.forEach((element) => {
if (element.text == 'Spara') {
element.text = "Saved";
fullLink = element.href;
var fullLink = fullLink.substring(fullLink.indexOf("'") + 1, fullLink.lastIndexOf("'") - 3);
//console.log(fullLink);
nu = new Date().valueOf() + 2500;
console.log('---',nu);
while (nu > new Date().valueOf()) {
// my kind of loop. bad! but i did not get SetTimeout to work.
}
console.log(new Date().valueOf());
if(count < 3) __doPostBack(fullLink, '');
count++;
}
});
}