tftv keyboard shortcuts

Adds keyboard shortcuts for navigating forum pages on teamfortress.tv

2015-01-15 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        tftv keyboard shortcuts
// @namespace   Lense
// @description Adds keyboard shortcuts for navigating forum pages on teamfortress.tv
// @include     http://teamfortress.tv/thread/*
// @include     https://teamfortress.tv/thread/*
// @version     0.1
// @grant       none
// ==/UserScript==

/*
 * Helper function to scroll to post by id
 */
function goto_post(id) {
  document.getElementById(id).scrollIntoView();
  window.scrollBy(0,-40)
}

/*
 * Find the number of pages in the thread
 */
var lastPage = 1;
var nodeList = document.querySelectorAll(".page-btn");
for(var i=0; i<nodeList.length; i++) {
  if(parseInt(nodeList[i].textContent) > lastPage) {
    lastPage = parseInt(nodeList[i].textContent);
  }
}

/*
 * Find the post number of the top post on the page
 */
var posts = document.querySelectorAll(".post");
var cur_post = 0;

/*
 * Get and process keypresses
 */
document.onkeypress = function (e) {
  // Not entirely sure why this is useful, but it can't break anything that isn't already broken
  e = e || window.event;
  // Ignore keypresses in text boxes
  if(e.target.nodeName != "BODY") {
    // got keypress, but it's probably in a textbox, so ignore it
    return;
  }
  // Current page number
  var page = window.location.search == "" || parseInt(window.location.search.match("page=([0-9]*)")[1]);
  // and yes, I am proud of the logic in that statement.

  switch(e["key"]) {
    /*
     * Pages (left/right)
     */
    case "d":
      if(page != lastPage) {
        page = (page+1).toString();
        window.location.search = "?page=" + page;
      }
      break;
    case "a":
      if(page != 1) {
        page = (page-1).toString();
        window.location.search = "?page=" + page;
      }
      break;
    case "D":
      if(page != lastPage) {
        window.location.search = "?page=" + lastPage.toString();
      }
      break;
    case "A":
      if(page != 1) {
        window.location.search = "?page=1";
      }
      break;
    /*
     * Posts (up/down)
     */
    case "w":
      if(cur_post > 0) {
        cur_post--;
      }
      goto_post(posts[cur_post].id);
      break;
    case "s":
      if(cur_post < posts.length - 1) {
        cur_post++;
      }
      goto_post(posts[cur_post].id);
      break;
    case "W":
      cur_post = 0;
      goto_post(posts[cur_post].id);
      break;
    case "S":
      cur_post = posts.length - 1;
      goto_post(posts[cur_post].id);
      break;
    /*
     * Frags (+/-)
     */
    case "q":
      $.ajax({
        type: 'post',
        data: {},
        url: '/post/frag/' + posts[cur_post].id.slice(8) + '/plus',
      });
      break;
    case "e":
      $.ajax({
        type: 'post',
        data: {},
        url: '/post/frag/' + posts[cur_post].id.slice(8) + '/minus',
      });
      break;
  }};