Advertisement
Guest User

ph_hotkeys_in_forum.user.js

a guest
May 14th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          prohardver.hu : forum : hotkeys in forum
  3. // @namespace     http://www.prohardver.hu/
  4. // @include       http://www.prohardver.hu/*
  5. // @include       http://prohardver.hu/*
  6. // @include       http://mobilarena.hu/*
  7. // @include       http://logout.hu/*
  8. // ==/UserScript==
  9.  
  10. function event_handler()
  11. {
  12.     var is_in_article = function()
  13.     {
  14.         var path = document.location.pathname;
  15.         return path.substr(0, 6) == "/teszt" || path.substr(0, 5) == "/cikk" || path.substr(0, 4) == "/hir";
  16.     }
  17.    
  18.     var get_article_pages = function(id, path)
  19.     {
  20.         var obj = document.getElementById(id);
  21.         if (obj == null)
  22.             return null;
  23.        
  24.         return document.evaluate(path, obj, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  25.     }
  26.    
  27.     var go_to_forum = function(in_new_window)
  28.     {
  29.         if (typeof in_new_window == "boolean" && in_new_window)
  30.             window.open("/forum/index.html");
  31.         else if (document.location.pathname != "/forum/index.html")
  32.             document.location.href = "/forum/index.html";
  33.     };
  34.    
  35.     var go_to_forum_in_new_window = function()
  36.     {
  37.         go_to_forum(true);
  38.     }
  39.    
  40.     var go_to_private_messages = function(in_new_window)
  41.     {
  42.         if (typeof in_new_window == "boolean" && in_new_window)
  43.             window.open("/privatok/listaz.php");
  44.         else if (document.location.pathname != "/privatok/listaz.php")
  45.             document.location.href = "/privatok/listaz.php";
  46.     };
  47.    
  48.     var go_to_private_messages_in_new_window = function()
  49.     {
  50.         go_to_private_messages(true);
  51.     }
  52.    
  53.     var go_to_article_page = function(target)
  54.     {
  55.         var pages = get_article_pages("toc1", "div[1]/ul/li/a");
  56.         if (pages == null)
  57.             pages = get_article_pages("navi_top_pages", "div[1]/div[1]/ul/li/a");
  58.        
  59.         if (pages == null)
  60.             return;
  61.        
  62.         var count = pages.snapshotLength;
  63.         var index = null;
  64.         if (target == "first" && count > 0)
  65.         {
  66.             index = 0;
  67.         }
  68.         else if (target == "last" && count > 0)
  69.         {
  70.             index = count - 1;
  71.         }
  72.         else
  73.         {
  74.             var current = null;
  75.             var href = window.location.href;
  76.             for (var n = 0; n < count; n++)
  77.             {
  78.                 if (pages.snapshotItem(n).href == href)
  79.                 {
  80.                     current = n;
  81.                     break;
  82.                 }
  83.             }  
  84.                
  85.             if (current != null)
  86.             {
  87.                 if (target == "next" && current < count - 1)
  88.                     index = current + 1;
  89.                 else if (target == "prev" && current > 0)
  90.                     index = current - 1;
  91.             }
  92.         }
  93.        
  94.         if (index != null)
  95.         {
  96.             href = pages.snapshotItem(index).href;
  97.             if (href != document.location.href)
  98.                 document.location.href = href;
  99.         }
  100.     }
  101.    
  102.     var go_to_prev_page = function ()
  103.     {
  104.         if (is_in_article())
  105.         {
  106.             go_to_article_page("prev");
  107.             return;
  108.         }
  109.  
  110.         var prev = document.evaluate("//img[@src=\"/design/arr-prev-norm.gif\"]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  111.         if (prev)
  112.             document.location.href = prev.parentNode.href;
  113.     };
  114.    
  115.     var go_to_next_page = function ()
  116.     {
  117.         if (is_in_article())
  118.         {
  119.             go_to_article_page("next");
  120.             return;
  121.         }
  122.  
  123.         var next = document.evaluate("//img[@src=\"/design/arr-next-norm.gif\"]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  124.         if (next)
  125.             document.location.href = next.parentNode.href;
  126.     };
  127.    
  128.     var go_to_first_page = function()
  129.     {
  130.         if (is_in_article())
  131.         {
  132.             go_to_article_page("first");
  133.             return;
  134.         }
  135.  
  136.         var path = document.location.pathname;
  137.         if (path.substr(0, 7) == "/temak/")
  138.         {
  139.             path = path.substr(0, path.lastIndexOf("/")) + "/listaz.php";
  140.         }
  141.         else
  142.         {
  143.             var block_size = document.evaluate("//div[@id=\"navi_top_prefs\"]/div[1]/a/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  144.             if (block_size == null)
  145.                 return;
  146.             block_size = block_size.innerText * 1;
  147.             path = path.substr(0, path.lastIndexOf("/")) + "/hsz_1-" + block_size + ".html";
  148.         }
  149.        
  150.         if (path != document.location.pathname)
  151.             document.location.pathname = path;
  152.     };
  153.    
  154.     var go_to_last_page = function()
  155.     {
  156.         if (is_in_article())
  157.         {
  158.             go_to_article_page("last");
  159.             return;
  160.         }
  161.  
  162.         var path = document.location.pathname;
  163.         var is_in_topic_list = path.substr(0, 7) == "/temak/";
  164.         var last = document.evaluate("//div[@id=\"navi_top_pages\"]/div[1]/div[1]/ul/li[last()]/a/span[2]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  165.         var block_size = document.evaluate("//div[@id=\"navi_top_prefs\"]/div[1]/a/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  166.         if (last == null || block_size == null)
  167.             return;
  168.         last = last.innerText * 1;
  169.         block_size = block_size.innerText * 1;
  170.         last -= (last % block_size);
  171.         if (is_in_topic_list)
  172.             path = path.substr(0, path.lastIndexOf("/")) + "/listaz.php?offset=" + last;
  173.         else
  174.             path = path.substr(0, path.lastIndexOf("/")) + "/hsz_" + (last + 1) + "-" + (last + block_size) + ".html";
  175.         if (path != document.location.pathname)
  176.             document.location.pathname = path;
  177.     };
  178.  
  179.     var hotkeys =
  180.     {
  181.         "ctrl-alt-f":   go_to_forum,
  182.         "ctrl-shift-f": go_to_forum_in_new_window,
  183.         "ctrl-alt-p":   go_to_private_messages,
  184.         "ctrl-shift-p": go_to_private_messages_in_new_window,
  185.         "ctrl-alt-2":   go_to_prev_page,
  186.         "ctrl-alt-3":   go_to_next_page,
  187.         "ctrl-alt-1":   go_to_first_page,
  188.         "ctrl-alt-4":   go_to_last_page
  189.     };
  190.    
  191.     keylib_initialize(window, hotkeys);
  192. }
  193.  
  194. if (document.body)
  195.     event_handler();
  196. else
  197.     window.addEventListener("load", event_handler, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement