Voat Enhancement

A simple script to provide some basic enhancement for voat (auto pagination so far).

当前为 2015-06-29 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Voat Enhancement
// @namespace   septus.info
// @include		http://voat.co/*
// @include		http://*.voat.co/*
// @include		https://voat.co/*
// @include		https://*.voat.co/*
// @version     1.0.2
// @grant       none
// @description:en A simple script to provide some basic enhancement for voat. Includes auto-pagination and info bar detachment.
// @description A simple script to provide some basic enhancement for voat (auto pagination so far).
// ==/UserScript==

var InitializeAutoPaginate = function(){
	var isFrontPage = false;
	var subvoat = "";
	var sorting = "";
	var urlHandled = false;

	var match = window.location.href.match(/^https?:\/\/[^\/]+(\/new)?\/?$/);
	if(!urlHandled && match != null){
		//console.log(match)
		isFrontPage= true;
		if(match[1]){
			sorting = match[1];
		}
		urlHandled = true;
	}
	
	match = window.location.href.match(/^https?:\/\/[^\/]+\/v\/([^\/]+)(\/new|\/top)?\/?$/);
	if(!urlHandled && match != null){
		subvoat = match[1];
		if(match[2]){
			sorting = match[2];
		}
		urlHandled = true;
	}
	
	if(!urlHandled){
		return;
	}
	
	var url = "";
	var currentPage = 0;
	var contentLoading = false;
	var morePages = true;
	
	if(! ($("li.btn-whoaverse-paging > a[rel^='next']")[0])){
		morePages = false;
	}
	
	if(isFrontPage){
		url = "/"+sorting;
	}
	else{
		url = "/v/" + subvoat +"/" + sorting;
	}
	//console.log(url);
	$(document).on("scroll", function(event){
		var distanceToNewPage = $("div.pagination-container").offset().top - 
			(window.pageYOffset + $(window).height()) - 600;
		//console.log(distanceToNewPage);
		
		if(distanceToNewPage < 0 && !contentLoading && morePages){
			
			contentLoading = true;
			currentPage++;
			//console.log("Loading page " + currentPage);
			
			$.get( url, {page: currentPage} )
				.done(function( data ) {
					var loadedDocument = $(data);
					var loadedSubmissions = null;
					
					if(isFrontPage){
						loadedSubmissions = loadedDocument.find("div.sitetable > div.submission");
						var paginationContainer = $("div.pagination-container");
						for(var i = 0; i < loadedSubmissions.length; i++){
							paginationContainer.before(loadedSubmissions[i]);
						}
					}
					else{
						loadedSubmissions = loadedDocument.find("div.linklisting > div.submission");
						var linklisting = $("div.linklisting");
						for(var i = 0; i < loadedSubmissions.length; i++){
							linklisting.append(loadedSubmissions[i]);
						}
					}
					
					if(!loadedDocument.find("li.btn-whoaverse-paging > a[rel^='next']")[0]){
						morePages = false;
					}
					contentLoading = false;
				})
				.fail(function(error) {
					console.log( error );
				});
		}
	});
};

var SaveStyles = function(domObject, styleObject){
	for (var property in styleObject) {
		styleObject[property] = $(domObject).css(property);
	}
};

var InitializeDetachedInfoBar = function(){
	var infoBar = $("div#header-account > div:nth-child(1)");
	var infoBarTop = infoBar.offset().top;
	var infoBarRight = $(window).width() - (infoBar.offset().left + infoBar.width());
	var infoBarDetached = false;
	var detachedStyle = {"position": "fixed", "top": "0px", "right": "0px"};
	var originalStyle = {"position": "", "top": "", "right": ""};
	SaveStyles(infoBar, originalStyle);
	
	//console.log(originalStyle);
	var changingState = false;
	$(document).on("scroll", function(event){
		if(!changingState){
			if(!infoBarDetached && window.pageYOffset - infoBarTop > 0){
				changingState = true;
				var animateBeginStyle = jQuery.extend(true, {}, detachedStyle);
				animateBeginStyle["right"] = infoBarRight + "px";
				
				infoBar.css(animateBeginStyle);
				infoBar.animate(detachedStyle, 200, function(){
					infoBarDetached = true;
					changingState = false;
				});
				//console.log("Info bar detached");
			}
			else if(infoBarDetached && window.pageYOffset - infoBarTop <= 0){
				changingState = true;
				var animateEndStyle = jQuery.extend(true, {}, detachedStyle);
				animateEndStyle["right"] = infoBarRight + "px";
				console.log(animateEndStyle);
				infoBar.animate(animateEndStyle, 200, function(){
					infoBar.css(originalStyle);
					infoBarDetached = false;
					changingState = false;
					console.log("complete");
				});
			}
		}
		
	});
};

$(document).ready(function(){
	InitializeAutoPaginate();
	InitializeDetachedInfoBar();
});