[greasemonkey-users] Re: How to interact with a jQuery Select2 element

2018-04-09 Thread ArmEagle
Or in an easier to use format, simple to include in a userscript:
https://github.com/ArmEagle/userscripts/blob/master/util/select2search.js

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


[greasemonkey-users] Re: How to interact with a jQuery Select2 element

2018-04-09 Thread ArmEagle
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
// @grantnone
// ==/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
  



[greasemonkey-users] Re: How to interact with a jQuery Select2 element

2018-04-08 Thread ArmEagle
Of course now I just submitted the post I realize the title of my post 
doesn't point at the specific issue I'm having: not being able to add a 
jQuery event listener in the 'unsafewindow'.

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