Heello Keyboard Shortcuts

jkでタイムラインをスクロール

当前为 2014-08-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Heello Keyboard Shortcuts
  3. // @namespace http://userstyles.org
  4. // @description jkでタイムラインをスクロール
  5. // @author kawau
  6. // @match http://heello.com/*
  7. // @match https://heello.com/*
  8. // @version 0.7
  9. // ==/UserScript==
  10.  
  11. // Navigation
  12. //
  13. // j: Next Ping
  14. // k: Previous Ping
  15. // gg: Scroll to top
  16. // shift+g: Scroll to bottom
  17. //
  18. // Actions
  19. //
  20. // e: Echo
  21.  
  22.  
  23.  
  24. /****************************************************************
  25. * http://www.openjs.com/scripts/events/keyboard_shortcuts/
  26. * Version : 2.01.B
  27. * By Binny V A
  28. * License : BSD
  29. */
  30. shortcut = {
  31. 'all_shortcuts':{},//All the shortcuts are stored in this array
  32. 'add': function(shortcut_combination,callback,opt) {
  33. //Provide a set of default options
  34. var default_options = {
  35. 'type':'keydown',
  36. 'propagate':false,
  37. 'disable_in_input':true,
  38. 'target':document,
  39. 'keycode':false
  40. }
  41. if(!opt) opt = default_options;
  42. else {
  43. for(var dfo in default_options) {
  44. if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
  45. }
  46. }
  47.  
  48. var ele = opt.target;
  49. if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
  50. var ths = this;
  51. shortcut_combination = shortcut_combination.toLowerCase();
  52.  
  53. //The function to be called at keypress
  54. var func = function(e) {
  55. e = e || window.event;
  56.  
  57. if(opt['disable_in_input']) { //Don't enable shortcut keys in Input, Textarea fields
  58. var element;
  59. if(e.target) element=e.target;
  60. else if(e.srcElement) element=e.srcElement;
  61. if(element.nodeType==3) element=element.parentNode;
  62.  
  63. if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
  64. }
  65.  
  66. //Find Which key is pressed
  67. if (e.keyCode) code = e.keyCode;
  68. else if (e.which) code = e.which;
  69. var character = String.fromCharCode(code).toLowerCase();
  70.  
  71. if(code == 188) character=","; //If the user presses , when the type is onkeydown
  72. if(code == 190) character="."; //If the user presses , when the type is onkeydown
  73.  
  74. var keys = shortcut_combination.split("+");
  75. //Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
  76. var kp = 0;
  77.  
  78. //Work around for stupid Shift key bug created by using lowercase - as a result the shift+num combination was broken
  79. var shift_nums = {
  80. "`":"~",
  81. "1":"!",
  82. "2":"@",
  83. "3":"#",
  84. "4":"$",
  85. "5":"%",
  86. "6":"^",
  87. "7":"&",
  88. "8":"*",
  89. "9":"(",
  90. "0":")",
  91. "-":"_",
  92. "=":"+",
  93. ";":":",
  94. "'":"\"",
  95. ",":"<",
  96. ".":">",
  97. "/":"?",
  98. "\\":"|"
  99. }
  100. //Special Keys - and their codes
  101. var special_keys = {
  102. 'esc':27,
  103. 'escape':27,
  104. 'tab':9,
  105. 'space':32,
  106. 'return':13,
  107. 'enter':13,
  108. 'backspace':8,
  109.  
  110. 'scrolllock':145,
  111. 'scroll_lock':145,
  112. 'scroll':145,
  113. 'capslock':20,
  114. 'caps_lock':20,
  115. 'caps':20,
  116. 'numlock':144,
  117. 'num_lock':144,
  118. 'num':144,
  119.  
  120. 'pause':19,
  121. 'break':19,
  122.  
  123. 'insert':45,
  124. 'home':36,
  125. 'delete':46,
  126. 'end':35,
  127.  
  128. 'pageup':33,
  129. 'page_up':33,
  130. 'pu':33,
  131.  
  132. 'pagedown':34,
  133. 'page_down':34,
  134. 'pd':34,
  135.  
  136. 'left':37,
  137. 'up':38,
  138. 'right':39,
  139. 'down':40,
  140.  
  141. 'f1':112,
  142. 'f2':113,
  143. 'f3':114,
  144. 'f4':115,
  145. 'f5':116,
  146. 'f6':117,
  147. 'f7':118,
  148. 'f8':119,
  149. 'f9':120,
  150. 'f10':121,
  151. 'f11':122,
  152. 'f12':123
  153. }
  154.  
  155. var modifiers = {
  156. shift: { wanted:false, pressed:false},
  157. ctrl : { wanted:false, pressed:false},
  158. alt : { wanted:false, pressed:false},
  159. meta : { wanted:false, pressed:false} //Meta is Mac specific
  160. };
  161.  
  162. if(e.ctrlKey) modifiers.ctrl.pressed = true;
  163. if(e.shiftKey) modifiers.shift.pressed = true;
  164. if(e.altKey) modifiers.alt.pressed = true;
  165. if(e.metaKey) modifiers.meta.pressed = true;
  166.  
  167. for(var i=0; k=keys[i],i<keys.length; i++) {
  168. //Modifiers
  169. if(k == 'ctrl' || k == 'control') {
  170. kp++;
  171. modifiers.ctrl.wanted = true;
  172.  
  173. } else if(k == 'shift') {
  174. kp++;
  175. modifiers.shift.wanted = true;
  176.  
  177. } else if(k == 'alt') {
  178. kp++;
  179. modifiers.alt.wanted = true;
  180. } else if(k == 'meta') {
  181. kp++;
  182. modifiers.meta.wanted = true;
  183. } else if(k.length > 1) { //If it is a special key
  184. if(special_keys[k] == code) kp++;
  185.  
  186. } else if(opt['keycode']) {
  187. if(opt['keycode'] == code) kp++;
  188.  
  189. } else { //The special keys did not match
  190. if(character == k) kp++;
  191. else {
  192. if(shift_nums[character] && e.shiftKey) { //Stupid Shift key bug created by using lowercase
  193. character = shift_nums[character];
  194. if(character == k) kp++;
  195. }
  196. }
  197. }
  198. }
  199.  
  200. if(kp == keys.length &&
  201. modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
  202. modifiers.shift.pressed == modifiers.shift.wanted &&
  203. modifiers.alt.pressed == modifiers.alt.wanted &&
  204. modifiers.meta.pressed == modifiers.meta.wanted) {
  205. callback(e);
  206.  
  207. if(!opt['propagate']) { //Stop the event
  208. //e.cancelBubble is supported by IE - this will kill the bubbling process.
  209. e.cancelBubble = true;
  210. e.returnValue = false;
  211.  
  212. //e.stopPropagation works in Firefox.
  213. if (e.stopPropagation) {
  214. e.stopPropagation();
  215. e.preventDefault();
  216. }
  217. return false;
  218. }
  219. }
  220. }
  221. this.all_shortcuts[shortcut_combination] = {
  222. 'callback':func,
  223. 'target':ele,
  224. 'event': opt['type']
  225. };
  226. //Attach the function with the event
  227. if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
  228. else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
  229. else ele['on'+opt['type']] = func;
  230. },
  231.  
  232. //Remove the shortcut - just specify the shortcut and I will remove the binding
  233. 'remove':function(shortcut_combination) {
  234. shortcut_combination = shortcut_combination.toLowerCase();
  235. var binding = this.all_shortcuts[shortcut_combination];
  236. delete(this.all_shortcuts[shortcut_combination])
  237. if(!binding) return;
  238. var type = binding['event'];
  239. var ele = binding['target'];
  240. var callback = binding['callback'];
  241.  
  242. if(ele.detachEvent) ele.detachEvent('on'+type, callback);
  243. else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
  244. else ele['on'+type] = false;
  245. }
  246. };
  247.  
  248. /**********************************************************/
  249. /* End of "shorcut.js" */
  250. /**********************************************************/
  251.  
  252.  
  253. // Add class name
  254. function add_class_name(obj,add_classes){
  255. var tmp_hash = new Array();
  256. var new_class_names = new Array();
  257. var class_names = obj.className.split(" ").concat(add_classes.split(" "));
  258. for(var i in class_names){if(class_names[i] != ""){tmp_hash[class_names[i]] = 0;}}
  259. for(var key in tmp_hash){new_class_names.push(key);}
  260. obj.className = new_class_names.join(" ");
  261. }
  262.  
  263. // Delete class name
  264. function delete_class_name(obj,delete_classes){
  265. var new_class_names = new Array();
  266. var class_names = obj.className.split(" ");
  267. var delete_class_names = delete_classes.split(" ");
  268. for(var i in class_names){
  269. var flag = true;
  270. for(var j in delete_class_names){
  271. if(class_names[i] == delete_class_names[j]){flag = false;break;};
  272. }
  273. if(flag){new_class_names.push(class_names[i])}
  274. }
  275. obj.className = new_class_names.join(" ");
  276. }
  277. /*
  278. // Send Ping
  279. function shortPing(content) {
  280. var obj = new XMLHttpRequest();
  281. obj.open('POST', "https://heello.com/pings.json", true);
  282. obj.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  283. obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  284. obj.send("text="+encodeURIComponent(content));
  285. }
  286.  
  287. // Echo
  288. function shortEcho(id) {
  289. var obj = new XMLHttpRequest();
  290. obj.open('POST', "https://heello.com/pings/" + id + "/echo", true);
  291. obj.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  292. obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  293. obj.send('');
  294. }
  295.  
  296. // Like
  297. function shortLike(id) {
  298. var obj = new XMLHttpRequest();
  299. obj.open('POST', "https://heello.com/pings/" + id + "/like.json", true);
  300. obj.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  301. obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  302. obj.send('');
  303. }
  304.  
  305. // Button
  306. function clickPing(){
  307. var forms = document.getElementsByTagName('form');
  308. if (forms[0].getElementsByTagName('textarea')[0].value){
  309. document.querySelector('div.modal-buttons input[type="button"].grey-button').click();
  310. }
  311. if (forms[1]) {
  312. if (forms[1].getElementsByTagName('textarea')[0].value){
  313. forms[1].querySelector('input[type="button"].grey-button').click();
  314. }
  315. }
  316. document.activeElement.blur();
  317. }
  318. */
  319.  
  320. // Scroll window
  321. function scrollStream(elm) {
  322. var body = document.body;
  323. var html = document.documentElement;
  324. var rect = elm.getBoundingClientRect();
  325. var pos = {
  326. "left": rect.left + (body.scrollLeft || html.scrollLeft),
  327. "top" : rect.top + (body.scrollTop || html.scrollTop) - (html.clientHeight)/2 + 90
  328. }
  329. window.scrollTo(body.scrollLeft || html.scrollLeft, pos.top);
  330. }
  331.  
  332. // Initialization
  333. var userprofile = document.querySelector('ul#nav-buttons.nav-pills li.dropdown ul.dropdown-menu li a').href;
  334. var username = userprofile.replace('https://heello.com/', '');
  335.  
  336. // Background-color of selected Ping
  337. var sh = document.styleSheets[0];
  338. sh.insertRule("div.hovered-stream-item {background-color: #eff8ff;}", sh.cssRules.length);
  339.  
  340. var pnt = null;
  341. var cmf = false;
  342. var elements = document.getElementById('timeline');
  343.  
  344. //var newPing = document.querySelector('span#nav-compose.black-button img');
  345. var click = function(n) {
  346. var e = document.createEvent('MouseEvents');
  347. e.initMouseEvent("click",true,true,window,0,0,0,0,0,false,false,false,false,0,null);
  348. n.dispatchEvent(e);
  349. };
  350.  
  351. // Keyboard shortcuts
  352.  
  353. // Navigation
  354. //
  355. // j: Next Ping
  356. shortcut.add('j', function() {
  357. var hov = elements.querySelector('div.hovered-stream-item');
  358. if (pnt === null) {
  359. add_class_name(elements.querySelector('div[data-id]'), 'hovered-stream-item');
  360. pnt = 0;
  361. } else if (pnt >= 0 && pnt < elements.querySelectorAll('div[data-id]').length-1) {
  362. delete_class_name(hov, 'hovered-stream-item');
  363. add_class_name(elements.querySelectorAll('div[data-id]')[pnt+1], 'hovered-stream-item');
  364. pnt++;
  365. }
  366. var hov2 = elements.querySelector('div.hovered-stream-item');
  367. if (hov2) {
  368. scrollStream(hov2);
  369. }
  370. });
  371.  
  372. // k: Previous Ping
  373. shortcut.add('k', function() {
  374. var hov = elements.querySelector('div.hovered-stream-item');
  375. if (pnt === 0) {
  376. delete_class_name(hov, 'hovered-stream-item');
  377. pnt = null;
  378. } else if (pnt > 0) {
  379. delete_class_name(hov, 'hovered-stream-item');
  380. add_class_name(elements.querySelectorAll('div[data-id]')[pnt-1], 'hovered-stream-item');
  381. pnt--;
  382. }
  383. var hov2 = elements.querySelector('div.hovered-stream-item');
  384. if (hov2) {
  385. scrollStream(hov2);
  386. }
  387. });
  388.  
  389. // shift+g: scroll to bottom
  390. shortcut.add('shift+g', function() {
  391. var hov = elements.querySelector('div.hovered-stream-item');
  392. var pings = elements.querySelectorAll('div[data-id]');
  393. if (pnt !== null && pnt < pings.length-1) {
  394. delete_class_name(hov, 'hovered-stream-item');
  395. }
  396. pnt = pings.length-1;
  397. add_class_name(pings[pnt], 'hovered-stream-item');
  398. scrollStream(pings[pnt]);
  399. });
  400.  
  401. // Actions
  402. //
  403. // e: Echo
  404. shortcut.add('e', function() {
  405. // var id;
  406. var echolink;
  407. var hov = elements.querySelector('div.hovered-stream-item');
  408. if (hov) {
  409. echolink = hov.querySelector('a[href$="echo"]');
  410. // id = hov.getAttribute('data-id');
  411. } else {
  412. // id = elements.querySelector('div[data-id]').getAttribute('data-id');
  413. echolink = elements.querySelector('div[data-id] a[href$="echo"]');
  414. }
  415. // shortEcho(id);
  416. click(echolink);
  417. });
  418. /*
  419. // c: Repeat
  420. shortcut.add('c', function() {
  421. var content;
  422. var hov = elements.querySelector('div.hovered-stream-item');
  423. if (hov) {
  424. content = hov.querySelector('div.ping-text').textContent;
  425. } else {
  426. content = elements.querySelector('div.ping-text').textContent;
  427. }
  428. shortPing(content);
  429. });
  430. */
  431. // enter: Open Ping details
  432. shortcut.add('enter', function() {
  433. window.location.href = elements.querySelector('div.hovered-stream-item h3 small a').href;
  434. });
  435. /*
  436. // n: New Ping
  437. shortcut.add('n', function() {
  438. click(newPing);
  439. });
  440.  
  441. // shift + enter: Send Ping
  442. // ctrl + enter : Send Ping
  443. shortcut.add('shift+enter', function() {
  444. clickPing();
  445. }, { 'disable_in_input': false});
  446.  
  447. shortcut.add('ctrl+enter', function() {
  448. clickPing();
  449. }, { 'disable_in_input': false});
  450. */
  451. // Timelines
  452. //
  453. // g: Timeline Commands Flag ON
  454. // g g: Scroll to top
  455. shortcut.add('g', function() {
  456. if (cmf) {
  457. cmf = false;
  458. var hov = elements.querySelector('div.hovered-stream-item');
  459. if (hov) {
  460. delete_class_name(hov, 'hovered-stream-item');
  461. }
  462. pnt = null;
  463. window.scrollTo(document.body.scrollLeft || document.documentElement.scrollLeft, 0);
  464. } else {
  465. cmf = true;
  466. }
  467. });
  468. /*
  469. // g h: Home
  470. shortcut.add('h', function() {
  471. if (cmf) {
  472. cmf = false;
  473. window.location.href = '/';
  474. }
  475. });
  476.  
  477. // r: Reply
  478. // g r: Replies
  479. shortcut.add('r', function() {
  480. if (cmf) {
  481. cmf = false;
  482. window.location.href = '/replies';
  483. } else {
  484. click(elements.querySelector('div.hovered-stream-item span.ping-reply img'));
  485. }
  486. });
  487.  
  488. // g l: What's Happening?
  489. shortcut.add('l', function() {
  490. if (cmf) {
  491. cmf = false;
  492. window.location.href = '/live';
  493. }
  494. });
  495.  
  496. // g p: Profile
  497. shortcut.add('p', function() {
  498. if (cmf) {
  499. cmf = false;
  500. window.location.href = '/' + username;
  501. }
  502. });
  503.  
  504. // g s: Settings
  505. shortcut.add('s', function() {
  506. if (cmf) {
  507. cmf = false;
  508. window.location.href = '/account/settings';
  509. }
  510. });
  511.  
  512. // f: Like
  513. // g f: Likes
  514. shortcut.add('f', function() {
  515. if (cmf) {
  516. cmf = false;
  517. window.location.href = '/' + username + '/likes';
  518. } else {
  519. var hov = elements.querySelector('div.hovered-stream-item');
  520. if (hov) {
  521. id = hov.getAttribute('data-id');
  522. shortLike(id);
  523. }
  524. }
  525. });
  526.  
  527. //
  528. // esc: Close modal windows
  529. shortcut.add('esc', function() {
  530. cmf = false;
  531. var closeButtons = document.querySelectorAll("div.modal-title div.modal-title-close");
  532. for (var i=0; i<closeButtons.length; i++) {
  533. click(closeButtons[i]);
  534. }
  535. document.activeElement.blur();
  536. }, {'disable_in_input': false});
  537. */
  538.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址