I'm trying to automate a selection an option from select on some another's web page using GreaseMonkey script. The select is defined as follow:
<select class="some-select">
<option value="one">One</option>
<option value="two">Two</option>
</select>
It has a jQuery trigger defined on the web page:
$(".some-select").on("change", doSomeStaff);
I wrote a GreaseMonkey script like this:
// ==UserScript==
// @match https://site/page/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @grant none
// ==/UserScript==
function run() {
$('.some-select option').removeAttr('selected').filter('[value=one]').prop('selected', true)
$('.some-select').val('one').trigger('change')
}
$(document).ready(function() {
run()
})
When I load the page the option is selected but the doSomeStaff
is not triggered.
When I install a new trigger in the user script it works. $('.some-select').on('change', function(){alert('user trigger')}).trigger('change')
will display the message. But the triggers defined at web page are not run.
This code has the same problem:
var event = new Event('change')
$('.some-select').each(function(){
this.dispatchEvent(event)
})
How to make web page defined triggers work from user script?