-
Notifications
You must be signed in to change notification settings - Fork 8
/
showtimeline.user.js
52 lines (48 loc) · 1.65 KB
/
showtimeline.user.js
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
50
51
52
// ==UserScript==
// @name Show Timeline
// @namespace https://github.com/Tunaki/stackoverflow-userscripts
// @version 0.3
// @description Adds an anchor below posts that links to their timeline
// @author Tunaki
// @include /^https?:\/\/\w*.?(stackexchange.com|stackoverflow.com|serverfault.com|superuser.com|askubuntu.com|stackapps.com|mathoverflow.net)\/.*/
// @grant none
// ==/UserScript==
function addXHRListener(callback) {
let open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
this.addEventListener('load', callback.bind(null, this), false);
open.apply(this, arguments);
};
};
function addTimelineLink(postId) {
var $posts;
if(!postId) {
$posts = $('.post-menu');
} else {
$posts = $('[data-questionid="' + postId + '"] .post-menu,[data-answerid="' + postId + '"] .post-menu');
}
$posts.each(function() {
var $this = $(this);
var postId = $this.find('a.short-link').attr('id').split('-')[2];
$this.append($('<span>').attr('class', 'lsep').html('|'));
$this.append($('<a>').attr({
'class': 'timeline-link',
'href': '/posts/' + postId + '/timeline'
}).html('timeline'));
});
};
addXHRListener(function(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;
}
}
addTimelineLink(matches[1]);
}
});
$(document).ready(function() {
addTimelineLink();
});