Last active
August 29, 2015 14:17
-
-
Save orderthruchaos/0974980f6d5bc5898244 to your computer and use it in GitHub Desktop.
Greasemonkey User Script: add "first page" and "last page" links to hex.pm package listing.
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 hex_pm_pages | |
// @namespace http://orderthruchaos.bitbucket.org/ | |
// @copyright 2015+, Brett DiFrischia | |
// @license | |
// @description | |
// @grant GM_log | |
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js | |
// @include https://hex.pm/packages | |
// @include https://hex.pm/packages?* | |
// ==/UserScript== | |
// @grant | |
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js | |
// @require https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js | |
// @require https://ajax.googleapis.com/ajax/libs/threejs/r67/three.min.js | |
// @require https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js | |
// @require https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.3/jquery.mobile.min.js | |
// @resource https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.3/jquery.mobile.min.css | |
// See the following for more Google-hosted libraries: | |
// https://developers.google.com/speed/libraries/ | |
this.$J = this.jQuery = jQuery.noConflict(true); | |
// var $J = jQuery.noConflict(); | |
( | |
function () { | |
var item_count_selector = 'p.pagination.pull-left'; | |
var page_links_selector = 'ul.pagination.pull-right'; | |
var left_chevron = '«'; | |
var right_chevron = '»'; | |
var is_chevron = function(s) { return s == '»'; }; | |
var trim = function (s) { | |
return s.replace(/^\s*/m, '').replace(/\s*$/m, ''); | |
}; | |
var get_prev_list_item = function () { | |
return $J('ul.pagination.pull-right li').first(); | |
}; | |
var get_next_list_item = function () { | |
return $J('ul.pagination.pull-right li').last(); | |
}; | |
var get_base_text = function() { | |
var txt = $J(item_count_selector).text(); | |
return txt.replace(/^[^0-9]*/m, '').replace(/[^0-9]*$/m, ''); | |
}; | |
var get_item1 = function() { | |
return parseInt( get_base_text().replace(/-.*/, '') ); | |
}; | |
var get_item2 = function() { | |
return parseInt( | |
get_base_text().replace(/^\d+./, '').replace(/ .*/, '') | |
); | |
}; | |
var get_count = function () { | |
return parseInt( get_base_text().replace(/.*\s/, '') ); | |
}; | |
var this_is_last_page = function () { | |
var el = $J('ul.pagination.pull-right li.disabled span'); | |
if (el.length > 0 && is_chevron(el.text())) return true; | |
else return false; | |
}; | |
var prev_page_number = function () { | |
return parseInt($J('ul.pagination.pull-right li a').last().text()); | |
}; | |
var curr_page_number = function () { | |
return parseInt($J('ul.pagination.pull-right li.active span').text()); | |
}; | |
var get_ppp = function () { | |
var it1 = get_item1(); | |
if (this_is_last_page()) { | |
it1 = it1 - 1; | |
return it1 / prev_page_number(); | |
} else { | |
var it2 = get_item2(); | |
return ppp = it2 - it1 + 1; | |
} | |
}; | |
var get_page_count = function(it1, it2, cnt) { | |
if (this_is_last_page()) { | |
return curr_page_number(); | |
} else { | |
var cnt = get_count(); | |
var ppp = get_ppp(); | |
return Math.ceil(cnt / ppp); | |
} | |
}; | |
var add_frst_link = function () { | |
var prev_link = get_prev_list_item(); | |
var prev_text = prev_link.text(); | |
var frst_link = prev_link.clone(); | |
frst_link.children().first().text(prev_text + prev_text); | |
if (frst_link.children('a').length > 0) { | |
var href = frst_link.children('a').attr('href').replace(/\d+$/, '1'); | |
frst_link.children('a').attr('href', href); | |
} | |
prev_link.before(frst_link); | |
}; | |
var add_last_link = function () { | |
var next_link = get_next_list_item(); | |
var next_text = next_link.text(); | |
var last_link = next_link.clone(); | |
last_link.children().first().text(next_text + next_text); | |
if (last_link.children('a').length > 0) { | |
var cnt = "" + get_page_count(); | |
var href = last_link.children('a').attr('href').replace(/\d+$/, cnt); | |
last_link.children('a').attr('href', href); | |
} | |
next_link.after(last_link); | |
}; | |
// var on_load = function() { add_frst_link(); add_last_link(); }; | |
$J(function() { add_frst_link(); add_last_link(); }); | |
} | |
)(); | |
// vim: sts=2 sw=2 ts=8 et |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment