AO3: Warn for Old Publication Date on Drafts

Drafts publish with their initial creation date. This script shows a hint to update the publication date before you post the work.

  1. // ==UserScript==
  2. // @name AO3: Warn for Old Publication Date on Drafts
  3. // @namespace https://greasyfork.org/en/users/906106-escctrl
  4. // @description Drafts publish with their initial creation date. This script shows a hint to update the publication date before you post the work.
  5. // @author escctrl
  6. // @version 2.0
  7. // @match https://*.archiveofourown.org/works/*/edit
  8. // @grant none
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. /* eslint-disable no-multi-spaces */
  14. /* global jQuery */
  15.  
  16. (function($) {
  17. 'use strict';
  18.  
  19. // stop if this is not a draft but an already posted work
  20. if ($('input[name="save_button"]').length === 0) return;
  21.  
  22. // compare the (possibly hidden) selected publication date with current
  23. let day = $('#work_chapter_attributes_published_at_3i, #chapter_published_at_3i').prop('value');
  24. let month = $('#work_chapter_attributes_published_at_2i, #chapter_published_at_2i').prop('value');
  25. let year = $('#work_chapter_attributes_published_at_1i, #chapter_published_at_1i').prop('value');
  26.  
  27. let selectedDate = new Date(`${month} ${day}, ${year} 00:00`);
  28. let currentDate = new Date();
  29.  
  30. // if the dates don't match, show a warning to user with a button to conveniently set the publication to today
  31. if (selectedDate.toDateString() !== currentDate.toDateString()) {
  32. $('#work-form, #chapter-form').find('fieldset').last().append(`<p id="warnOldPublishDate" class="caution notice">This draft is using an old publication date.
  33. You might want to <button type="button" id="setNewPublishDate">update it to today's date</button>.</p>`);
  34. }
  35.  
  36. $('button#setNewPublishDate').on('click', function(e) {
  37. e.preventDefault(); // don't try to submit the form
  38.  
  39. if ($('#work-form').length > 0 && !$('#backdate-options-show').prop('checked')) $('#backdate-options-show').trigger('click'); // mimick user click -> trigger events to show fields
  40. $('#work_chapter_attributes_published_at_3i, #chapter_published_at_3i').prop('value', currentDate.getDate()); // set today's date
  41. $('#work_chapter_attributes_published_at_2i, #chapter_published_at_2i').prop('value', currentDate.getMonth()+1); // set today's month (getMonth is zero-based)
  42. $('#work_chapter_attributes_published_at_1i, #chapter_published_at_1i').prop('value', currentDate.getFullYear()); // set today's year (4-digit version)
  43.  
  44. $('#warnOldPublishDate').hide(); // hide the warning message
  45. $('dt.backdate, dt#managePublicationDate')[0].scrollIntoView(); // scroll to the updated publication date for visual confirmation
  46. });
  47.  
  48. })(jQuery);