Created
March 6, 2016 23:53
-
-
Save Tiny-Giant/f372828d044bcd692c9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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'; | |
var funcs = {}; | |
funcs.addXHRListener = function(callback) | |
{ | |
var open = XMLHttpRequest.prototype.open; | |
XMLHttpRequest.prototype.open = function() | |
{ | |
this.addEventListener('load', callback.bind(null, this), false); | |
open.apply(this, arguments); | |
}; | |
}; | |
funcs.addTimelineLink = function(postid) | |
{ | |
var 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 (var i in posts) | |
{ | |
var post = posts[i]; | |
var timelinelink = post.querySelector('.timeline-link'); | |
if (timelinelink !== null) | |
{ | |
continue; | |
} | |
var _postid = (/\d+/.exec(post.querySelector('a.short-link')) || [false])[0]; | |
if (!_postid) | |
{ | |
continue; | |
} | |
var lsep = document.createElement('span'); | |
lsep.className = 'lsep'; | |
lsep.textContent = '|'; | |
post.appendChild(lsep); | |
var link = document.createElement('a'); | |
link.className = 'timeline-link'; | |
link.textContent = 'timeline'; | |
link.href = '/posts/' + _postid + '/timeline'; | |
link.target = '_blank'; | |
post.appendChild(link); | |
} | |
}; | |
window.addEventListener('load', function(event) | |
{ | |
funcs.addTimelineLink(); | |
}, false); | |
funcs.addXHRListener(function(xhr) | |
{ | |
if (/ajax-load-realtime/.test(xhr.responseURL)) | |
{ | |
var 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