MediaWiki:Common.js: Difference between revisions

From PBARC
No edit summary
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
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 automatically apply to any pictures or other files that may be used or linked to in the page. Click a picture to check for licensing information. If no license is given, the image may not be reused without explicit permission from the copyright holder except under <a href="https://en.wikipedia.org/wiki/Fair_use">fair use</a>.'


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


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


  const label = summaryBox.querySelector("label")
    const label = summaryBox.querySelector("label")
  label.setAttribute("for", "citations")
    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."
    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")
    const input = summaryBox.querySelector("input")
  input.id = "citations"
    input.id = "citations"
  input.setAttribute("name", "citations")
    input.setAttribute("name", "citations")
  input.setAttribute("title", "Enter URLs to Wikipedia articles")
    input.setAttribute("title", "Enter URLs to Wikipedia articles")


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


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


    const url = input.value
        const url = input.value


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


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


// CC BY-SA checkbox
if (document.querySelector('.mw-newarticletext')) {
  const checkboxWrapper = document.createElement('div')
  checkboxWrapper.innerHTML = '<div id="mw-editpage-ccbysa" class="oo-ui-layout oo-ui-labelElement oo-ui-fieldLayout oo-ui-fieldLayout-align-inline"><div class="oo-ui-fieldLayout-body"><span class="oo-ui-fieldLayout-field"><span id="wpCcbysaWidget" class="oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-checkboxInputWidget" data-ooui="{&quot;_&quot;:&quot;OO.ui.CheckboxInputWidget&quot;,&quot;selected&quot;:true,&quot;name&quot;:&quot;wpCcbysa&quot;,&quot;inputId&quot;:&quot;wpCcbysa&quot;,&quot;tabIndex&quot;:4,&quot;accessKey&quot;:&quot;w&quot;,&quot;required&quot;:false}"><input type="checkbox" tabindex="4" accesskey="w" name="wpCcbysa" value="" id="wpCcbysa" class="oo-ui-inputWidget-input" checked="checked"><span class="oo-ui-checkboxInputWidget-checkIcon oo-ui-widget oo-ui-widget-enabled oo-ui-iconElement-icon oo-ui-icon-check oo-ui-iconElement oo-ui-labelElement-invisible oo-ui-iconWidget oo-ui-image-invert"></span></span></span><span class="oo-ui-fieldLayout-header"><label title="Publish this page under CC BY-SA" for="wpCcbysa" class="oo-ui-labelElement-label">Publish under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank">CC BY-SA 4.0 International</a> license</label></span></div></div>'
  document.querySelector(".editCheckboxes").replaceWith(checkboxWrapper)
  const checkbox = checkboxWrapper.firstChild
  document.querySelector("textarea").value = "\n<!-- Please do not edit this line; keep all content above it -->{{CC BY-SA}}"
  checkbox.querySelector("input").addEventListener('change', function(e) {
    area = document.querySelector("textarea")
    if (e.currentTarget.checked) {
      area.value = area.value.replaceAll("{{CC BY-SA}}", "").replaceAll("<!-- Please do not edit this line; keep all content above it -->", "").trimEnd() + "\n<!-- Please do not edit this line; keep all content above it -->{{CC BY-SA}}"
    } else {
      area.value = area.value.replaceAll("{{CC BY-SA}}", "").replaceAll("<!-- Please do not edit this line; keep all content above it -->", "").trimEnd()
    }
  });
  document.querySelector(".editCheckboxes").firstChild.appendChild(checkbox)
}


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


// MobileEcho
// MobileEcho
var buttonsAdded = false;
var buttonsAdded = false


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


if (! buttonsAdded) {
    if (!buttonsAdded) {
setTimeout(
        setTimeout(function () {
function() {
            var closeButton = document.createElement("button")
var closeButton = document.createElement('button');
            closeButton.setAttribute(
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");
                "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');
            var headers = document.querySelectorAll(".oo-ui-popupWidget-head")
console.log(headers)
            console.log(headers)
var header;
            var header


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


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


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


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


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


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


// End MobileEcho
// End MobileEcho

Latest revision as of 17:13, 15 May 2024

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 automatically apply to any pictures or other files that may be used or linked to in the page. Click a picture to check for licensing information. If no license is given, the image may not be reused without explicit permission from the copyright holder except under <a href="https://en.wikipedia.org/wiki/Fair_use">fair use</a>.'

// 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