XKCD - Display Title Below Comic

Display the title text ("alt-text") of XKCD comics below the comic

安裝腳本?
作者推薦腳本

您可能也會喜歡 XKCD Explain Button

安裝腳本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         XKCD - Display Title Below Comic
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Display the title text ("alt-text") of XKCD comics below the comic
// @author       Jonah Lawrence (youtube.com/DevProTips)
// @match        *://*.xkcd.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // when the page has loaded
    window.addEventListener("load", function () {
        // search for the first element in the comic with a title attribute
        const titleElement = document.querySelector("#comic [title]")

        // if the element exists
        if (titleElement) {
            // get the title attribute of the element
            const title = titleElement.title

            // create a paragraph element
            let p = document.createElement("p")

            // set the text to the title attribute
            p.innerText = title

            // create object to contain styles
            const styles = {
                fontVariant: 'none',    // remove small caps font variant
                background: '#aaaaaa',  // add a light gray background
                padding: '15px',        // add padding around the text
                maxWidth: '100vw',      // set the max width to the viewport width
                margin: '5px auto'      // Add 5px on top and bottom and center horizontally
            }

            // apply all styles to the paragraph
            Object.assign(p.style, styles)

            // append the paragraph at the end of the comic
            document.querySelector("#comic").append(p)
        }
    }, false)
})();