AO3: Jump to a Random Work

adds a button when viewing works, which will pick a random work in your current view and redirect you there

当前为 2022-11-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AO3: Jump to a Random Work
  3. // @namespace https://gf.qytechs.cn/en/users/906106-escctrl
  4. // @version 0.1
  5. // @description adds a button when viewing works, which will pick a random work in your current view and redirect you there
  6. // @author escctrl
  7. // @match *://*.archiveofourown.org/tags/*/works*
  8. // @match *://*.archiveofourown.org/works?*
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function($) {
  15. 'use strict';
  16.  
  17. // add a button
  18. var nav = document.querySelector('div#main.works-index div.navigation.actions.module ul.user.navigation.actions');
  19. var button = document.createElement('li');
  20. button.innerHTML = '<a href="#">Random Work</a>';
  21. button.addEventListener("click", RandomWork);
  22. nav.appendChild(button);
  23.  
  24. // when the button was pressed, read the number of works, pick a random one, and redirect there
  25. function RandomWork() {
  26.  
  27. // .contents().first() grabs only the textNode (1-20 of X Works in) instead of the whole heading text
  28. // that way we don't care what numbers might be part of the tag, it's not going to confuse the code
  29. const worksTitle = $('#main h2.heading').contents().first().text();
  30.  
  31. const worksMatch = worksTitle.match(/\d+/g);
  32. /* if "1-20 of X Works", [0] match is "1" and [1] is "20" from the "1-20" part, and [2] is the total number
  33. if "X Works" (below 20), [0] is the total number
  34. either way it's the last entry in the array
  35. */
  36. const worksCount = parseInt(worksMatch[worksMatch.length-1]);
  37.  
  38. // pick random whole number of work within the filter (between 1 and total number of works)
  39. const chooseRandom = Math.floor((Math.random() * worksCount) + 1);
  40.  
  41. // there are always 20 works per page. find the n-th page and m-th work on that page
  42. const pageRandom = Math.ceil(chooseRandom/20);
  43. const workRandom = chooseRandom - (20*(pageRandom-1));
  44.  
  45. // check: are we currently on that page?
  46. var thisPage = location.search.match(/page=(\d)+/);
  47. thisPage = thisPage === null ? 1 : parseInt(thisPage[1]); // match only worked if URL contains pagination already
  48.  
  49. //console.log({chosen: chooseRandom, onpage: pageRandom, workonpage: workRandom, thispage: thisPage});
  50.  
  51. // if not - built the URL of that page and read it to find the random work link
  52. if (thisPage !== pageRandom) {
  53. var pageURL = location.search.indexOf('page=') > 0 ? location.href.replace(/page=(\d)+/, 'page='+pageRandom) // replace existing page number
  54. : location.href + (location.href.indexOf('?') ? '&' : '?') + 'page='+pageRandom; // add page number if not yet in URL search parameters
  55.  
  56. // grab the list of works from the page
  57. console.log('loading page...');
  58. $.get(pageURL, function(response) {
  59. }).done(function(response) {
  60. Redirect2Work($(response).find('ol.work.index.group li.work.blurb'), workRandom);
  61.  
  62. // if that sent us to jail, set the ao3jail marker
  63. }).fail(function(data, textStatus, xhr) {
  64. console.log("Random Work script has hit Retry later", data.status);
  65. return false;
  66. });
  67. }
  68. // if yes - read the random work link from this page
  69. else {
  70. Redirect2Work($('ol.work.index.group li.work.blurb'), workRandom);
  71. }
  72. }
  73.  
  74. function Redirect2Work(worksList, pick) {
  75. //console.log(worksList);
  76. pick = $(worksList[pick-1]).find('h4 a').first();
  77. var path = $(pick).attr('href');
  78. var title = $(pick).text();
  79. alert('redirecting to a random work: '+title);
  80. window.location.assign(path);
  81. }
  82.  
  83. })(jQuery);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址