AO3 Publication date

Adds AO3 publication date i.e. date of publication of first chapter to AO3 search/sort page

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         AO3 Publication date
// @namespace    https://greasyfork.org/en/scripts/455343/
// @version      2.1
// @description  Adds AO3 publication date i.e. date of publication of first chapter to AO3 search/sort page
// @author       MM
// @match        https://archiveofourown.org/tags/*
// @match        https://archiveofourown.org/works?commit=Sort+and+Filter*
// @match        https://archiveofourown.org/works?utf8=%E2%9C%93&commit=Sort+and+Filter*
// @grant        none
// @license      none
// ==/UserScript==




(function ao3firstpubdate() {
    'use strict';

    // Define the HTML template for the date display with a span for styling
    var html_date_heading = '&nbsp;&nbsp;&nbsp;<span class="date-published" style="font-size: 12px; color: #555; font-family: monospace;"> Date Published: <strong><span style="font-size: 14px; font-family: inherit;">';

    // Check for story headings in the index page
    if (jQuery('.header h4.heading').length) {
        jQuery('.header h4.heading').each(function() {
            var sStoryPath = jQuery(this).find('a').first().attr('href');
            var oHeader = this;

            // Check if link is for an individual work
            var aMatch = sStoryPath.match(/works\/(\d+)/);
            if (aMatch !== null) {
                var iStoryId = aMatch[1];

                // Fetch the work page
                jQuery.get('https://archiveofourown.org/works/' + iStoryId, function(oData) {
                    // Extract and reformat the publication date
                    var rawDate = jQuery(oData).find('dd.published').text().trim();
                    if (rawDate) {
                        // Reformat the date to '05 Oct 2020'
                        var formattedDate = new Intl.DateTimeFormat('en-GB', {
                            day: '2-digit',
                            month: 'short',
                            year: 'numeric'
                        }).format(new Date(rawDate));

                        // Append the formatted date to the story header
                        jQuery(oHeader).append(html_date_heading + '&nbsp;' +  formattedDate + '</span></strong></span>&nbsp;');
                    }
                }).fail(function() {
                    console.log('Failed to fetch publication date for story ID:', iStoryId);
                });
            }
        });
    }

})();