For a mouseless future.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

onload.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. var conf = {
  2. scroll_speed: 0.3,
  3. scroll_speed_fast: 1.1,
  4. scroll_friction: 0.8,
  5. chars: "SANOTEHUCP",
  6. input_whitelist: ["checkbox", "radio", "hidden", "submit", "reset", "button", "file", "image"],
  7. location_change_check_timeout: 2000
  8. }
  9. var keys = {
  10. scroll_up: {code: "T"},
  11. scroll_down: {code: "N"},
  12. scroll_up_fast: {code: 219, shiftKey: true},
  13. scroll_down_fast: {code: 221, shiftKey: true},
  14. blobs_show: {code: "D"},
  15. blobs_show_reload: {code: "D", ctrlKey: true},
  16. blobs_hide: {code: 27},
  17. blobs_click: {code: 13},
  18. blobs_click_new_tab: {code: 13, shiftKey: true},
  19. elem_deselect: {code: 27},
  20. change_tab_left: {code: "H"},
  21. change_tab_right: {code: "S"},
  22. move_tab_left: {code: "H", shiftKey: true},
  23. move_tab_right: {code: "S", shiftKey: true},
  24. history_back: {code: "H", ctrlKey: true},
  25. history_forward: {code: "S", ctrlKey: true}
  26. }
  27. for (var i in keys) {
  28. if (typeof keys[i].code === "string") {
  29. keys[i].code = keys[i].code.charCodeAt(0);
  30. }
  31. }
  32. function isMatch(k, evt) {
  33. if ((k.code === evt.keyCode)
  34. && (!!k.ctrlKey == evt.ctrlKey)
  35. && (!!k.shiftKey == evt.shiftKey)
  36. && (!!k.altKey == evt.altKey)
  37. && (!!k.metaKey == evt.metaKey)) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. //There's a lot we don't want to do if we're not on an actual webpage, but on
  43. //the "speed dial"-ish pages.
  44. var onWebPage = (document.body !== undefined);
  45. function randomChar() {
  46. var index = Math.floor(Math.random() * conf.chars.length);
  47. return conf.chars[index];
  48. }
  49. function getElemPos(elem) {
  50. var curtop = 0;
  51. var curleft = 0;
  52. do {
  53. curtop += elem.offsetTop;
  54. curleft += elem.offsetLeft;
  55. } while (elem = elem.offsetParent);
  56. return {top: curtop, left: curleft};
  57. }
  58. var blobList = {
  59. blobs: {},
  60. container: null,
  61. visible: false,
  62. needLoadBlobs: true,
  63. currentKey: "",
  64. createContainer: function() {
  65. var container = document.createElement("div");
  66. container.style =
  67. "pointer-events: none;"+
  68. "display: none;"+
  69. "position: absolute;"+
  70. "top: 0px;"+
  71. "left: 0px;"+
  72. "z-index: 2147483647"; //Max z-index value in most browsers
  73. document.body.appendChild(container);
  74. blobList.container = container;
  75. },
  76. init: function() {
  77. if (!onWebPage)
  78. return;
  79. blobList.createContainer();
  80. window.addEventListener("scroll", function() {
  81. blobList.needLoadBlobs = true;
  82. });
  83. },
  84. currentIndex: 0,
  85. loadBlobs: function() {
  86. if (!onWebPage)
  87. return;
  88. var linkElems = document.querySelectorAll("a, button, input, textarea");
  89. //Remove old container contents
  90. blobList.container.innerHTML = ""
  91. //Remove old blobs
  92. blobList.blobs = {};
  93. var i = 0;
  94. function addBlob() {
  95. var linkElem = linkElems[i];
  96. i += 1;
  97. if (i > linkElems.length)
  98. return false;
  99. if (linkElem === undefined)
  100. return true;
  101. //We don't want hidden elements
  102. if ((linkElem === undefined)
  103. || (linkElem.style.display == "none")
  104. || (linkElem.style.visibility == "hidden")) {
  105. return true;
  106. }
  107. //Get element's absolute position
  108. var pos = getElemPos(linkElem);
  109. //Lots of things which don't really exist have an X and Y value of 0
  110. if (pos.top == 0 && pos.left == 0)
  111. return true;
  112. //We don't need to get things far above our current scroll position
  113. if (pos.top < (window.scrollY - 100))
  114. return true;
  115. //We don't need things below our scroll position either
  116. if (pos.top - 100 > (window.scrollY + window.innerHeight))
  117. return true;
  118. var key = randomChar();
  119. while (blobList.blobs[key])
  120. key += randomChar();
  121. var blobElem = document.createElement("div");
  122. blobElem.innerHTML = key;
  123. blobElem.style = [
  124. "position: absolute",
  125. "background-color: yellow",
  126. "border: 1px solid black",
  127. "border-radius: 10px",
  128. "padding-left: 3px",
  129. "padding-right: 3px",
  130. "color: black",
  131. "top: "+pos.top+"px",
  132. "left: "+pos.left+"px",
  133. "line-height: 12px",
  134. "font-size: 12px"
  135. ].join(" !important;");
  136. blobList.container.appendChild(blobElem);
  137. blobList.blobs[key] = {
  138. blobElem: blobElem,
  139. linkElem: linkElem
  140. }
  141. return true;
  142. }
  143. while (addBlob()) {};
  144. },
  145. showBlobs: function() {
  146. blobList.visible = true;
  147. blobList.container.style.display = "block";
  148. },
  149. hideBlobs: function() {
  150. blobList.currentKey = "";
  151. blobList.visible = false;
  152. blobList.container.style.display = "none";
  153. },
  154. click: function() {
  155. if (!blobList.visible)
  156. return;
  157. var blob = blobList.blobs[blobList.currentKey];
  158. if (!blob)
  159. return;
  160. blob.linkElem.click();
  161. blob.linkElem.focus();
  162. blobList.hideBlobs();
  163. },
  164. clickNewTab: function() {
  165. if (!blobList.visible)
  166. return;
  167. var blob = blobList.blobs[blobList.currentKey];
  168. if (!blob)
  169. return;
  170. if (blob.linkElem.tagName == "A" && blob.linkElem.href) {
  171. self.port.emit("tab_open", blob.linkElem.href);
  172. } else {
  173. blob.linkElem.click();
  174. blob.linkElem.focus();
  175. }
  176. blobList.hideBlobs();
  177. },
  178. appendKey: function(c) {
  179. blobList.currentKey += c;
  180. }
  181. }
  182. blobList.init();
  183. //Reload blobs whenever the URL changes
  184. var currentUrl = location.href;
  185. setInterval(function() {
  186. if (currentUrl !== location.href) {
  187. blobList.loadBlobs();
  188. }
  189. currentUrl = location.href;
  190. }, conf.location_change_check_timeout);
  191. var pressedKeys = [];
  192. function inArray(arr, val) {
  193. return (arr.indexOf(val) !== -1);
  194. }
  195. function isValidElem(elem) {
  196. var tag = elem.tagName.toLowerCase();
  197. if (tag === "textarea")
  198. return false;
  199. if (tag === "select")
  200. return false;
  201. if (elem.contentEditable.toLowerCase() === "true")
  202. return false;
  203. if ((tag === "input")
  204. && (!inArray(conf.input_whitelist, elem.type.toLowerCase()))) {
  205. return false;
  206. }
  207. return true;
  208. }
  209. window.addEventListener("keydown", function(evt) {
  210. if (/about:.+/.test(location.href))
  211. return;
  212. var active = document.activeElement;
  213. //We don't want to do anything if the user is tpying in an input field,
  214. //unless the key is to deselect an input field
  215. if (!isValidElem(active)) {
  216. if (isMatch(keys.elem_deselect, evt)) {
  217. active.blur();
  218. blobList.hideBlobs();
  219. return;
  220. } else {
  221. return;
  222. }
  223. }
  224. //User is typing a key to a blob
  225. var c = String.fromCharCode(evt.keyCode);
  226. if (blobList.visible && conf.chars.indexOf(c) !== -1) {
  227. //Hide blobs if appropriate
  228. if (isMatch(keys.blobs_hide, evt)) {
  229. blobList.hideBlobs();
  230. return;
  231. }
  232. blobList.appendKey(c);
  233. evt.preventDefault();
  234. evt.stopPropagation();
  235. return false;
  236. }
  237. //Handle other key presses
  238. //Deselect element
  239. if (onWebPage && isMatch(keys.elem_deselect, evt)) {
  240. blobList.hideBlobs();
  241. active.blur();
  242. //Show/hide/reload blobs
  243. } else if (onWebPage && !blobList.visible && isMatch(keys.blobs_show, evt)) {
  244. if (blobList.needLoadBlobs)
  245. blobList.loadBlobs();
  246. blobList.needLoadBlobs = false;
  247. blobList.showBlobs();
  248. } else if (onWebPage && !blobList.visible && isMatch(keys.blobs_show_reload, evt)) {
  249. blobList.loadBlobs();
  250. blobList.needLoadBlobs = false;
  251. blobList.showBlobs();
  252. } else if (onWebPage && blobList.visible && isMatch(keys.blobs_hide, evt)) {
  253. blobList.hideBlobs();
  254. //Simulate clicks
  255. } else if (onWebPage && blobList.visible && isMatch(keys.blobs_click, evt)) {
  256. blobList.click();
  257. } else if (onWebPage && blobList.visible && isMatch(keys.blobs_click_new_tab, evt)) {
  258. blobList.clickNewTab();
  259. //Scrolling
  260. } else if (onWebPage && isMatch(keys.scroll_up, evt)) {
  261. scroll.start(-conf.scroll_speed);
  262. } else if (onWebPage && isMatch(keys.scroll_down, evt)) {
  263. scroll.start(conf.scroll_speed);
  264. } else if (onWebPage && isMatch(keys.scroll_up_fast, evt)) {
  265. scroll.start(-conf.scroll_speed_fast);
  266. } else if (onWebPage && isMatch(keys.scroll_down_fast, evt)) {
  267. scroll.start(conf.scroll_speed_fast);
  268. //Back and forwards
  269. } else if (isMatch(keys.history_back, evt)) {
  270. history.back();
  271. } else if (isMatch(keys.history_forward, evt)) {
  272. history.forward();
  273. //Change tab
  274. } else if (isMatch(keys.change_tab_left, evt)) {
  275. self.port.emit("change_tab_left");
  276. } else if (isMatch(keys.change_tab_right, evt)) {
  277. self.port.emit("change_tab_right");
  278. //Move tab
  279. } else if (isMatch(keys.move_tab_left, evt)) {
  280. self.port.emit("move_tab_left");
  281. } else if (isMatch(keys.move_tab_right, evt)) {
  282. self.port.emit("move_tab_right");
  283. //We don't want to stop the event from propagating
  284. //if it hasn't matched anything yet
  285. } else {
  286. return true;
  287. }
  288. evt.preventDefault();
  289. evt.stopPropagation();
  290. return false;
  291. }, true);
  292. window.addEventListener("keyup", function(evt) {
  293. if ((isMatch(keys.scroll_up, evt))
  294. || (isMatch(keys.scroll_down, evt))
  295. || (isMatch(keys.scroll_up_fast, evt))
  296. || (isMatch(keys.scroll_down_fast, evt))) {
  297. scroll.stop();
  298. }
  299. }, true);
  300. var scroll = {
  301. start: function(acceleration) {
  302. scroll.acceleration = acceleration;
  303. if (scroll.raf == null)
  304. scroll.update();
  305. },
  306. stop: function() {
  307. scroll.acceleration = 0;
  308. },
  309. update: function() {
  310. var tdiff = scroll.endTime - scroll.startTime;
  311. if (tdiff < 100) {
  312. scroll.velocity += scroll.acceleration;
  313. window.scrollBy(0, scroll.velocity * tdiff);
  314. scroll.velocity *= conf.scroll_friction;
  315. }
  316. if (tdiff < 100 && scroll.velocity > -0.1 && scroll.velocity < 0.1) {
  317. scroll.velocity = 0;
  318. cancelAnimationFrame(scroll.raf);
  319. scroll.raf = null;
  320. } else {
  321. scroll.startTime = scroll.endTime;
  322. scroll.endTime = new Date().getTime();
  323. scroll.raf = requestAnimationFrame(scroll.update);
  324. }
  325. },
  326. raf: null,
  327. acceleration: 0,
  328. velocity: 0,
  329. startDate: 0,
  330. endDate: 0
  331. }