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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. clickClipboard: function() {
  193. if (!blobList.visible)
  194. return;
  195. var blob = blobList.blobs[blobList.currentKey];
  196. if (!blob)
  197. return;
  198. if (!blob.linkElem.href)
  199. return;
  200. self.port.emit("clipboard_set", blob.linkElem.href);
  201. blobList.hideBlobs();
  202. },
  203. appendKey: function(c) {
  204. blobList.currentKey += c;
  205. blobList.overview.innerHTML = blobList.currentKey;
  206. },
  207. backspace: function() {
  208. blobList.currentKey = blobList.currentKey.substring(0, blobList.currentKey.length - 1);
  209. blobList.overview.innerHTML = blobList.currentKey;
  210. }
  211. }
  212. blobList.init();
  213. //Reload blobs whenever the URL changes
  214. var currentUrl = location.href;
  215. setInterval(function() {
  216. if (currentUrl !== location.href) {
  217. blobList.loadBlobs();
  218. }
  219. currentUrl = location.href;
  220. }, conf.location_change_check_timeout);
  221. var pressedKeys = [];
  222. function inArray(arr, val) {
  223. return (arr.indexOf(val) !== -1);
  224. }
  225. function isValidElem(elem) {
  226. var tag = elem.tagName.toLowerCase();
  227. if (tag === "textarea")
  228. return false;
  229. if (tag === "select")
  230. return false;
  231. if (elem.contentEditable.toLowerCase() === "true")
  232. return false;
  233. if ((tag === "input")
  234. && (!inArray(conf.input_whitelist, elem.type.toLowerCase()))) {
  235. return false;
  236. }
  237. return true;
  238. }
  239. window.addEventListener("keydown", function(evt) {
  240. if (/about:.+/.test(location.href))
  241. return;
  242. var active = document.activeElement;
  243. //We don't want to do anything if the user is tpying in an input field,
  244. //unless the key is to deselect an input field
  245. if (!isValidElem(active)) {
  246. if (isMatch(keys.elem_deselect, evt)) {
  247. active.blur();
  248. blobList.hideBlobs();
  249. return;
  250. } else {
  251. return;
  252. }
  253. }
  254. //User is typing a key to a blob
  255. if (blobList.visible) {
  256. //Hide blobs if appropriate
  257. if (isMatch(keys.blobs_hide, evt)) {
  258. blobList.hideBlobs();
  259. return;
  260. }
  261. //Backspace if appropriate
  262. if (isMatch(keys.blobs_backspace, evt)) {
  263. blobList.backspace();
  264. return;
  265. }
  266. var c = evt.key;
  267. if (conf.chars.indexOf(c) !== -1) {
  268. blobList.appendKey(c);
  269. evt.preventDefault();
  270. evt.stopPropagation();
  271. return false;
  272. }
  273. }
  274. //Handle other key presses
  275. //Deselect element
  276. if (onWebPage && isMatch(keys.elem_deselect, evt)) {
  277. blobList.hideBlobs();
  278. active.blur();
  279. //Show/hide/reload blobs
  280. } else if (onWebPage && !blobList.visible && isMatch(keys.blobs_show, evt)) {
  281. blobList.loadBlobs();
  282. blobList.needLoadBlobs = false;
  283. blobList.showBlobs();
  284. } else if (onWebPage && blobList.visible && isMatch(keys.blobs_hide, evt)) {
  285. blobList.hideBlobs();
  286. //Simulate clicks
  287. } else if (onWebPage && blobList.visible && isMatch(keys.blobs_click, evt)) {
  288. blobList.click();
  289. } else if (onWebPage && blobList.visible && isMatch(keys.blobs_click_new_tab, evt)) {
  290. blobList.clickNewTab();
  291. } else if (onWebPage && blobList.visible && isMatch(keys.blobs_click_clipboard, evt)) {
  292. blobList.clickClipboard();
  293. //Scrolling
  294. } else if (onWebPage && isMatch(keys.scroll_up, evt)) {
  295. scroll.start(-conf.scroll_speed);
  296. } else if (onWebPage && isMatch(keys.scroll_down, evt)) {
  297. scroll.start(conf.scroll_speed);
  298. } else if (onWebPage && isMatch(keys.scroll_up_fast, evt)) {
  299. scroll.start(-conf.scroll_speed_fast);
  300. } else if (onWebPage && isMatch(keys.scroll_down_fast, evt)) {
  301. scroll.start(conf.scroll_speed_fast);
  302. //Back and forwards
  303. } else if (isMatch(keys.history_back, evt)) {
  304. history.back();
  305. } else if (isMatch(keys.history_forward, evt)) {
  306. history.forward();
  307. //Change tab
  308. } else if (isMatch(keys.change_tab_left, evt)) {
  309. self.port.emit("change_tab_left");
  310. } else if (isMatch(keys.change_tab_right, evt)) {
  311. self.port.emit("change_tab_right");
  312. //Move tab
  313. } else if (isMatch(keys.move_tab_left, evt)) {
  314. self.port.emit("move_tab_left");
  315. } else if (isMatch(keys.move_tab_right, evt)) {
  316. self.port.emit("move_tab_right");
  317. //We don't want to stop the event from propagating
  318. //if it hasn't matched anything yet
  319. } else {
  320. return true;
  321. }
  322. evt.preventDefault();
  323. evt.stopPropagation();
  324. return false;
  325. }, true);
  326. window.addEventListener("keyup", function(evt) {
  327. if ((isMatch(keys.scroll_up, evt))
  328. || (isMatch(keys.scroll_down, evt))
  329. || (isMatch(keys.scroll_up_fast, evt))
  330. || (isMatch(keys.scroll_down_fast, evt))) {
  331. scroll.stop();
  332. }
  333. }, true);
  334. var scroll = {
  335. start: function(acceleration) {
  336. scroll.acceleration = acceleration;
  337. if (scroll.raf == null)
  338. scroll.update();
  339. },
  340. stop: function() {
  341. scroll.acceleration = 0;
  342. },
  343. update: function() {
  344. var tdiff = scroll.endTime - scroll.startTime;
  345. if (tdiff < 100) {
  346. scroll.velocity += scroll.acceleration;
  347. window.scrollBy(0, scroll.velocity * tdiff);
  348. scroll.velocity *= conf.scroll_friction;
  349. }
  350. if (tdiff < 100 && scroll.velocity > -0.1 && scroll.velocity < 0.1) {
  351. scroll.velocity = 0;
  352. cancelAnimationFrame(scroll.raf);
  353. scroll.raf = null;
  354. } else {
  355. scroll.startTime = scroll.endTime;
  356. scroll.endTime = new Date().getTime();
  357. scroll.raf = requestAnimationFrame(scroll.update);
  358. }
  359. },
  360. raf: null,
  361. acceleration: 0,
  362. velocity: 0,
  363. startDate: 0,
  364. endDate: 0
  365. }