MediaWiki:Common.js

From PBARC
Revision as of 20:20, 14 November 2023 by KJ7RRV (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
document.querySelector("li#footer-info-lastmod").innerHTML += ' Article text is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-ShareAlike 4.0 International</a>, unless otherwise noted. Contributors have agreed that a link to the article is sufficient attribution. This license does not apply to any pictures that may be used in the page. Click a picture to check for licensing information. If no license is given, the image may not be reused. '

// Wikipedia citation tool
const textbox = document.querySelector("#wpTextbox1")

if (textbox) {
    const summaryBox = document.querySelector(
        "#wpSummaryLabel > div:nth-child(1)"
    )

    const label = summaryBox.querySelector("label")
    label.setAttribute("for", "citations")
    label.textContent =
        'If you quoted a Wikipedia article, paste its Web address (starting with "https://en.wikipedia.org/") here, then click "Add citation." Do this for each article quoted. This is not necessary if the article is already cited in this page.'

    const input = summaryBox.querySelector("input")
    input.id = "citations"
    input.setAttribute("name", "citations")
    input.setAttribute("title", "Enter URLs to Wikipedia articles")

    const button = document.createElement("button")
    button.classList = ["oo-ui-inputWidget-input", "oo-ui-buttonElement-button"]
    button.style.marginBottom = "1em"
    button.textContent = "Add citation"
    summaryBox.appendChild(button)

    button.addEventListener("click", function (e) {
        e.preventDefault()

        const url = input.value

        const regex = /\/wiki\/(.+)/ // Regex pattern to extract the title
        const match = url.match(regex) // Perform matching

        if (url.search("en.wikipedia.org") != -1 && match && match.length > 1) {
            const title = decodeURIComponent(match[1]).replace(/_/g, " ")
            const citation = "{{Wikipedia|" + title + "}}"
            if (textbox.value.search(citation) == -1) {
                textbox.value = textbox.value.trim() + "\n\n" + citation
                input.value = ""
            }
        } else {
            alert("That URL does not seem to be a Wikipedia article.")
        }
    })
}
// End Wikipedia citation tool


if (
    document.querySelector("#p-personal-label").textContent == "Not logged in"
) {
    document.querySelectorAll("a.new, .new a").forEach(function (redlink) {
        var span = document.createElement("span")
        span.textContent = redlink.textContent
        redlink.replaceWith(span)
    })
}

// MobileEcho
var buttonsAdded = false

function echoLoading() {
    document.body.classList.add("mobileecho-hide-overflow")

    if (!buttonsAdded) {
        setTimeout(function () {
            var closeButton = document.createElement("button")
            closeButton.setAttribute(
                "class",
                "mobileecho-close-button oo-ui-widget oo-ui-widget-enabled oo-ui-iconElement oo-ui-iconElement-icon oo-ui-icon-close oo-ui-labelElement-invisible oo-ui-iconWidget"
            )

            var headers = document.querySelectorAll(".oo-ui-popupWidget-head")
            console.log(headers)
            var header

            for (header in headers) {
                var newCloseButton = closeButton.cloneNode()
                newCloseButton.addEventListener("click", function () {
                    document.body.click()
                })
                headers[header].appendChild(newCloseButton)
            }
        }, 500)
        buttonsAdded = true
    }

    setTimeout(function () {
        document
            .querySelector("a.oo-ui-icon-bell")
            .addEventListener("click", echoLoading)

        document
            .querySelector("a.oo-ui-icon-tray")
            .addEventListener("click", echoLoading)
    }, 500)
}

document.body.addEventListener("click", function (e) {
    if (
        !document.querySelector(".mw-echo-ui-overlay").contains(e.target) &&
        !document.querySelector("#personal-extra").contains(e.target)
    ) {
        document.body.classList.remove("mobileecho-hide-overflow")
    }
})

document
    .querySelector("a.oo-ui-icon-bell")
    .addEventListener("click", echoLoading)

document
    .querySelector("a.oo-ui-icon-tray")
    .addEventListener("click", echoLoading)

// End MobileEcho