View difference between Paste ID: tShv5zX7 and n9WYDC6d
SHOW: | | - or go back to the newest paste.
1
// ==UserScript==
2
// @name       Detect new comments on Hashtable's blog
3
// @namespace  http://h16free.pltplp.net/
4-
// @version    0.1
4+
// @version    0.2
5-
// @description  enter something useful
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-
	    var cookieName = 'post' + articleId;
40+
        // Get comments' page
41
        var hr = jQ('article.comment-block:first header a').attr('href');
42-
	    var cookie = readCookie(cookieName);
42+
        var commentPage = parseInt(hr.replace(/#.*/, '').replace(/^.*comment-page-/, ''), 10);
43
	    var cookieName = 'lastVisits';
44-
	        var lastViewed = parseInt(cookie, 10);
44+
45
	    var cookie = readCookie('lastVisits');
46-
	            var t = new Date(jQ(this).attr('datetime'))
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-
	    }
52+
        var lastViewed = lastVisits[key];
53-
	    document.cookie = cookieName + '=' + (new Date().getTime()) + ';path=/;expires=Tue, 31 Dec 2019 23:00:00 GMT';
53+
        if (lastViewed !== undefined) {
54
            // Mark as new
55
	        jQ('article.comment-block > header > a > time').each(function() {
56
	            var t = new Date(jQ(this).attr('datetime'));
57
	            t = t.getTime() - 7200000; // Fix timezone
58
	            if (t > lastViewed) {
59
	                jQ(this).parents('article').parent('li').css('outline', '2px dotted red');
60
	            }
61
	        });
62
        }
63
        lastVisits[key] = new Date().getTime();
64
        // Set cookie for next time
65
	    document.cookie = cookieName + '=' + JSON.stringify(lastVisits) + ';path=/;expires=Tue, 31 Dec 2019 23:00:00 GMT';
66
    }
67
}
68
69
// load jQuery and execute the main function
70
addJQuery(main);