forked from Tiny-Giant/myuserscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStickyVoteControls.user.js
49 lines (40 loc) · 1.68 KB
/
StickyVoteControls.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
// ==UserScript==
// @name Sticky vote controls
// @namespace http://github.com/TinyGiant/
// @version 1.0.0.3
// @description Brings back the experimental sticky vote controls.
// @author @TinyGiant
// @include /^https?://(?!chat)\w*.?(stackoverflow|stackexchange|serverfault|superuser|askubuntu|stackapps)\.com/.*/
// @grant none
// ==/UserScript==
/* jshint esnext: true */
(function(){
"use strict";
const 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.run = () => {
const votes = $('.question .vote:not(.sticky), .answer .vote:not(.sticky)');
const cells = $('.question .votecell:not(.sticky), .answer .votecell:not(.sticky)');
votes.css({'position':'absolute', 'z-index':'100'}).addClass('sticky');
cells.css({'width':'46px'}).addClass('sticky');
votes.each(function(i,el){
el = $(el);
var postheight = el.parent().next().find('.post-text').height();
if(el.height() >= postheight) return;
var parent = el.parent();
$(document).scroll(window.requestAnimationFrame.bind(null,function() {
if(document.body.scrollTop > parent.offset().top) document.body.scrollTop < parent.offset().top + parent.height() - el.height() && el.css({'top':document.body.scrollTop}); else el.css({'top':''});
}));
});
};
funcs.run();
funcs.addXHRListener(funcs.run);
})();