// ==UserScript== // @name AO3: Kudos/hits ratio // @description Replace hitcount with kudos/hits percentage. Sort works on the page by this ratio. // @namespace https://greasyfork.org/scripts/3144-ao3-kudos-hits-ratio // @author Min // @version 1.1.1 // @grant none // @include http://archiveofourown.org/* // @include https://archiveofourown.org/* // @downloadURL none // ==/UserScript== // ~~ SETTINGS ~~ // // change hitcount to kudos/hits automatically: true/false var always_count = true; // sort works on this page by kudos/hits ratio in descending order automatically: true/false var always_sort = false; // colour background depending on percentage: true/false var colourbg = true; // lvl1 & lvl2 - percentage levels separating red, yellow and green background; ratio_red, ratio_yellow, ratio_green - background colours var ratio_red = '#ffdede'; var lvl1 = 4; var ratio_yellow = '#fdf2a3'; var lvl2 = 7; var ratio_green = '#c4eac3'; // ~~ END OF SETTINGS ~~ // // STUFF HAPPENS BELOW // (function($) { // check user settings if (typeof(Storage) !== 'undefined') { var always_count_set = localStorage.getItem('alwayscountlocal'); var always_sort_set = localStorage.getItem('alwayssortlocal'); if (always_count_set == 'no') { always_count = false; } if (always_sort_set == 'yes') { always_sort = true; } } // set defaults for countableness and sortableness var countable = false; var sortable = false; // check if it's a list of works or bookmarks, or header on work page, and attach the menu checkCountable(); // if set to automatic if (always_count) { countRatio(); if (always_sort) { sortWorks(); } } // check if it's a list of works or bookmarks, or header on work page function checkCountable() { var found_stats = $('dl.stats'); if (found_stats.length) { // check if it's a list of works or bookmarks, or header on work page var stats_parent = found_stats.first().parent(); if (stats_parent.hasClass('work') || stats_parent.hasClass('bookmark')) { countable = true; sortable = true; addRatioMenu(); } else if (stats_parent.hasClass('stats')) { countable = true; addRatioMenu(); } } } function countRatio() { if (countable) { $('dl.stats').each(function() { var found_hits = false; var found_kudos = false; // get all label elements var stat_labels = $(this).find('dt'); // search labels for hits and kudos for (var i = 0; i < stat_labels.length; i++) { if (stat_labels.eq(i).text() == 'Hits:') { var hits_label = stat_labels.eq(i); var hits_value = stat_labels.eq(i).next(); found_hits = true; } else if (stat_labels.eq(i).text() == 'Kudos:') { var kudos_value = stat_labels.eq(i).next(); found_kudos = true; } } // if hits and kudos were found if (found_hits == true && found_kudos == true) { // get counts var hits_count = parseFloat(hits_value.text()); var kudos_count = parseFloat(kudos_value.text()); // count percentage var percents = 100*kudos_count/hits_count; // get percentage with one decimal point var percents_print = percents.toFixed(1).replace('.',','); // replace percentage and label on page hits_value.text(percents_print + '%'); hits_label.text('Kudos/Hits:'); if (colourbg) { // colour background depending on percentage if (percents > lvl2) {hits_value.css('background-color', ratio_green);} else if (percents < lvl1) {hits_value.css('background-color', ratio_red);} else {hits_value.css('background-color', ratio_yellow);} } // add attribute to the blurb for sorting $(this).parent().attr('kudospercent', percents); } else { // add attribute to the blurb for sorting $(this).parent().attr('kudospercent', 0); } }); } } function sortWorks() { if (sortable) { var work_list = $('li.blurb').first().parent(); var blurbs = work_list.children('li'); // sort the blurbs by kudos/hits ratio in descending order blurbs.sort(function(a, b) { return parseFloat(b.getAttribute('kudospercent')) - parseFloat(a.getAttribute('kudospercent')); }); blurbs.detach().appendTo(work_list); } } // attach the menu function addRatioMenu() { // get the header menu var header_menu = $('ul.primary.navigation.actions'); // create and insert menu button var ratio_menu = $('
').html('Kudos/hits'); header_menu.find('li.search').before(ratio_menu); // create and append dropdown menu var drop_menu = $('