Advertisement
Guest User

ph_hotkeys_in_forum.user.js

a guest
May 22nd, 2013
74
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. // @include   http://prohardver.hu/*
  4. // @include   http://mobilarena.hu/*
  5. // @include   http://logout.hu/*
  6. // @include   http://itcafe.hu/*
  7. // @include   http://gamepod.hu/*
  8. // @include   http://hardverapro.hu/*
  9. // @version   1.1
  10. // ==/UserScript==
  11.  
  12. (function()
  13. {
  14.     var event_handler = function()
  15.     {
  16.         var is_old_style = document.location.host == "logout.hu" || document.location.host == "hardverapro.hu";
  17.        
  18.         var is_in_article = function()
  19.         {
  20.             var path = document.location.pathname;
  21.             return path.substr(0, 6) == "/teszt" || path.substr(0, 5) == "/cikk" || path.substr(0, 4) == "/hir";
  22.         }
  23.        
  24.         var is_in_list = function()
  25.         {
  26.             var path = document.location.pathname.substr(0, 7);
  27.             return path == "/temak/" || path == "/aprok/";
  28.         }
  29.  
  30.         var get_block_size = function()
  31.         {
  32.             try
  33.             {
  34.                 if (is_old_style)
  35.                 {
  36.                     var text = document.evaluate("//select[@name=\"listsett\"]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.options[0].text;
  37.                     var n = text.indexOf(" ");
  38.                     if (n == -1)
  39.                         return null;
  40.                     return text.substr(0, n) * 1;
  41.                 }
  42.                 else
  43.                 {
  44.                     return document.evaluate("//div[@id=\"navi_top_prefs\"]/div[1]/a/b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerText * 1;
  45.                 }
  46.             }
  47.             catch (e)
  48.             {
  49.                 return null;
  50.             }
  51.         }
  52.        
  53.         var get_article_pages = function(id, path)
  54.         {
  55.             var obj = document.getElementById(id);
  56.             if (obj == null)
  57.                 return null;
  58.            
  59.             return document.evaluate(path, obj, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  60.         }
  61.        
  62.         var go_to_forum = function(in_new_window)
  63.         {
  64.             if (typeof in_new_window == "boolean" && in_new_window)
  65.                 window.open("/forum/index.html");
  66.             else if (document.location.pathname != "/forum/index.html")
  67.                 document.location.href = "/forum/index.html";
  68.         };
  69.        
  70.         var go_to_forum_in_new_window = function()
  71.         {
  72.             go_to_forum(true);
  73.         }
  74.        
  75.         var go_to_private_messages = function(in_new_window)
  76.         {
  77.             if (typeof in_new_window == "boolean" && in_new_window)
  78.                 window.open("/privatok/listaz.php");
  79.             else if (document.location.pathname != "/privatok/listaz.php")
  80.                 document.location.href = "/privatok/listaz.php";
  81.         };
  82.        
  83.         var go_to_private_messages_in_new_window = function()
  84.         {
  85.             go_to_private_messages(true);
  86.         }
  87.        
  88.         var go_to_article_page = function(target)
  89.         {
  90.             if (is_old_style)
  91.             {
  92.                 try
  93.                 {
  94.                     var pages = document.evaluate("//div[@class=\"toc\"]/form/select", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.options;
  95.                     var count = pages.length;
  96.                     var index = null;
  97.                     if (target == "first" && count > 0)
  98.                     {
  99.                         index = 0;
  100.                     }
  101.                     else if (target == "last" && count > 0)
  102.                     {
  103.                         index = count - 1;
  104.                     }
  105.                     else
  106.                     {
  107.                         var current = pages.selectedIndex;
  108.                         if (target == "next" && current < count - 1)
  109.                             index = current + 1;
  110.                         else if (target == "prev" && current > 0)
  111.                             index = current - 1;
  112.                     }
  113.                    
  114.                     if (index == null)
  115.                         return;
  116.  
  117.                     var path = document.location.pathname;
  118.                     path = path.substr(0, path.lastIndexOf("/") + 1) + pages[index].value + ".html";
  119.                     if (path != document.location.pathname)
  120.                         document.location.pathname = path;
  121.                 }
  122.                 catch (e)
  123.                 {
  124.                 }
  125.             }
  126.             else
  127.             {
  128.                 var pages = get_article_pages("toc1", "div[1]/ul/li/a");
  129.                 if (pages == null)
  130.                     pages = get_article_pages("navi_top_pages", "div[1]/div[1]/ul/li/a");
  131.                 if (pages == null)
  132.                     return;
  133.                
  134.                 var count = pages.snapshotLength;
  135.                 var index = null;
  136.                 if (target == "first" && count > 0)
  137.                 {
  138.                     index = 0;
  139.                 }
  140.                 else if (target == "last" && count > 0)
  141.                 {
  142.                     index = count - 1;
  143.                 }
  144.                 else
  145.                 {
  146.                     var current = null;
  147.                     var href = window.location.href;
  148.                     for (var n = 0; n < count; n++)
  149.                     {
  150.                         if (pages.snapshotItem(n).href == href)
  151.                         {
  152.                             current = n;
  153.                             break;
  154.                         }
  155.                     }  
  156.                        
  157.                     if (current != null)
  158.                     {
  159.                         if (target == "next" && current < count - 1)
  160.                             index = current + 1;
  161.                         else if (target == "prev" && current > 0)
  162.                             index = current - 1;
  163.                     }
  164.                 }
  165.                
  166.                 if (index != null)
  167.                 {
  168.                     href = pages.snapshotItem(index).href;
  169.                     if (href != document.location.href)
  170.                         document.location.href = href;
  171.                 }
  172.             }
  173.         }
  174.        
  175.         var go_to_prev_page = function ()
  176.         {
  177.             if (is_in_article())
  178.             {
  179.                 go_to_article_page("prev");
  180.                 return;
  181.             }
  182.  
  183.             var prev = document.evaluate(is_old_style ? "//img[@src=\"/design/arr_prev.gif\"]" : "//img[@src=\"/design/arr-prev-norm.gif\"]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  184.             if (prev)
  185.                 document.location.href = prev.parentNode.href;
  186.         };
  187.        
  188.         var go_to_next_page = function ()
  189.         {
  190.             if (is_in_article())
  191.             {
  192.                 go_to_article_page("next");
  193.                 return;
  194.             }
  195.  
  196.             var next = document.evaluate(is_old_style ? "//img[@src=\"/design/arr_next.gif\"]" : "//img[@src=\"/design/arr-next-norm.gif\"]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  197.             if (next)
  198.                 document.location.href = next.parentNode.href;
  199.         };
  200.        
  201.         var go_to_first_page = function()
  202.         {
  203.             if (is_in_article())
  204.             {
  205.                 go_to_article_page("first");
  206.                 return;
  207.             }
  208.  
  209.             var path = document.location.pathname;
  210.             var search = document.location.search;
  211.             if (is_in_list())
  212.             {
  213.                 path = path.substr(0, path.lastIndexOf("/")) + "/listaz.php";
  214.                 search = "";
  215.             }
  216.             else
  217.             {
  218.                 var block_size = get_block_size();
  219.                 if (block_size == null)
  220.                     return;
  221.                 path = path.substr(0, path.lastIndexOf("/")) + "/hsz_1-" + block_size + ".html";
  222.             }
  223.            
  224.             if (path != document.location.pathname)
  225.                 document.location.pathname = path;
  226.             else if (search != document.location.search)
  227.                 document.location.search = search;
  228.         };
  229.        
  230.         var go_to_last_page = function()
  231.         {
  232.             if (is_in_article())
  233.             {
  234.                 go_to_article_page("last");
  235.                 return;
  236.             }
  237.  
  238.             var path = document.location.pathname;
  239.             var block_size = get_block_size();
  240.             var last;
  241.             try
  242.             {
  243.                 if (is_old_style)
  244.                 {
  245.                     last = document.evaluate("//select[@name=\"ugro\"]/option[last()]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.text;
  246.                     var n = last.lastIndexOf(" ");
  247.                     if (n == -1)
  248.                         return;
  249.                     last = last.substr(n + 1) * 1;
  250.                 }
  251.                 else
  252.                 {
  253.                     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.innerText * 1;
  254.                 }
  255.             }
  256.             catch (e)
  257.             {
  258.                 return;
  259.             }
  260.  
  261.             if (last == null || block_size == null)
  262.                 return;
  263.             last -= (last % block_size);
  264.            
  265.             if (is_in_list())
  266.                 path = path.substr(0, path.lastIndexOf("/")) + "/listaz.php?offset=" + last;
  267.             else
  268.                 path = path.substr(0, path.lastIndexOf("/")) + "/hsz_" + (last + 1) + "-" + (last + block_size) + ".html";
  269.             if (path != document.location.pathname)
  270.                 document.location.pathname = path;
  271.         };
  272.        
  273.         var go_to_comments = function()
  274.         {
  275.             try
  276.             {
  277.                 document.location.href = document.evaluate(is_old_style ? "//a[@name=\"rel_msgs\"]" : "//li[@class=\"oforum\"]/a", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.href;
  278.             }
  279.             catch (e)
  280.             {
  281.             }
  282.         };
  283.  
  284.         var go_to_article = function()
  285.         {
  286.             try
  287.             {
  288.                 document.location.href = document.evaluate(is_old_style ? "//div[@class=\"kapcsanyag\"]/a" : "//div[@class=\"inr\"]/following-sibling::a", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.href;
  289.             }
  290.             catch (e)
  291.             {
  292.             }
  293.         };
  294.  
  295.         var hotkeys =
  296.         {
  297.             "ctrl-alt-f":   go_to_forum,
  298.             "ctrl-shift-f": go_to_forum_in_new_window,
  299.             "ctrl-alt-p":   go_to_private_messages,
  300.             "ctrl-shift-p": go_to_private_messages_in_new_window,
  301.             "ctrl-alt-2":   go_to_prev_page,
  302.             "ctrl-alt-3":   go_to_next_page,
  303.             "ctrl-alt-1":   go_to_first_page,
  304.             "ctrl-alt-4":   go_to_last_page,
  305.             "ctrl-alt-c":   go_to_comments,
  306.             "ctrl-alt-a":   go_to_article
  307.         };
  308.        
  309.         keylib_initialize(window, hotkeys);
  310.     }
  311.  
  312.     if (document.body)
  313.         event_handler();
  314.     else
  315.         window.addEventListener("load", event_handler, false);
  316. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement