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.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==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
        });
    }

})();