Advertisement
Guest User

Untitled

a guest
Nov 19th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name       Detect new comments on Hashtable's blog
  3. // @namespace  http://h16free.pltplp.net/
  4. // @version    0.3
  5. // @description Detect new comments since last visit on h16free.com blog. This version supports comments on multiple pages
  6. // @match      http://h16free.com/*
  7. // @copyright  2013, Etienne Bernard
  8. // ==/UserScript==
  9.  
  10. // a function that loads jQuery and calls a callback function when jQuery has finished loading
  11. function addJQuery(callback) {
  12.     var script = document.createElement("script");
  13.     script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
  14.     script.addEventListener('load', function() {
  15.         var script = document.createElement("script");
  16.         script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
  17.         document.body.appendChild(script);
  18.     }, false);
  19.     document.body.appendChild(script);
  20. }
  21.  
  22. // the guts of this userscript
  23. function main() {
  24.    
  25.     var cookies;
  26.     var readCookie = function(name,c,C,i){
  27.         if(cookies){ return cookies[name]; }
  28.         c = document.cookie.split('; ');
  29.         cookies = {};
  30.         for(i=c.length-1; i>=0; i--){
  31.             C = c[i].split('=');
  32.             cookies[C[0]] = C[1];
  33.         }
  34.         return cookies[name];
  35.     };
  36.    
  37.     var articleId = jQ('article.entry-plain').attr('id');
  38.     if (articleId) {
  39.         articleId = articleId.substr(5);
  40.         // Get comments' page
  41.         var hr = jQ('article.comment-block:first header a').attr('href');
  42.         var commentPage = parseInt(hr.replace(/#.*/, '').replace(/^.*comment-page-/, ''), 10);
  43.         var cookieName = 'lastVisits';
  44.         // Check if we have a cookie for this article
  45.         var cookie = readCookie('lastVisits');
  46.         var lastVisits = {};
  47.         if (cookie !== undefined) {
  48.             lastVisits = JSON.parse(cookie);
  49.         }
  50.         // Get last visit time
  51.         var key = 'p' + articleId + '-' + commentPage;
  52.         var lastViewed = lastVisits[key];
  53.         if (lastViewed !== undefined) {
  54.             // Mark as new
  55.             var count = 1;
  56.             jQ('article.comment-block > header > a > time').each(function() {
  57.                 var t = new Date(jQ(this).attr('datetime'));
  58.                 t = t.getTime() - 3600000; // Fix timezone. TODO: THIS IS UGLY AND DOES NOT WORK WELL. FIX THIS
  59.                 if (t > lastViewed) {
  60.                     var article = jQ(this).parents('article');
  61.                     article.attr("tabindex", "" + count++);
  62.                     article.parent('li').css('outline', '2px dotted red');
  63.                 }
  64.             });
  65.         }
  66.         lastVisits[key] = new Date().getTime();
  67.         // Set cookie for next time
  68.         document.cookie = cookieName + '=' + JSON.stringify(lastVisits) + ';path=/;expires=Tue, 31 Dec 2019 23:00:00 GMT';
  69.     }
  70. }
  71.  
  72. // load jQuery and execute the main function
  73. addJQuery(main);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement