I've been poking quite a bit at this. Of course running code in the console 
works fine. But accessing a jQuery Select2 element from a userscript is a 
whole other thing.

I'm trying to set the value of an Ajax backed Select2 (v3.5) element on a 
website I don't control.
Briefly explained: What I'm trying to achieve is a combination of the 
.select2("search") method, combined with the "select2-loaded" event so I 
can pick an option and close the dropdown.

https://eddb.io/trade/commodity

Putting the following in the console on that page works fine:

jQuery.fn.select2search = function(query, property) {
  var property = (typeof property === "undefined") ? "name" : property;
  function itemsLoadedHandler(event) {
    console.log(event);
    var data = event.items.results.find(data => data[property] === query);
    this.select2("data", data);
    this.select2("close");
  }
  this.one("select2-loaded", itemsLoadedHandler.bind(this));
  this.select2("search", query);
}

jQuery('#closestlistingform-systemid').select2search("Sol");

But even with 
jQuery = window.wrappedJSObject.jQuery;

added in front in a userscript (and a timeout delay to make sure the 
Select2 has initialized - normally actions would not start on page load but 
on user interaction) the logic stops at the .one() call. Nothing is logged, 
even with a try/catch/log. The code just stops there.
When I don't add the event listener I can successfully call the 
select2("search") method.

I think I need to follow the 'unsafewindow' like solution because otherwise 
I'd be talking with a different jQuery and Select2 library to the same DOM 
element and they shouldn't be able to interact.

Is there something I can do to make this work?


-- 
You received this message because you are subscribed to the Google Groups 
"greasemonkey-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to greasemonkey-users+unsubscr...@googlegroups.com.
To post to this group, send email to greasemonkey-users@googlegroups.com.
Visit this group at https://groups.google.com/group/greasemonkey-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to