I'm trying to write a greasemonkey script that automtically presses a button on a page, but only if a tab is disabled, the tab shows duplicates records and when I inspect the page in firefox using Web Devloper Tools/Inspector I find that when we have duplicates records and the tab is enabled that we have
<li class="ui-state-default ui-corner-top" role="tab" tabindex="-1" aria-controls="duplicates-tab" aria-labelledby="ui-id-2" aria-selected="false"><a href="#duplicates-tab" class="ui-tabs-anchor" role="presentation" tabindex="-1" id="ui-id-2">Release Duplicates</a></li>
but when it is disabled i have
<li class="ui-state-default ui-corner-top ui-state-disabled" role="tab" tabindex="-1" aria-controls="duplicates-tab" aria-labelledby="ui-id-2" aria-selected="false" aria-disabled="true"><a href="#duplicates-tab" class="ui-tabs-anchor" role="presentation" tabindex="-1" id="ui-id-2">Release Duplicates</a></li>
i.e when disabled the enclosing list element of the link has an additional ui-state-disabled class (this was the only difference I could find when i inspected the html)
So I write my greasemonkey script as follows
// ==UserScript==
// @name AutoContinueAddRelease
// @version
// @grant none
// @include https://musicbrainz.org/release/add
// ==/UserScript==
window.addEventListener ("load", Greasemonkey_main, false);
function Greasemonkey_main ()
{
var as = document.getElementsByTagName("a");
for(a of as)
{
if(a.href=='https://musicbrainz.org/release/add#duplicates-tab')
{
alert(a.parentElement.classList);
if(a.parentElement.classList.contains('ui-state-disabled'))
{
var button = document.getElementById("enter-edit");
if(button!=null)
{
button.click();
}
}
break;
}
}
}
But in both cases it finds the ui-state-disabled class and submits the form, which is not what i want, I dont understand why.
Solved the problem by setting timeout to delay running the script until a bit after page loaded based on @Ourobirus comment
// ==UserScript==
// @name AutoContinueAddRelease
// @version
// @grant none
// @include https://musicbrainz.org/release/add
// ==/UserScript==
setTimeout(check, 5000);
function check()
{
var as = document.getElementsByTagName("a");
for(a of as)
{
if(a.href=='https://musicbrainz.org/release/add#duplicates-tab')
{
if(a.parentElement.classList.contains('ui-state-disabled'))
{
var button = document.getElementById("enter-edit");
if(button!=null)
{
button.click();
}
}
else
{
alert('duplicatesFound');
}
break;
}
}
}