MediaWiki:Common.js: Difference between revisions
From PBARC
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
// Wikipedia citation tool | // Wikipedia citation tool | ||
const textbox = document.querySelector( | const textbox = document.querySelector("#wpTextbox1") | ||
if (textbox) { | 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 | // End Wikipedia citation tool | ||
// CC BY-SA checkbox | // CC BY-SA checkbox | ||
if (document.querySelector( | 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="{"_":"OO.ui.CheckboxInputWidget","selected":true,"name":"wpCcbysa","inputId":"wpCcbysa","tabIndex":4,"accessKey":"w","required":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.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) | ||
}) | |||
} | } | ||
// End CC BY-SA checkbox | // End CC BY-SA checkbox | ||
// MobileEcho | // MobileEcho | ||
var buttonsAdded = false | var buttonsAdded = false | ||
function echoLoading() { | 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) | 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( | 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( | document | ||
.querySelector("a.oo-ui-icon-bell") | |||
) | .addEventListener("click", echoLoading) | ||
document.querySelector( | document | ||
.querySelector("a.oo-ui-icon-tray") | |||
) | .addEventListener("click", echoLoading) | ||
// End MobileEcho | // End MobileEcho | ||
Revision as of 16:45, 28 June 2023
// 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
// 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="{"_":"OO.ui.CheckboxInputWidget","selected":true,"name":"wpCcbysa","inputId":"wpCcbysa","tabIndex":4,"accessKey":"w","required":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"
) {
document.querySelectorAll("a.new, .new a").forEach(function (redlink) {
var span = document.createElement("span")
span.textContent = redlink.textContent
redlink.replaceWith(span)
})
}
// End CC BY-SA checkbox
// 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