Skip to content

Instantly share code, notes, and snippets.

@tung
Created June 11, 2012 04:42
Show Gist options
  • Save tung/2908537 to your computer and use it in GitHub Desktop.
Save tung/2908537 to your computer and use it in GitHub Desktop.
SourceForge Trac float diff format switcher with page scroll
// Set to autorun on sourceforge\.net\/.*/trac/.*/changeset/
(function ($) {
var mods = $("#file-legend .mod + dd");
if (mods.length == 0) return;
var modsLen = parseInt(mods[0].textContent);
if (!modsLen) return;
// Cache diff dimensions.
var tops = new Array(modsLen);
var bottoms = new Array(modsLen);
var switchers = new Array(modsLen);
$("ul.entries li.entry").each(function (i, e) {
var entry = $(e);
var top = entry.offset().top;
tops[i] = top;
bottoms[i] = top + entry.height();
switchers[i] = entry.find(".switch").first();
});
var fixstyle = $("<style>.switchfix { position: fixed; right: 23px; top: 5px; }</style>");
fixstyle.appendTo("head");
// Create page scroll detector.
var scrolldet = $("<div class='scrolldet'></div>");
scrolldet.css({
position: 'fixed', right: '0px', top: '0px'
});
scrolldet.prependTo("body");
$(window).scroll(function () {
var pos = scrolldet.offset().top;
for (var i = 0; i < modsLen; i++) {
if (pos >= tops[i] && pos < bottoms[i])
switchers[i].addClass("switchfix");
else
switchers[i].removeClass("switchfix");
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment