Guest User

Root topic url

a guest
Jan 24th, 2014
94
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     *root.cz*
  5. // @version     1.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, 'root.cz');
  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.  * @param {String} [domain]     Cookie domain
  57.  */
  58. rtu.cookies.set = function(name, value, expiration, domain) {
  59.     var date = new Date();
  60.     date.setDate(date.getDate() + expiration);
  61.     expiration = !expiration ? '' : ';expires=' + date.toUTCString();
  62.     domain = ';domain=' + (domain || document.domain);
  63.     document.cookie = name + '=' + escape(value) + expiration + domain;
  64. };
  65.  
  66. /**
  67.  * Get cookie value
  68.  *
  69.  * @param  {String} name Cookie name
  70.  *
  71.  * @return {String}      Cookie value
  72.  */
  73. rtu.cookies.get = function(name) {
  74.     if (document.cookie.length) {
  75.         var start = document.cookie.indexOf(name + '=');
  76.         if (start != -1) {
  77.             start += name.length + 1;
  78.             var end = document.cookie.indexOf(';', start);
  79.             end = end == -1 ? document.cookie.length : end;
  80.             return unescape(document.cookie.substring(start, end));
  81.         }
  82.     }
  83.     return null;
  84. };
  85.  
  86. /**
  87.  * Load script
  88.  *
  89.  * @param {String}   src      Script path
  90.  * @param {Function} callback Function to call after script is loaded
  91.  */
  92. rtu.load = function(src, callback) {
  93.     var element = document.createElement('script');
  94.     element.setAttribute('src', src);
  95.     element.addEventListener('load', callback);
  96.     document.body.appendChild(element);
  97. };
  98.  
  99. // Initialization
  100. rtu.load('http://code.jquery.com/jquery-1.10.2.min.js', rtu.main);
Advertisement
Add Comment
Please, Sign In to add comment