Note: This story was dynamically reformatted for online reading convenience. /* * To integrate: * * Within the HEAD tag of the page, add these two lines: * <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></scr ipt> * <script type="text/javascript" src="/nifty/scripts/nifty-filter.js"></script> * * I changed the script to wake up when the page finishes loading and inject the "Filter" link. It should work * all the way down to IE 6, because I don't do anything fancy. And it only uses things on the page - no JSON * calls or cookies or anything. I've thought of adding an option to cookie the user if they want with stories * they're following or "since last time" links, but the current privacy policy doesn't allow for it, so I've * left it alone for now. * * Copyright 2012,2013 Nifty Archive Alliance. All Rights Reserved. * */ $(document).ready(function(){ main(); }); function main() { var today = new Date(); var rows = []; var sectionsAll = {}; var sections = {}; var result = []; var day = 24 * 60 * 60 * 1000; var mySec = "ALL"; var mySub = "ALL"; var daysBack = 10; var sectionKeys = []; var subsectionsAll = []; var subsections = []; var sortTypes = { compareDate:function (a, b) { if ((today - a.date) < (today - b.date)) return -1; if ((today - a.date) > (today - b.date)) return 1; return 0; }, compareSub:function (a, b) { if (subsections.indexOf(a.subsection) < subsections.indexOf(b.subsection)) return -1; if (subsections.indexOf(a.subsection) > subsections.indexOf(b.subsection)) return 1; return 0; } }; var currentSort = sortTypes['compareDate']; Array.prototype.getUnique = function () { var u = {}, a = []; for (var i = 0, l = this.length; i < l; ++i) { if (this[i] in u) continue; if (this[i] == undefined) continue; a.push(this[i]); u[this[i]] = 1; } return a; } function compareDate(a, b) { if ((today - a.date) < (today - b.date)) return -1; if ((today - a.date) > (today - b.date)) return 1; return 0; } function compareSub(a, b) { if (subsections.indexOf(a.subsection) < subsections.indexOf(b.subsection)) return -1; if (subsections.indexOf(a.subsection) > subsections.indexOf(b.subsection)) return 1; return 0; } function Story(size, date, link) { this.size = size; this.date = date; this.link = link; var pieces = link.split("/"); this.section = pieces[0]; if (this.section != "bestiality") { this.subsection = pieces[1]; } else { this.subsection = ""; } subsectionsAll.push(this.subsection); if (!sectionsAll[this.section]) sectionsAll[this.section] = []; sectionsAll[this.section].push(this.subsection); this.isSeries = false; if ((pieces.length > 3) && (this.section != "bestiality")) this.isSeries = true; if ((pieces.length > 2) && (this.section == "bestiality")) this.isSeries = true; this.name = pieces[pieces.length - 1]; } function render(arr) { arr.sort(currentSort); var retV = ""; for (var i = 0; i < arr.length; i++) { retV += "<tr>"; retV += "<td>" + arr[i].date.toDateString() + "</td>"; retV += "<td><a href='" + arr[i].link + "'>"; retV += arr[i].name + "</a> (" + arr[i].size + ")</td>"; retV += "<td>" + arr[i].section + " / " + arr[i].subsection if (arr[i]['isSeries']) retV += " (series)"; retV += "</td><tr>"; } return retV; } function renderSubs(sec) { var uiRet = ""; if (sec != "ALL") { uiRet += "<option value='ALL'>(all)</option>"; for (var i = 0; i < sec.length; i++) { uiRet += "<option value='" + sec[i] + "'>" + sec[i] + "</option>"; } } else uiRet = ""; $('select#subsection').html(uiRet); } function renderBody() { $('tbody').html(render(result)); } function filterData(sec, sub, days) { result = []; for (var i in rows) { if ((rows[i]['date'] > (today - day * days)) && ((rows[i]['section'] == sec) || (sec == "ALL")) && ((rows[i]['subsection'] == sub) || (sub == "ALL")) ) result.push(rows[i]); } } function init() { // grab all the table data $('tr:has(td)').each(function () { var fileSize = $(this).children('td:nth-child(1)').text(); var rawDate = $(this).children('td:nth-child(2)').text(); var rawLink = $(this).children('td').children('a').attr('href'); var splitDate = rawDate.split(" "); var fullYear = today.getFullYear(); if (today.getMonth() === 0 && splitDate[0] === 'Dec') --fullYear; var date = new Date(splitDate[0] + " " + splitDate[1] + " " + fullYear + " " + splitDate[2]); rows.push(new Story(fileSize, date, rawLink)); }); // make the sections object sectionKeys = Object.keys(sectionsAll).sort(); for (var i = 0; i < sectionKeys.length; i++) { sections[sectionKeys[i]] = sectionsAll[sectionKeys[i]].getUnique(); } sections['ALL'] = []; subsections = subsectionsAll.getUnique(); subsections.sort(); // paint initial UI var initUI = ""; initUI += "<thead><tr><th>Show Last <input type='text' maxlength='2' id='daysBack' value='" + daysBack + "' style='width:24px;'/> Days</th>"; // list out sections initUI += "<th>Section:<select id='section'><option value='ALL'>(all)</option>"; for (var i = 0; i < sectionKeys.length; i++) { initUI += "<option value='" + sectionKeys[i] + "'>" + sectionKeys[i] + "</option>"; } initUI += "</select></th>"; initUI += "<th><a href='javascript:void(0)' id='sortSub'>Subsection:</a><select id='subsection'></select></th></tr></thead><tbody></tbody>"; $('table').html(initUI); $('select#section').change(function (e) { mySec = this.value; mySub = "ALL"; renderSubs(sections[mySec]); filterData(mySec, mySub, daysBack); renderBody(); }); $('select#subsection').change(function (e) { mySub = this.value; filterData(mySec, mySub, daysBack); renderBody(); }); $('#daysBack').keyup(function (e) { daysBack = this.value; filterData(mySec, mySub, daysBack); currentSort = sortTypes['compareDate']; renderBody(); }); $('a#sortSub').click(function (e) { e.preventDefault(); currentSort = sortTypes['compareSub']; renderBody(); }); } // Create new "filter" UI element and make it clickable to start the functionality var newContents = "<tr><th colspan='3'><a href='javascript:void(0)' id='filter' class='seclist'>Filter Results</th></tr>"; $('table').prepend(newContents); $('a#filter').click(function (e) { e.preventDefault(); // setup everything init(); // launch activating event $('select#section').trigger('change'); }); }