0

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?

  • Several issues are potentially at play and you did not provide enough information to duplicate the problem. If the duplicates do not point you to your issue (they all apply), supply your version numbers, plus either a link to the actual target page or a [mcve]. – Brock Adams Apr 06 '19 at 00:35

1 Answers1

0

May be this will work for you.

$(".some-select").on("change", function(){
    alert('yes');
});
Shoaib
  • 314
  • 1
  • 4