Created
June 15, 2015 19:00
-
-
Save anonymous/1df96dce2ae9cafccce7 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 Voat subverses header | |
// @author Horza | |
// @date 15 june 2015 | |
// @description Replaces subverse list | |
// @include *://*.voat.co/* | |
// @include *://voat.co/* | |
// @version 1 | |
// @grant GM_getValue | |
// @grant GM_setValue | |
// @run-at document-end | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js | |
// ==/UserScript== | |
'use strict'; | |
(function () { | |
DisplayCustomSubversesList(); | |
if (isPageSubverse()) { | |
AppendButton(); | |
} | |
}).call(this); | |
function isPageSubverse() { | |
if (GetSubverseName() != null) { | |
return true; | |
} | |
return false; | |
} | |
function GetSubverseName() { | |
var re = new RegExp(/voat\.co\/v\/([\w\d]*)/); | |
var m = re.exec(window.location.href); | |
if (m == null) { return null; } | |
else { return m[1].toLowerCase(); } | |
} | |
function isPageInShortcuts() { | |
var subversesArr = GetSubversesList(); | |
var subName = GetSubverseName(); | |
for (var i in subversesArr) { | |
if (subversesArr[i] == subName) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function AppendButton() { | |
if (!isPageInShortcuts()) { | |
var btnHTML = '<button id="GM_Shortcut" type="button" class="btn-whoaverse-paging btn-xs btn-default btn-sub">+ shortcut</button>'; | |
} | |
else { | |
var btnHTML = '<button id="GM_Shortcut" type="button" class="btn-whoaverse-paging btn-xs btn-default">- shortcut</button>'; | |
} | |
$(btnHTML).insertAfter(".btn-whoaverse-paging.btn-xs.btn-default.btn-sub"); | |
} | |
$('#GM_Shortcut').click(function () { | |
if (isPageInShortcuts()) { | |
RemoveFromShortcuts(GetSubverseName()); | |
} | |
else { | |
AddToShortcuts(GetSubverseName()); | |
} | |
}); | |
function AddToShortcuts(SubName) { | |
var subversesArr = GetSubversesList(); | |
var str = subversesArr.join(",") + "," + SubName; | |
GM_setValue("Voat_Subverses", str); | |
ToggleButton(false); | |
DisplayCustomSubversesList(); | |
} | |
function RemoveFromShortcuts(SubName) { | |
var subversesArr = GetSubversesList(); | |
var idx = subversesArr.indexOf(SubName); | |
subversesArr.splice(idx, 1); | |
GM_setValue("Voat_Subverses", subversesArr.join(",")); | |
ToggleButton(true); | |
DisplayCustomSubversesList(); | |
} | |
function ToggleButton(state) { | |
if (state == true) { | |
$("#GM_Shortcut").text('+ shortcut'); | |
$("#GM_Shortcut").addClass('btn-sub') | |
} | |
else { | |
$("#GM_Shortcut").text('- shortcut'); | |
$("#GM_Shortcut").removeClass('btn-sub'); | |
} | |
} | |
function GetSubversesList(){ | |
var subversesArr = GM_getValue( "Voat_Subverses" ); | |
if (subversesArr == undefined){ | |
subversesArr = "newsubverses,introductions,news" | |
GM_setValue("Voat_Subverses", subversesArr); | |
} | |
return subversesArr.split(','); | |
} | |
function DisplayCustomSubversesList (){ | |
var SubString = ''; | |
var subArr = GetSubversesList(); | |
for (var idx in subArr) { | |
SubString += '\ | |
<li class=""><span class="separator">-</span><a href="/v/' + subArr[idx] + '/">' + subArr[idx] + '</a></li>'; | |
} | |
$('ul#sr-bar').html(SubString); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment