AO3: [Wrangling] Check Unique Tag Users

Adds a button to count unique users in your unfilterable wrangling bins. Based on Check Tag Status script: https://github.com/kaerstyne/ao3-wrangling-scripts

  1. // ==UserScript==
  2. // @name AO3: [Wrangling] Check Unique Tag Users
  3. // @description Adds a button to count unique users in your unfilterable wrangling bins. Based on Check Tag Status script: https://github.com/kaerstyne/ao3-wrangling-scripts
  4. // @version 0.2
  5.  
  6. // @author endofthyme
  7. // @namespace http://tampermonkey.net/
  8. // @license GPL-3.0 <https://www.gnu.org/licenses/gpl.html>
  9.  
  10. // @match *://*.archiveofourown.org/tags/*/wrangle?*&status=unfilterable
  11. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function($) {
  16. // add the button
  17. var count_users_button = $('<ul class="actions" role="menu"><li><a id="count_users">Count Users</a></li></ul>');
  18. $("thead").find("th:contains('Taggings')").append(count_users_button);
  19.  
  20. // when button is pressed
  21. $("a[id='count_users']").on("click", function() {
  22.  
  23. // check each tag on page
  24. $("tbody tr").each(function(i, row) {
  25. var tag_link = $(this).find("a[href$='/works']").attr("href").slice(0, -6);
  26. var taggings_cell = $(this).find("td[title='taggings']");
  27.  
  28. // check the tag's landing page
  29. $.get(tag_link, function(response) {
  30. var has_multiple_pages = $(response).find("div.work h4.landmark").size() > 0;
  31.  
  32. var users = new Set();
  33. var user_count = 0;
  34. // loop through all works
  35. $(response).find("div.work div.header").each(function(j, row2) {
  36. var found_a_user = false;
  37. var found_repeat_user_or_orphaned = false;
  38. var new_user_set = new Set(users);
  39. // loop through all authors on a work
  40. $(this).find("a[rel$='author']").each(function(k, row3) {
  41. var parsed_user = $(this).text();
  42. var check_pseud = parsed_user.match(/[^(]*\(([^)]*)\)/) // do not count pseuds separately
  43. if (check_pseud) {
  44. parsed_user = check_pseud[1]
  45. }
  46. if (parsed_user) {
  47. found_a_user = true;
  48. if (parsed_user == "orphan_account" || users.has(parsed_user)) {
  49. found_repeat_user_or_orphaned = true;
  50. } else {
  51. new_user_set.add(parsed_user);
  52. }
  53. }
  54. });
  55. if (found_a_user && !found_repeat_user_or_orphaned) {
  56. user_count++;
  57. // only add users to list if the work was counted as a unique use
  58. for (const x of new_user_set) {
  59. users.add(x);
  60. }
  61. }
  62. });
  63. if (user_count > 1 || has_multiple_pages) {
  64. taggings_cell.append(" (" + user_count + (has_multiple_pages ? "+" : "") + ")");
  65. console.log(user_count);
  66. }
  67. });
  68. });
  69.  
  70. // remove the button when finished
  71. $("ul").remove(":has(li a[id='count_users'])");
  72. });
  73. })(jQuery);