PageRenderTime 21ms CodeModel.GetById 8ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/CVox/res/raw/gws.user.js

http://eyes-free.googlecode.com/
JavaScript | 195 lines | 160 code | 26 blank | 9 comment | 29 complexity | 7c507842bd061afde2228f455229e3a9 MD5 | raw file
  1// ==UserScript==
  2// @name          GWS
  3// @description   GWS Script
  4// @author        Charles L. Chen
  5// @include       http://www.google.com/search?*
  6// ==/UserScript==
  7
  8var inputFocused = false;
  9
 10var resultElems = new Array();
 11var resultsIdx = -1;
 12
 13
 14
 15function buildResultElems(){
 16  resultElems = new Array();
 17  var resNode = document.getElementById("res");
 18  var medNodes = resNode.getElementsByClassName("med");
 19  if (medNodes.length > 0){
 20//    resultElems.push(medNodes[0]);
 21  }
 22  var gNodes = resNode.getElementsByClassName("g");
 23  for (var i=0, gNode; gNode = gNodes[i]; i++){
 24    resultElems.push(gNode);
 25  }
 26}
 27
 28
 29function getNextResult(){
 30  if (resultElems.length < 1){
 31    return null;
 32  }
 33  resultsIdx++;
 34  if (resultsIdx > resultElems.length - 1){
 35    resultsIdx = 0;
 36    return null;
 37  }
 38  var elem = resultElems[resultsIdx];
 39  return elem;
 40}
 41
 42function getPrevResult(){
 43  if (resultElems.length < 1){
 44    return null;
 45  }
 46  resultsIdx--;
 47  if (resultsIdx < 0){
 48    resultsIdx = resultElems.length - 1;
 49    return null;
 50  }
 51  var elem = resultElems[resultsIdx];
 52  return elem;
 53}
 54
 55
 56function scrollToElem(targetNode){
 57  while (!targetNode.offsetParent){
 58    targetNode = targetNode.parentNode;
 59  }
 60  var left = 0;
 61  var top = 0;
 62  while (targetNode.offsetParent) { 
 63    left += targetNode.offsetLeft;
 64    top += targetNode.offsetTop; 
 65    targetNode = targetNode.offsetParent;
 66  }
 67  left += targetNode.offsetLeft;
 68  top += targetNode.offsetTop;
 69  window.scrollTo(left, top);
 70}
 71
 72function applyLens(targetNode){
 73  var lensNode = document.getElementById("CVOX_LENS");
 74  if (!lensNode){
 75    lensNode = document.createElement('span');
 76	lensNode.id = "CVOX_LENS";
 77    lensNode.style.backgroundColor = '#CCE6FF';
 78    lensNode.style.borderColor = '#0000CC';
 79    lensNode.style.borderWidth = 'medium';
 80    lensNode.style.borderStyle = 'groove';
 81    lensNode.style.position = 'absolute';
 82    lensNode.style.display = 'none';
 83    document.body.appendChild(lensNode);
 84  }
 85  while (lensNode.firstChild){
 86    lensNode.removeChild(lensNode.firstChild);
 87  }
 88  if (targetNode === null) {
 89    lensNode.style.display = 'none';
 90    return;
 91  }
 92  var left = 0;
 93  var top = 0;
 94  var obj = targetNode;
 95  if (obj.offsetParent) {
 96    left = obj.offsetLeft;
 97    top = obj.offsetTop;
 98    obj = obj.offsetParent;
 99    while (obj !== null) {
100      left += obj.offsetLeft;
101      top += obj.offsetTop;
102      obj = obj.offsetParent;
103    }
104  }
105  lensNode.appendChild(targetNode.cloneNode(true));
106  lensNode.style.top = top + 'px';
107  lensNode.style.left = left + 'px';
108  lensNode.style.zIndex = 999;
109  lensNode.style.display = 'block';
110
111  var adjustment = (1.3 * 100) + '%';
112  lensNode.style.setProperty('font-size', adjustment, 'important');
113  var subnodes = lensNode.getElementsByTagName('*');
114  for (var i = 0, node; node = subnodes[i]; i++){
115    node.style.setProperty('line-height', 'normal', 'important');
116    node.style.setProperty('font-size', '100%', 'important');
117  }
118
119  
120  scrollToElem(lensNode);
121}
122
123
124
125
126function speak(textStr, queueMode, paramsArray){
127  window.ttsHelper.speak(textStr, queueMode);
128  //alert(textStr);
129}
130
131//TODO: Come up with a working escape sequence!
132function keyPressHandler(evt){
133  var keyCode = evt.keyCode;
134  if (inputFocused){
135    return true;
136  }
137  if (keyCode == 106) { // j
138    readNextResult();
139    return false;
140  }
141  if (keyCode == 107) { // k
142    var resultNode = getPrevResult();
143    if (resultNode){
144      speak(resultNode.textContent,0,null);
145	    applyLens(resultNode);
146    }
147    return false;
148  }
149  return true;
150}
151
152function keyDownHandler(evt){
153  window.ttsHelper.stop();
154  var keyCode = evt.keyCode;
155  if (inputFocused){
156    return true;
157  }
158  if ((keyCode == 32) || (keyCode == 13)) { // space or Enter (G1 enter key does not work)
159    var elem = resultElems[resultsIdx];
160    var links = elem.getElementsByTagName('A');
161    document.location = links[0].href;
162    return false;
163  }  
164  return true;
165}
166
167function focusHandler(evt){
168  if (evt.target.tagName && 
169      evt.target.tagName == 'INPUT'){
170      inputFocused = true;
171      }
172  return true;
173}
174
175function blurHandler(evt){
176  inputFocused = false;
177  return true;
178}
179
180function readNextResult(){
181  var resultNode = getNextResult();
182  if (resultNode){
183    speak(resultNode.textContent,0,null);
184	  applyLens(resultNode);
185  }
186}
187
188document.addEventListener('keypress', keyPressHandler, true);
189document.addEventListener('keydown', keyDownHandler, true);
190document.addEventListener('focus', focusHandler, true);
191document.addEventListener('blur', blurHandler, true);
192
193
194buildResultElems();
195readNextResult();