Guest User

Root topic url

a guest
Jan 24th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Root topic url
  3. // @namespace   root
  4. // @include     */forum.root.cz/*
  5. // @version     1
  6. // @grant       none
  7. // ==/UserScript==
  8.  
  9. /** @namespace Root topic url */
  10. var rtu = {};
  11.  
  12. /** Main function */
  13. rtu.main = function() {
  14.  
  15.     // Load read topic links list
  16.     var read = rtu.cookies.get('root_read_topics');
  17.     read = read ? read.split(',') : [];
  18.  
  19.     // Jump to first post on unread topics
  20.     $('a[href*="?topic="]').each(function() {
  21.         var $link = $(this);
  22.         var first = rtu.first($link);
  23.         if (read.indexOf(first) == -1)
  24.             $link.attr('href', first);
  25.     });
  26.  
  27.     // Add read topic to list
  28.     $('a[href*="?topic="]:not([href*=".msg"])').on('click', function() {
  29.         if (read.length == 100)
  30.             read.splice(0, 1);
  31.         read.push(rtu.first(this));
  32.         rtu.cookies.set('root_read_topics', read.toString(), 30);
  33.     });
  34. };
  35.  
  36. /**
  37.  * Modify url to jump to first post
  38.  *
  39.  * @param  {Object} link Topic link
  40.  *
  41.  * @return {String}
  42.  */
  43. rtu.first = function(link) {
  44.     return $(link).attr('href').split('.msg')[0];
  45. };
  46.  
  47. /** @namespace Cookies */
  48. rtu.cookies = {};
  49.  
  50. /**
  51.  * Set cookie value
  52.  *
  53.  * @param {String} name         Cookie name
  54.  * @param {String} value        Cookie value
  55.  * @param {Number} [expiration] Number of days after which the cookie expires
  56.  */
  57. rtu.cookies.set = function(name, value, expiration) {
  58.     var date = new Date();
  59.     date.setDate(date.getDate() + expiration);
  60.     expiration = !expiration ? '' : ';expires=' + date.toUTCString();
  61.     document.cookie = name + '=' + escape(value) + expiration;
  62. };
  63.  
  64. /**
  65.  * Get cookie value
  66.  *
  67.  * @param   {String} name Cookie name
  68.  *
  69.  * @returns {String}      Cookie value
  70.  */
  71. rtu.cookies.get = function(name) {
  72.     if (document.cookie.length) {
  73.         var start = document.cookie.indexOf(name + '=');
  74.         if (start != -1) {
  75.             start += name.length + 1;
  76.             end = document.cookie.indexOf(';', start);
  77.             if (end == -1)
  78.                 end = document.cookie.length;
  79.             return unescape(document.cookie.substring(start, end));
  80.         }
  81.     }
  82.     return null;
  83. };
  84.  
  85. /**
  86.  * Load script
  87.  *
  88.  * @param {String}   src      Script path
  89.  * @param {Function} callback Function to call after script is loaded
  90.  */
  91. rtu.load = function(src, callback) {
  92.     var element = document.createElement('script');
  93.     element.setAttribute('src', src);
  94.     element.addEventListener('load', callback);
  95.     document.body.appendChild(element);
  96. };
  97.  
  98. // Initialization
  99. rtu.load('http://code.jquery.com/jquery-1.10.2.min.js', rtu.main);
Advertisement
Add Comment
Please, Sign In to add comment