Take the 2-minute tour ×
Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. It's 100% free, no registration required.

Inspired by this MSO question.

This tiny userscript will add a link to the timeline for any post, just below the "favorite question" star button.

Example image:

timeline button example

This is the code:

// ==UserScript==
// @name        Add Timeline Button
// @namespace   none
// @description Adds a "View Timeline" Button to StackOverflow
// @match       http://*.askubuntu.com/questions/*
// @match       http://*.onstartups.com/questions/*
// @match       http://*.serverfault.com/questions/*
// @match       http://*.stackapps.com/questions/*
// @match       http://*.stackexchange.com/questions/*
// @match       http://*.stackoverflow.com/questions/*
// @match       http://*.superuser.com/questions/*
// @author      csharpler
// @version     0.2
// ==/UserScript==

/*
 * Version History
 *      * 0.1 (2013-06-11): First release
 *      * 0.2 (2013-06-12): * changed includes
 *                          * improved regex
 *                          - removed unsafeWindow, replaced with function wrapper
 */

withPages_jQuery(run);

function withPages_jQuery (NAMED_FunctionToRun) {
    //--- Use named functions for clarity and debugging...
    var funcText        = NAMED_FunctionToRun.toString ();
    var funcName        = funcText.replace (/^function\s+(\w+)\s*\((.|\n|\r)+$/, "$1");
    var script          = document.createElement ("script");
    script.textContent  = funcText + "\n\n";
    script.textContent += 'jQuery(document).ready( function () {' + funcName + '(jQuery);} );';
    document.body.appendChild (script);
};

function run(){
    var uri = location.href;
    var post = location.pathname.match(/questions\/(\d+)\D/i);
    var newuri = "";
    if(post && post.length > 1){
        newuri = location.protocol + "//" + location.host + "/posts/" + post[1] + "/timeline";
    }
    $(".favoritecount").after("<a href='" + newuri + "'>View Timeline</a>");
}

Feel free to submit any bugs, feature requests and comments on the script.

This script was tested with the following settings:

  • Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:20.0) Gecko/20130520 Firefox/20.0 PaleMoon/20.1-x64
  • SO and MSO pages, other SE pages will probably work (please tell me if they don't!)
  • Greasemonkey 1.9
share|improve this question
1  
Note that there are at least 3 other userscripts that do this. There's mine, below. There's this one from April 2011. And, there's this one from Feb 2011. –  Brock Adams Jun 12 '13 at 19:22
add comment

1 Answer 1

Good idea. There's a few minor niggles, though:

  1. Don't use unsafeWindow, this will break in Chrome and will not even work in Firefox+Greasemonkey if the effective @grant changes.

  2. Avoid leading * wildcards in @includes, if you can help it. This is a big performance hit. @match limits the wildcard scope and performs much better and reports the sites/security correctly on Chrome. This gets all current SE sites:

    // @match       http://*.askubuntu.com/questions/*
    // @match       http://*.onstartups.com/questions/*
    // @match       http://*.serverfault.com/questions/*
    // @match       http://*.stackapps.com/questions/*
    // @match       http://*.stackexchange.com/questions/*
    // @match       http://*.stackoverflow.com/questions/*
    // @match       http://*.superuser.com/questions/*
    
  3. The regex match needs to be more robust, to account for all legal characters in the URL, and for anchors.

  4. Use injection so that the script will work on most userscript-capable browsers (Chrome, Firefox, Opera, Safari, etc.).

  5. Mathoverflow.net is not an SE site! Use math.stackexchange.com for that. mathoverflow.net also does not appear to support the timeline view/page.


Here's a variant of the script I've been using (It works on Chrome and Firefox, and should work on most other userscript-capable browsers):

Easy install URL at GitHub for Greasemonkey, Tampermonkey, Scriptish, etc.

// ==UserScript==
// @name        Stack Exchange: Add Timeline Button to questions
// @description Adds a "View Timeline" Button to Stack Exchange sites
// @namespace   StackExchange
// @match       http://*.askubuntu.com/questions/*
// @match       http://*.onstartups.com/questions/*
// @match       http://*.serverfault.com/questions/*
// @match       http://*.stackapps.com/questions/*
// @match       http://*.stackexchange.com/questions/*
// @match       http://*.stackoverflow.com/questions/*
// @match       http://*.superuser.com/questions/*
// ==/UserScript==

function addTimelineButton ($) {
    var buttonText      = "View Timeline";
    var timelineLink    = "#";
    var postNum         = 0;

    var postMatch   = location.pathname.match (/questions\/(\d+)\D/i);
    if (postMatch  &&  postMatch.length > 1) {
        postNum         = postMatch[1];
        timelineLink    = location.protocol + "//"
                        + location.host
                        + "/posts/"
                        + postNum + "/timeline"
                        ;
    }
    else {
        buttonText  = "Path error 1";
    }

    var targetNode  = $(".favoritecount");
    if (targetNode.length) {
        targetNode.after ('<a href="' + timelineLink + '">' + buttonText + '</a>');
    }
    else if (
        postNum
        &&  $("#content div.subheader > h1:contains('Page Not Found')").length == 0
    ) {
        alert ("From Timeline script:  Unexpected Page layout.");
    }
}

withPages_jQuery (addTimelineButton);

function withPages_jQuery (NAMED_FunctionToRun) {
    //--- Use named functions for clarity and debugging...
    var funcText        = NAMED_FunctionToRun.toString ();
    var funcName        = funcText.replace (/^function\s+(\w+)\s*\((.|\n|\r)+$/, "$1");
    var script          = document.createElement ("script");
    script.textContent  = funcText + "\n\n";
    script.textContent += 'jQuery(document).ready( function () {' + funcName + '(jQuery);} );';
    document.body.appendChild (script);
};
share|improve this answer
    
thanks, I included some of your proposals :) –  pascalhein Jun 12 '13 at 16:15
add comment

You must log in to answer this question.

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