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

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