Replace hitcount with kudos/hits percentage. Sort works on the page by this ratio.
目前為
// ==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
// @grant none
// @include http://archiveofourown.org/*
// @include https://archiveofourown.org/*
// ==/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 = $('<li class="dropdown"></li>').html('<a>Kudos/hits</a>');
header_menu.find('li.search').before(ratio_menu);
// create and append dropdown menu
var drop_menu = $('<ul class="menu dropdown-menu"></li>');
ratio_menu.append(drop_menu);
// create button - count
var button_count = $('<li></li>').html('<a>Count on this page</a>');
button_count.click(function() {countRatio();});
// create button - sort
var button_sort = $('<li></li>').html('<a>Sort on this page</a>');
button_sort.click(function() {sortWorks();});
// create button - settings
var button_settings = $('<li></li>').html('<a>== Settings (click to change): ==</a>');
// create button - always count
var button_count_yes = $('<li class="count-yes"></li>').html('<a>Count automatically: YES</a>');
drop_menu.on('click', 'li.count-yes', function() {
localStorage.setItem('alwayscountlocal', 'no');
button_count_yes.replaceWith(button_count_no);
});
// create button - not always count
var button_count_no = $('<li class="count-no"></li>').html('<a>Count automatically: NO</a>');
drop_menu.on('click', 'li.count-no', function() {
localStorage.setItem('alwayscountlocal', 'yes');
button_count_no.replaceWith(button_count_yes);
});
// create button - always sort
var button_sort_yes = $('<li class="sort-yes"></li>').html('<a>Sort automatically: YES</a>');
drop_menu.on('click', 'li.sort-yes', function() {
localStorage.setItem('alwayssortlocal', 'no');
button_sort_yes.replaceWith(button_sort_no);
});
// create button - not always sort
var button_sort_no = $('<li class="sort-no"></li>').html('<a>Sort automatically: NO</a>');
drop_menu.on('click', 'li.sort-no', function() {
localStorage.setItem('alwayssortlocal', 'yes');
button_sort_no.replaceWith(button_sort_yes);
});
// append buttons to the dropdown menu
drop_menu.append(button_count);
if (sortable) {
drop_menu.append(button_sort);
}
if (typeof(Storage) !== 'undefined') {
drop_menu.append(button_settings);
if (always_count) {
drop_menu.append(button_count_yes);
}
else {
drop_menu.append(button_count_no);
}
if (always_sort) {
drop_menu.append(button_sort_yes);
}
else {
drop_menu.append(button_sort_no);
}
}
}
})(jQuery);