Given a set of webpages open in a browser all from the same website each with a submit button, is it possible to write something that would go to each tab in turn and click on the submit button?
I am wondering if something like that could be done with a userscript manager, like Greasemonkey or Tampermonkey.
Or, alternatively, is there some way that when the webpage is opened instead of just opening the page it could open it, wait for it to load, and then press the submit button?
The webpage is opened via a form, not with window.open():
<form target="_blank" action="https://acoustid.org/edit/toggle-track-mbid" method="POST">
<input name="state" value="1" hidden="">
<input name="track_id" value="9509889" hidden="">
<input name="mbid" value="6573f01d-0df5-442d-90c3-a69783c217c3" hidden="">
<input type="submit" value="Unlink">
</form>
Here's the website. Click Unlink to open a new window and view the form (you need an account):
Its HTML is:
<form method="POST" action="/edit/toggle-track-mbid">
<div class="form-group">
<label for="note_i">Comment:</label>
<textarea class="form-control" name="note" id="name_i"></textarea>
</div>
<input type="hidden" name="mbid" value="b3350a3b-b8e0-45c0-8289-006451788849">
<input type="hidden" name="track_id" value="12211538">
<input type="hidden" name="state" value="1">
<input type="hidden" name="submit" value="1">
<input type="submit" class="btn btn-default" value="Submit">
</form>
The form can be submitted without actually opening another one in a new tab. The difference between the Unlink and Submit forms is that the latter has two new values: submit set to 1 (that is, unlink when form submitted) and note, a comment to explain the unlinking.
Since the question was posted, though, you've added the submit=1 parameter to the first form, so that a track can be directly unlinked by clicking the Unlink button.
Here's a solution that automates the process a bit:
// ==UserScript==
// @name Submit all forms
// @version 0.1
// @description see https://stackoverflow.com/questions/70227065
// @author double-beep
// @match http://reports.albunack.net/*.html
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
async function submitAllForms() {
const forms = document.querySelectorAll('table > tbody > tr > :nth-child(2) > form');
const formPromises = [...forms].map(form => {
const formData = new FormData(form);
if (!formData.get('submit')) { // old version of the website
formData.set('submit', '1');
}
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: 'POST', // post
url: form.action,
data: formData,
onload: resolve,
onerror: reject
});
});
});
await Promise.all(formPromises);
}
const submitButton = document.createElement('input');
submitButton.value = 'Submit forms';
submitButton.type = 'submit';
submitButton.addEventListener('click', async () => {
submitButton.disabled = true;
submitButton.innerText = 'Please wait...';
await submitAllForms();
submitButton.disabled = false;
submitButton.value = 'Re-submit forms';
});
document
.querySelector('table')
.previousElementSibling
.insertAdjacentElement('beforebegin', submitButton);
});
Only problem is - track_ids don't match and there's nothing the userscript can do about it.