MyBB Community Forums

Full Version: View Unread Posts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
I would like to add a browser tab counter of the same unread posts counter. So for example:

When the unread post counter is 0 the browser tab is just the regular title such as
Quote:title
but when the unread posts counter is 1, i would like the browser tab to update as well to
Quote:(1) title

from basically here https://webdesign.tutsplus.com/tutorials/how-to-display-update-notifications-in-the-browser-tab--cms-23458

I tried modifying the plugins javascript file but i suck at js

    updateCounter: function() {
        if (!unreadPosts.enable) {
            return;
        }

        $.get( "xmlhttp.php?action=unreadPosts_getUnreads&fid" + unreadPosts.fid, function( data ) {
            $("#unreadCounter").replaceWith(data);
        });

       // var title = document.title;
        //var newTitle = '(' + unreadPosts.fid + ') ' + title;
        //document.title = newTitle;

        if (unreadPosts.timeout) clearTimeout(unreadPosts.timeout);
        unreadPosts.timeout = setTimeout('unreadPosts.updateCounter()', unreadPosts.interval * 1000);
    },
but i cant find where to obtain the actual counter variable, and this constantly adds a new digit every time it updates. Can you explain how to modify the plugin to do this?
This counter variable id "data" from GET request (ajax). You should change title inside that callback function where I updated #unreadCounter. It's not clean integer value, but you can parse reply data using javascript.
(2017-04-17, 05:52 PM)lukasamd Wrote: [ -> ]This counter variable id "data" from GET request (ajax). You should change title inside that callback function where I updated #unreadCounter. It's not clean integer value, but you can parse reply data using javascript.

So i stripped off the data to only show the counter
var unreadPosts = {

    timeout:    false,
    interval:   10,
    enable:     false,
    fid:        0,
    hide:       false,

    updateCounter: function() {
        if (!unreadPosts.enable) {
            return;
        }       
    
        $.get( "xmlhttp.php?action=unreadPosts_getUnreads&fid" + unreadPosts.fid, function( data ) {
            $("#unreadCounter").replaceWith(data);

        var d = data.split('<!--')[2] //strip non-counter
        var d = d.split('-->')[1] //strip to only counter
        var d = d.slice(3, -2); //remove extra paren and get only (number)
        var title = document.title;
        var newTitle = '(' + d + ') ' + title;
        document.title = newTitle;

        });

Now i have 2 problems after this.
  • This only works in Firefox, not chrome
  • Every X seconds it adds a new number. I tried just hard coding the name "Python Forum" instead of getting the old title, but then it just would not add the counter at all.

Look at your code. It's ok because you add counter to old title (and old title has counter after first refresh). You must change that logic. I think good idea it's to save title in global variable in the beginning. Question about chrome - not sure, check console for any errors.
the only way i could get it to work is hard code the title name in. However after that issue is fixed, then it shows (0) in the title when there is no unread posts? I tried adding a clause but it doesnt show anything after adding that
var unreadPosts = {

    timeout:    false,
    interval:   10,
    enable:     false,
    fid:        0,
    hide:       false,

    updateCounter: function() {
        if (!unreadPosts.enable) {
            return;
        }

        $.get( "xmlhttp.php?action=unreadPosts_getUnreads&fid" + unreadPosts.fid, function( data ) {
            $("#unreadCounter").replaceWith(data);

        var d = data.split('<!--')[2]; //strip non-counter
        var d = d.split('-->')[1]; //strip to only counter
        var d = d.slice(3, -2); //remove extra paren
        var title = document.title;
       // var title = "Python Forum";
        //if title.startsWtih('('){
        //    var title = title.split(')')[1] //remove extra num if there is one
        //}
         
        var title = title.split(" ").splice(1,-2);
        var newTitle = '(' + d + ') ' + "Python Forum";
        if (title.startsWith("(0)")) {
            document.title = "Python Forum";
        }   
        else if (title.startsWith('(')){
            document.title = newTitle;
        }


        });
        if (unreadPosts.timeout) clearTimeout(unreadPosts.timeout);
        unreadPosts.timeout = setTimeout('unreadPosts.updateCounter()', unreadPosts.interval * 1000);
    },
};




Regarding Chrome, i found the problem to be of needing to clear the cache.

EDIT:
actually i might of hacked together something that worked. Not the best im sure.
var unreadPosts = {

    timeout:    false,
    interval:   10,
    enable:     false,
    fid:        0,
    hide:       false,

    updateCounter: function() {
        if (!unreadPosts.enable) {
            return;
        }

        $.get( "xmlhttp.php?action=unreadPosts_getUnreads&fid" + unreadPosts.fid, function( data ) {
            $("#unreadCounter").replaceWith(data);

        var d = data.split('<!--')[2] //strip non-counter
        var d = d.split('-->')[1] //strip to only counter
        var d = d.slice(3, -2); //remove extra paren
        var title = document.title;
       // var title = "Python Forum";
        //if title.startsWtih('('){
        //    var title = title.split(')')[1] //remove extra num if there is one
        //}
         

        //var title = title.split(" ").splice(1,-2)

        if (d != "0"){
            var newTitle = '(' + d + ') ' + "Python Forum";
            document.title = newTitle;
        }
        else{
            document.title = "Python Forum";
        }


        });
        if (unreadPosts.timeout) clearTimeout(unreadPosts.timeout);
        unreadPosts.timeout = setTimeout('unreadPosts.updateCounter()', unreadPosts.interval * 1000);
    },
};

I would like to modify this even further and add a User CP option to enable or disable the browser tab counter. Would you know how to do this? This would be the ideal solution. 

Another simpler option would be to make a "browser tab" usergroup and add people that want it in that group....but how would you code that if condition usergroup in javascript?
Hello!

Can I mark all forums read for ALL users? I installed this plugin and all users have 500+ posts for read... XDD
No, it's not possible.
And with MySQL? neither? Confused

Thanks for your reply! Smile
Use SQL... yes, it's possible.

UPDATE mybb_users SET lastmark = UNIX_TIMESTAMP()
(2017-05-11, 10:07 AM)lukasamd Wrote: [ -> ]Use SQL... yes, it's possible.

UPDATE mybb_users SET lastmark = UNIX_TIMESTAMP()

Thanks you!

And last question, this is compatible with PHP 7.0?
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49