Well, perhaps something more simple works also. But the following also 
works.
Download the attached file, open it and create the userscript further down 
(Window based @include).

Basically we inject a native window script that listens to a window 
CustomEvent which we trigger without a problem from the userscript.
I'm using a little functionality (*function.toString()*) to keep nice 
syntax of code while being able to easily treat it as a string.

// ==UserScript==
// @name     Select2 search across userscript/window boundary.
// @version  1
// @include  file:///*/select2-userscript-test.html
// @grant    none
// ==/UserScript==

(function() {
  function DOM_inject_script() {
    // Use function.toString() to keep pretty code and inject it into the 
body for native browser code.
    var script = document.getElementsByTagName('head')[0].appendChild(
document.createElement('script'));
    script.setAttribute('type', 'text/javascript');
    return script.textContent=DOM_inject_script.toString().replace(
/[\s\S]*"\$1"\);([\s\S]*)}/,"$1");

    /**
     * Select an item in a Select2 element. Supports Ajax backed data.
     *
     * @param {string} query: value to look for
     * @param {string} property (optional): Property of select2 data to 
compare the value with.
     *  Defaults to "name".
     */
    jQuery.fn.select2search = function(query, property) {
      var property = (typeof property === "undefined") ? "name" : property;
      function itemsLoadedHandler(event) {
        var data = event.items.results.find(data => data[property] === query
);
        this.select2("data", data);
        // Somehow calling "close" directly doesn't close it because of the 
magic we're using.
        window.setTimeout(function() {
        this.select2("close");
        }.bind(this), 10);
      }
      this.one("select2-loaded", itemsLoadedHandler.bind(this));
      this.select2("search", query);
    }

    window.addEventListener("select2search", function(event) {
      if (typeof event.detail === "undefined") { return; }
      // We can't pass objects across the boundary, but a string is ok.
      var eventdata = JSON.parse(event.detail);
      if (typeof eventdata.query === "undefined" || typeof eventdata.selector 
=== "undefined") { return; }
      // Optional property to check the select2 data query on.
      var property = (typeof eventdata.property !== "undefined") ? eventdata
.property : undefined;

      jQuery(eventdata.selector).select2search(eventdata.query, property);
    });
  }
  DOM_inject_script();
})();

/*
var select2search_script = document.createElement('script');
select2search_script.textContent = browser_select2search_source;
document.querySelector('body').appendChild(select2search_script);
*/
function select2search(selector, query, property) {
  var event = new CustomEvent("select2search", {
    "detail": JSON.stringify({
      "selector": selector,
      "query": query,
      "property": property
    })
  });
  window.dispatchEvent(event);
}

window.setTimeout(function() {
  select2search(".jSelectbox", "Bicycle", "text");
}, 1000);


-- 
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.
Car Bicycle

Reply via email to