14

Often when I follow a question, I had like to see the updates.

So I have read a few comments (expanded the comments section since they are too many), then I hit Refresh in my browser to see the updated close votes, edits, updated comment votes, answers in the right order, etc.

The problem is: when I hit Refresh, I have to manually expand the comment section again, which is a bad user experience in my opinion. Can we make the page remember I expanded the comments, so I don't have to hit the show more comments link again?

4
+50

It would be nice if this gets implemented and that shouldn't be to difficult as all bits that are needed can be found in the current javascript.

Until that moment (the famous 6 to 8 weeks) you can use the following TamperMoney/GreaseMonkey script:

// ==UserScript==
// @name         Expand comments
// @namespace    http://stackoverflow.com/users/578411/rene
// @version      0.1
// @description  expand comments based on localStorage
// @author       rene
// @match        http://stackoverflow.com/questions/*
// @grant        none
// ==/UserScript==

(function ($, localStorage){
    // find all show comments link 
    // so we can iterate over them
    $('a.js-show-link.comments-link').each(function() {
        // use the id of the parent div as key in 
        // local storage i.e. comments-link-28475383
        var pid = $(this).parents('div').attr('id'),
            expand = localStorage.getItem(pid),
            _this = $(this);
        if (expand !== null) {
            // we found our id, let's expand comments
            // use stackexchange provided functionality
            StackExchange.ready(function () {
                // From the dev stack js script
                // StackExchange.comments.loadAll(elem) can be called to fetch missing comments
                // from the server. elem should be a jQuery object or anything that can be passed
                // to $(...), and should be something (anything, really) within a question or answer.
                StackExchange.comments.loadAll(_this);
            });
        } else {
            // not found yet so let's subscribe on click events
            $(this).on('click', function (e) {
                // get the parent id from the div i.e comments-link-28475383
                var parentid =  e.target.parentElement.id;
                // store the date (so we can ignore expand request after 3 days or so)
                localStorage.setItem(parentid, new Date());
            });
        }
    });
}($ || unsafeWindow.$, window.localStorage || unsafeWindow.localStorage ))  // for Firefox use unsafeWindow

I decided for once to add some comments to my code so I think this is self-explanatory. The page behaves like normal but if you ever click on a show comment link it stores a key in localStorage and on page load it checks for that key to re-load all the comments again. Notice that I used the function StackExchange.comments.loadAll which I found when I checked the dev version of the full.js (and that one comes with comments)

So far tested on Google Chrome 40.0 with TamperMonkey and Firefox 35.0.1 with GreaseMonkey, both running on Win7.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .