// ==UserScript==
// @name          instant alc search
// @namespace     http://blog.fkoji.com/
// @description	  we always search by alc
// @include       http://*
// ==/UserScript==

(function() {
 var x = 100; // point x
 var y = 100; // point y
 var id = 0;  // div tag id

 document.addEventListener("keydown", my_key_down, true);
 document.addEventListener("click", my_click, true);

 function my_click(e) {
  x = e.clientX + window.scrollX;
  y = e.clientY + window.scrollY;
 }

 // keyCode:65 is 'a'
 function my_key_down(evt) {
  if (evt.keyCode == 65) {
   // q is selected text
   var q = document.getSelection();
   if (q != "") {
    callXmlHttpRequest(q);
    id++;
   }
  }
 }

 function callXmlHttpRequest(q) {
  // alc Search URL
  var url = 'http://www2.alc.co.jp/ejr/index.php?word_in=' + q + '&word_in2=%82%A0%82%A2%82%A4%82%A6%82%A8&word_in3=PVawEWi72JXCKoa0Je';

  GM_xmlhttpRequest({method: "GET", url: url, onload: onload});

  function onload(response) {
   var res = response.responseText;
   res.match(/<li>.+/);
   res = RegExp.rightContext;
   res.match(/.+/);
   res = RegExp.rightContext;
   res.match(/<br>(.+?<\/.+?>)?(.+?)<br>/);
   var res = RegExp.$2;
   alert(res);
  }
 }
})();

