Greasemonkey - Auto select value from dropdown menu on page load

Scorpion Ghost

Elite Member
Executive VIP
Jr. VIP
Joined
Mar 22, 2013
Messages
9,144
Reaction score
10,489
I want to auto-select a value from a dropdown menu on pageload using Greasemonkey.

Here is the code of the dropdown menu:

HTML:
 <div class="col-sm-9 col-md-9 col-lg-10">
<select name="type" id="type" "="" style="display: none;">
<option value="0"></option>
<option value="something1">Something 1</option>
<option value="something2">Something 2</option>
<option value="something3">Something 3</option>
<option value="something4">Something 4</option>
<option value="something5">Something 5</option>
<option value="something6">Something 6</option>
<option value="something7">Something 7</option>
<option value="something8">Something 8</option>
</select>

<div class="chosen-container chosen-container-single chosen-container-single-nosearch chosen-with-drop chosen-container-active" style="width: 182px;" title="" id="type_chosen">
<a class="chosen-single chosen-default" tabindex="-1">
<span>Faites votre selection</span>
<div>
<b></b></div></a>

<div class="chosen-drop">
<div class="chosen-search">
<input type="text" autocomplete="off" readonly="">
</div>

<ul class="chosen-results">
<li class="active-result" style="" data-option-array-index="1">Something 1</li>
<li class="active-result" style="" data-option-array-index="2">Something 2</li>
<li class="active-result" style="" data-option-array-index="3">Something 3</li>
<li class="active-result" style="" data-option-array-index="4">Something 4</li>
<li class="active-result" style="" data-option-array-index="5">Something 5</li>
<li class="active-result" style="" data-option-array-index="6">Something 6</li>
<li class="active-result" style="" data-option-array-index="7">Something 7</li>
<li class="active-result" style="" data-option-array-index="8">Something 8</li>
</ul>
</div>
</div>
</div>

So let's say I want to auto-select something7 (or Something 7).

I have this code that I use in Greasemonkey to auto-select from a dropdown on a different website. I tried playing with the code a bit, but failed to make it work:

Code:
// ==UserScript==
// @name     Automate
// @match    https://example.com
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.

waitForKeyElements (".a-center.div-center:has(option[value=3])", selectFinickyDropdown);

function selectFinickyDropdown (jNode) {
    var evt = new Event ("click");
    jNode[0].dispatchEvent (evt);

    jNode.val('3');

    evt = new Event ("change");
    jNode[0].dispatchEvent (evt);
}

I also have this code that I use to click some button on the same website where I have an issue:

Code:
// ==UserScript==
// @name     Automated
// @include  https://www.example.com
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

function clickSubmitBtnWhenItAppears (jNode) {
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}

//-- Value match is case-sensitive
waitForKeyElements (
    //".btn.btn-danger[type='submit'][value*='Supprimer']",
    "input[type='submit'][value*='Supprimer']",
    clickSubmitBtnWhenItAppears
);

This is one of those pesky things that's not important enough for me to pay someone to do, and I'm just short of the necessary knowledge to get it done. I've spent quite some time going through Stackoverflow threads and using various Grasemonkey scripts I use for other purposes to click on things, trying to adapt them to work here. But all failed. And yet I'm sure it's an easy solution.

Anyone?
 
Back
Top