User:DexterCD/wandCopy.js
Jump to navigation
Jump to search
In other languages: 日本語 • Български • Deutsch • Ελληνικά • English • Español • Français • Magyar • Italiano • 한국어 • Nederlands • Polski • Português • Português do Brasil • Русский • Türkçe • 中文
CSS and Javascript changes must not modify the wiki.gg branding or advertisements.
Note: After saving, 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 | Hold Ctrl while clicking Refresh, or press Ctrl-F5
|
Opera | Clear the cache in Tools → Preferences |
// Copyright Dexter Döpping, 2023
// MIT licensed: https://opensource.org/license/mit/
$(document).ready(async () => {
const wand_spells_cards = $(".wand-spells-card")
const wand_cards = $(".wand-card")
// Don't do unnecessary work if there are no wand templates on the page
if (wand_spells_cards.length === 0 && wand_cards.length === 0)
return;
const page_name = mw.config.get("wgPageName")
const response = await fetch(`https://noita.wiki.gg/api.php?action=query&prop=revisions&titles=${page_name}&rvslots=*&rvprop=content&formatversion=2&format=json`)
const page_data = await response.json()
const page_markup = page_data.query.pages[0].revisions[0].slots.main.content
const get_templates = (template_name) => {
const re = new RegExp(`{{\\s*${template_name}\\s*\\|[^}]*}}`, "g")
return Array.from(page_markup.matchAll(re))
}
const add_copy_button = (elem, template_text) => {
const button = $("<button>Copy Template</button>", {
type: "button",
})
button.on("click", async () => {
await navigator.clipboard.writeText(template_text)
})
button.prependTo(elem)
}
const wand_spell_templates = get_templates("Wand")
for (let i = 0; i != wand_spell_templates.length; ++i) {
add_copy_button(wand_spells_cards[i], wand_spell_templates[i])
}
const wand_card_templates = get_templates("Wand Card")
for (let i = 0; i != wand_card_templates.length; ++i) {
add_copy_button(wand_cards[i], wand_card_templates[i])
}
})