For a mouseless future.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

onload.js 9.9KB

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