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

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