// ==UserScript== // @name AO3: bookmarks/kudos ratio // @description Replace hitcount with bookmarks/kudos percentage. Sort works on the page by this ratio. // @namespace https://greasyfork.org/scripts/3144-ao3-bookmarks-kudos-ratio // @author Min // @version 1.6 // @history 1.4 - always show kudos on stats page, require jquery (for firefox) // @history 1.3 - works for statistics, option to show hitcount // @history 1.2 - makes use of new stats classes // @grant none // @require https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js // @include http://archiveofourown.org/* // @include https://archiveofourown.org/* // @downloadURL none // ==/UserScript== // ~~ SETTINGS ~~ // // count bookmarks/kudos automatically: true/false var always_count = true; // sort works on this page by bookmarks/kudos ratio in descending order automatically: true/false var always_sort = false; // hide hitcount: true/false var hide_hitcount = true; // 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 = 22.5; var ratio_yellow = '#fdf2a3'; var lvl2 = 27.5; 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'); var hide_hitcount_set = localStorage.getItem('hidehitcountlocal'); if (always_count_set == 'no') { always_count = false; } if (always_sort_set == 'yes') { always_sort = true; } if (hide_hitcount_set == 'no') { hide_hitcount = false; } } // set defaults for countableness and sortableness var countable = false; var sortable = false; var stats_page = 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) { sortByRatio(); } } // check if it's a list of works/bookmarks/statistics, or header on work page function checkCountable() { var found_stats = $('dl.stats'); if (found_stats.length) { if (found_stats.closest('li').is('.work') || found_stats.closest('li').is('.bookmark')) { countable = true; sortable = true; addRatioMenu(); } else if (found_stats.parents('.statistics').length) { countable = true; sortable = true; stats_page = true; addRatioMenu(); } else if (found_stats.parents('dl.work').length) { countable = true; addRatioMenu(); } } } function countRatio() { if (countable) { $('dl.stats').each(function() { var kudos_value = $(this).find('dd.kudos'); var bookmarks_value = $(this).find('dd.bookmarks'); // if kudos and bookmarks were found if (bookmarks_value.length && kudos_value.length && kudos_value.text() !== '0') { // get counts var kudos_count = parseInt(kudos_value.text()); var bookmarks_count = parseInt(bookmarks_value.text()); // count percentage var percents = 100*bookmarks_count/kudos_count; // get percentage with one decimal point var percents_print = percents.toFixed(1).replace('.',','); // add ratio stats var ratio_label = $('
').text('bookmarks/kudos:'); var ratio_value = $('
').text(percents_print + '%'); kudos_value.after('\n', ratio_label, '\n', ratio_value); if (colourbg) { // colour background depending on percentage if (percents >= lvl2) { ratio_value.css('background-color', ratio_green); } else if (percents >= lvl1) { ratio_value.css('background-color', ratio_yellow); } else { ratio_value.css('background-color', ratio_red); } } if (hide_hitcount && !stats_page) { // hide hitcount label and value $(this).find('.kudos').css('display', 'none'); } // add attribute to the blurb for sorting $(this).closest('li').attr('bookmarkspercent', percents); } else { // add attribute to the blurb for sorting $(this).closest('li').attr('bookmarkspercent', 0); } }); } } function sortByRatio(ascending) { if (sortable) { var sortable_lists = $('dl.stats').closest('li').parent(); sortable_lists.each(function() { var list_elements = $(this).children('li'); // sort by bookmarks/kudos ratio in descending order list_elements.sort(function(a, b) { return parseFloat(b.getAttribute('bookmarkspercent')) - parseFloat(a.getAttribute('bookmarkspercent')); }); if (ascending) { $(list_elements.get().reverse()).detach().appendTo($(this)); } else { list_elements.detach().appendTo($(this)); } }); } } // 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('bookmarks/kudos'); header_menu.find('li.search').before(ratio_menu); // create and append dropdown menu var drop_menu = $('