PageRenderTime 32ms CodeModel.GetById 16ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/WebVox/res/raw/mgws.user.js

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