Skip to content

Instantly share code, notes, and snippets.

@KonTrax
Created July 30, 2014 22:24
Show Gist options
  • Save KonTrax/e98f4f91b29e0879b8c3 to your computer and use it in GitHub Desktop.
Save KonTrax/e98f4f91b29e0879b8c3 to your computer and use it in GitHub Desktop.
Fixes some lazy issues in the sharetv code
// ==UserScript==
// @id STVF
// @name ShareTV Fixes
// @version 0.1
// @namespace KonTraBox
// @author KonTrax
// @description Fixes some lazy issues in the sharetv code
// @match
// @include http://sharetv.com/*
// @copyright 1960-1961, SantaClaus
// @run-at document-end
// ==/UserScript==
// =============================================================================
// Notes
// http://sharetv.com/user/*USERNAME*/shows/0
// =============================================================================
// Utilities
function slicedToLast(haystack, needle) {
return haystack.slice(0, haystack.lastIndexOf(needle) +1);
}
function getTags(name, context) {
if (typeof context == 'undefined') context = document;
return context.getElementsByTagName(name);
}
function removeFromStart(original, stringToRemove) {
if (original.startsWith(stringToRemove)) {
return original.slice(stringToRemove.length);
}
return '';
}
function defaultValue(val, defaultVal) {
return (typeof val == 'undefined') ? defaultVal : val;
}
function substrCompare(a, b, start, end) {
start = defaultValue(start, 0);
end = defaultValue(end, undefined);
return a.slice(start, end) == b.slice(start, end);
}
function equalUpTo(a, b, length) {
length = defaultValue(length, undefined);
return substrCompare(a, b, 0, length);
}
function stopAt(haystack, needle, searchOffset) {
searchOffset = defaultValue(searchOffset, 0);
var end = haystack.indexOf(needle, searchOffset);
return (end == -1) ? haystack : haystack.slice(0, end);
}
// =============================================================================
// Operations
function Fix_Pagination () {
var anchors = getTags("a");
if (anchors.length === 0) return false;
var location = window.location.href;
var origin = window.location.origin;
for (var i = 0, len = anchors.length; i < len; i++) {
var pos = anchors[i].href.indexOf('//', origin.length);
if (pos == -1){ continue;}
pos += 1;
if (!equalUpTo(location, anchors[i].href, pos)){ continue;}
var newUrl = stopAt(location, '/', pos)
+ anchors[i].href.slice(pos);
anchors[i].href = newUrl;
// console.log("RESULT: " + newUrl);
// console.log('-- ## --');
}
return true;
}
// =============================================================================
// Main
function Main () {
// console.log('Main');
Fix_Pagination();
}
// =============================================================================
// Exec
Main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment