Skip to content

Instantly share code, notes, and snippets.

@Tiny-Giant
Last active March 20, 2016 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tiny-Giant/8dcafe194706bb8a0dec to your computer and use it in GitHub Desktop.
Save Tiny-Giant/8dcafe194706bb8a0dec to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Show Timeline
// @namespace https://github.com/Tunaki/stackoverflow-userscripts
// @version 0.4
// @description Adds an anchor below posts that links to their timeline
// @author Tunaki @TinyGiant
// @include /^https?:\/\/(?!chat)\w*.?(stackexchange.com|stackoverflow.com|serverfault.com|superuser.com|askubuntu.com|stackapps.com|mathoverflow.net)\/.*/
// @run-at document-start
// @grant none
// ==/UserScript==
/* jshint -W097 */
/* jshint esnext:true */
'use strict';
let funcs = {};
funcs.addXHRListener = callback =>
{
let open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function()
{
this.addEventListener('load', callback.bind(null, this), false);
open.apply(this, arguments);
};
};
funcs.addTimelineLink = postid =>
{
let posts;
if(!postid)
{
posts = Array.from(document.querySelectorAll('.post-menu'));
}
else
{
posts = [document.querySelector('[data-questionid="' + postid + '"] .post-menu,[data-answerid="' + postid + '"] .post-menu')];
}
for(let post of posts)
{
const postid = (/\d+/.exec(post.querySelector('a.short-link')) || [false])[0];
if (!postid)
{
continue;
}
const lsep = document.createElement('span');
lsep.className = 'lsep';
lsep.textContent = '|';
post.appendChild(lsep)
const link = document.createElement('a');
link.className = 'timeline-link';
link.textContent = 'timeline';
link.href = '/posts/' + postid + '/timeline';
link.target = '_blank';
post.appendChild(link);
}
}
document.addEventListener('load', event =>
{
funcs.addTimelineLink();
}, false);
funcs.addXHRListener(xhr =>
{
if (/ajax-load-realtime/.test(xhr.responseURL))
{
let matches = /question" data-questionid="(\d+)/.exec(xhr.responseText);
if (matches === null)
{
matches = /answer" data-answerid="(\d+)/.exec(xhr.responseText);
if (matches === null)
{
return;
}
}
funcs.addTimelineLink(matches[1]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment