Drafts publish with their initial creation date. This script shows a hint to update the publication date before you post the work.
// ==UserScript==
// @name AO3: Warn for Old Publication Date on Drafts
// @namespace https://greasyfork.org/en/users/906106-escctrl
// @description Drafts publish with their initial creation date. This script shows a hint to update the publication date before you post the work.
// @author escctrl
// @version 2.1
// @match https://*.archiveofourown.org/works/*/edit
// @grant none
// @license GNU GPL-3.0-only
// ==/UserScript==
(function() {
'use strict';
const q = (selector, node=document) => node.querySelector(selector);
const ins = (n, l, html) => n.insertAdjacentHTML(l, html);
// stop if this is not a draft but an already posted work
const saveButton = q('input[name="save_button"]');
if (!saveButton) return;
// compare the (possibly hidden) selected publication date with current
let d = q('#work_chapter_attributes_published_at_3i, #chapter_published_at_3i').value;
let m = q('#work_chapter_attributes_published_at_2i, #chapter_published_at_2i').value;
let y = q('#work_chapter_attributes_published_at_1i, #chapter_published_at_1i').value;
let selectedDate = new Date(`${m} ${d}, ${y} 00:00`);
let currentDate = new Date();
// if the dates don't match, show a warning to user with a button to conveniently set the publication to today
if (selectedDate.toDateString() !== currentDate.toDateString()) {
ins(saveButton.closest('fieldset'), 'beforeend', `<p id="warnOldPublishDate" class="caution notice">This draft is using an old publication date
(${selectedDate.toDateString()}). You might want to <button type="button" id="setNewPublishDate">update it to today's date</button>.</p>`);
q('button#setNewPublishDate').addEventListener('click', function(e) {
e.preventDefault(); // don't try to submit the form
if (q('#work-form') && !q('#backdate-options-show').checked) q('#backdate-options-show').click(); // mimick user click -> trigger events to show fields
q('#work_chapter_attributes_published_at_3i, #chapter_published_at_3i').value = currentDate.getDate(); // set today's date
q('#work_chapter_attributes_published_at_2i, #chapter_published_at_2i').value = currentDate.getMonth()+1; // set today's month (getMonth is zero-based)
q('#work_chapter_attributes_published_at_1i, #chapter_published_at_1i').value = currentDate.getFullYear(); // set today's year (4-digit version)
q('#warnOldPublishDate').remove(); // hide the warning message
q('dt.backdate, dt#managePublicationDate').scrollIntoView(); // scroll to the updated publication date for visual confirmation
});
}
})();