AO3: Jump to a Random Work

adds a "Random Work" button (top right corner) when viewing works in a tag/filter or your Marked For Later list

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

  1. // ==UserScript==
  2. // @name AO3: Jump to a Random Work
  3. // @namespace https://gf.qytechs.cn/en/users/906106-escctrl
  4. // @version 0.2
  5. // @description adds a "Random Work" button (top right corner) when viewing works in a tag/filter or your Marked For Later list
  6. // @author escctrl
  7. // @match *://*.archiveofourown.org/tags/*/works*
  8. // @match *://*.archiveofourown.org/works?*
  9. // @match *://*.archiveofourown.org/users/*/readings*show=to-read*
  10. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js
  11. // @grant none
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function($) {
  16. 'use strict';
  17.  
  18. // add a button
  19. var button = document.createElement('li');
  20. button.innerHTML = '<a href="#">Random Work</a>';
  21. button.addEventListener("click", RandomWork);
  22. if (location.href.indexOf('show=to-read') > 0) document.querySelector('div#main.readings-index ul.navigation.actions').appendChild(button);
  23. else document.querySelector('div#main.works-index div.navigation.actions.module ul.user.navigation.actions').appendChild(button);
  24.  
  25. // when the button was pressed, read the number of works, pick a random one, and redirect there
  26. function RandomWork() {
  27.  
  28. // Find number of pages. content of second-to-last <li> tells us
  29. var pageCount = parseInt($('ol.pagination').first().find('li').last().prev().text() || 1);
  30.  
  31. // pick random whole number of the available pages
  32. const pageRandom = Math.floor((Math.random() * pageCount) + 1);
  33.  
  34. // figure out which page we're currently viewing
  35. var thisPage = location.search.match(/page=(\d)+/);
  36. thisPage = thisPage === null ? 1 : parseInt(thisPage[1]); // match only works if URL contained a page (i.e. if not on page 1)
  37.  
  38. // check: are we currently on the randomly chosen page?
  39. if (thisPage !== pageRandom) LoadRandomPage(pageRandom); // if not - read that page to find a random work link
  40. else Redirect2Work($('ol.work.index.group li.work.blurb')); // if yes - skip page loads, read a random work link from this page
  41. }
  42.  
  43. function LoadRandomPage(r) {
  44. // build the URL of the page to load
  45. var pageURL = location.search.indexOf('page=') > 0 ? location.href.replace(/page=(\d)+/, 'page='+r) // replace existing page number
  46. : location.href + (location.href.indexOf('?') > 0 ? '&' : '?') + 'page='+r; // add page number if not yet in URL search parameters
  47.  
  48. // grab the list of works from the page
  49. $.get(pageURL, function(response) {
  50. }).done(function(response) {
  51. Redirect2Work($(response).find('ol.work.index.group li.work.blurb'));
  52.  
  53. // if that sent us to jail, set the ao3jail marker
  54. }).fail(function(data, textStatus, xhr) {
  55. console.log("Random Work script has hit Retry later", data.status);
  56. return false;
  57. });
  58. }
  59.  
  60. function Redirect2Work(worksList) {
  61. // pick a random work from within the list
  62. var pick = Math.floor((Math.random() * worksList.length) + 1);
  63.  
  64. // read that random work's URL and title
  65. pick = $(worksList[pick-1]).find('h4 a').first();
  66. var path = $(pick).attr('href');
  67. var title = $(pick).text();
  68.  
  69. // jump to that work but warn the user
  70. alert('Redirecting you to a random work: '+title);
  71. window.location.assign(path);
  72. }
  73.  
  74. })(jQuery);

QingJ © 2025

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