Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Root topic url
- // @namespace root
- // @include */forum.root.cz/*
- // @version 1
- // @grant none
- // ==/UserScript==
- /** @namespace Root topic url */
- var rtu = {};
- /** Main function */
- rtu.main = function() {
- // Load read topic links list
- var read = rtu.cookies.get('root_read_topics');
- read = read ? read.split(',') : [];
- // Jump to first post on unread topics
- $('a[href*="?topic="]').each(function() {
- var $link = $(this);
- var first = rtu.first($link);
- if (read.indexOf(first) == -1)
- $link.attr('href', first);
- });
- // Add read topic to list
- $('a[href*="?topic="]:not([href*=".msg"])').on('click', function() {
- if (read.length == 100)
- read.splice(0, 1);
- read.push(rtu.first(this));
- rtu.cookies.set('root_read_topics', read.toString(), 30);
- });
- };
- /**
- * Modify url to jump to first post
- *
- * @param {Object} link Topic link
- *
- * @return {String}
- */
- rtu.first = function(link) {
- return $(link).attr('href').split('.msg')[0];
- };
- /** @namespace Cookies */
- rtu.cookies = {};
- /**
- * Set cookie value
- *
- * @param {String} name Cookie name
- * @param {String} value Cookie value
- * @param {Number} [expiration] Number of days after which the cookie expires
- */
- rtu.cookies.set = function(name, value, expiration) {
- var date = new Date();
- date.setDate(date.getDate() + expiration);
- expiration = !expiration ? '' : ';expires=' + date.toUTCString();
- document.cookie = name + '=' + escape(value) + expiration;
- };
- /**
- * Get cookie value
- *
- * @param {String} name Cookie name
- *
- * @returns {String} Cookie value
- */
- rtu.cookies.get = function(name) {
- if (document.cookie.length) {
- var start = document.cookie.indexOf(name + '=');
- if (start != -1) {
- start += name.length + 1;
- end = document.cookie.indexOf(';', start);
- if (end == -1)
- end = document.cookie.length;
- return unescape(document.cookie.substring(start, end));
- }
- }
- return null;
- };
- /**
- * Load script
- *
- * @param {String} src Script path
- * @param {Function} callback Function to call after script is loaded
- */
- rtu.load = function(src, callback) {
- var element = document.createElement('script');
- element.setAttribute('src', src);
- element.addEventListener('load', callback);
- document.body.appendChild(element);
- };
- // Initialization
- rtu.load('http://code.jquery.com/jquery-1.10.2.min.js', rtu.main);
Advertisement
Add Comment
Please, Sign In to add comment