User:Dixtosa/nymReferer.js
Jump to navigation
Jump to search
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
// Description : adds several parameters to redlinks so that after clicking it
// wikitext will be automatically filled with some initial data
// Dependency : None
// UserConfig : None
// Problems/to-do : the assumptions written below
// UserConfig for creationrules?
// get definition for synonyms
// use modules for rules?
// types of greenifying: translation, from synonym, from antonym, alternative form,
// (?) related terms, etymology
// <nowiki>
var basicPartsOfSpeech = ["Noun", "Proper noun", "Phrase", "Adjective", "Adverb", "Verb", "Verbal noun"]
.map((pos) => pos.toLowerCase() + "s"); //Georgian nouns
function pasteIfReferred(){
let word = mw.util.getParamValue("from");
if (word === null) return true; //sanity check
let header = mw.util.getParamValue("heading");
let langname = mw.util.getParamValue("langname");
let langcode = mw.util.getParamValue("langcode");
let partOfSpeech = mw.util.getParamValue("partOfSpeech") || "PARTOFSPEECH";
let gloss = mw.util.getParamValue("gloss");
let entry = `\
==${langname}==
===${partOfSpeech}===
{{head|${langcode}|${partOfSpeech.toLowerCase()}}}
# `;
if (header == "Translations")
{
entry += "[[" + word + "]]";
if (gloss)
entry += " {{gloss|" + gloss + "}}";
}
else if (header == "Alternative forms") {
entry += `{{alternative form of|${word}|lang=${langcode}}}`;
}
else if (header == "Synonyms") {
entry += `{{synonym of|${word}|lang=${langcode}}}`;
}
else {
entry += "\n\n====" + header + "====";
entry += `\n* {{l|${langcode}|${word}}}`;
}
$("#wpTextbox1").val(entry);
return false;
}
nymConfig = window.nymConfig || {
carelangs: ["ka"]
};
$( function () {
if (pasteIfReferred() && mw.config.values.wgAction == "view" && !mw.config.values.wgCanonicalSpecialPageName)
{
//it is assumed that <a>nchor is in between <span>
$("a.new")
.filter(function(){ return nymConfig.carelangs.includes($(this).parent().attr("lang"));})
.each(function(){
elem = $(this);
let header = elem.parentsUntil("#mw-content-text").last().prevUntil(":header").addBack().prev().filter(":header");// filter is due to andSelf
if (header.length === 0) //a.new's that are without header (e.g. {{also|title_that_does_not_exist}})
return ; //should not happen
let parentOfHeader = "h" + (header.prop("tagName")[1] - 1);
let headingTitle = header.children().first().text();
let partOfSpeech = null;
let langname = header.prevUntil("h2").last().prev().children().first().text();
let langcode = elem.parent().attr("lang");
let nymData = {};
if (!["Alternative forms", "Synonyms", "Antonyms", "Translations"].includes(headingTitle)) return ;
if (["Synonyms", "Antonyms", "Translations"].includes(headingTitle))
{
partOfSpeech = header.prevUntil(parentOfHeader).last().prev().children().first().text();
}
if (headingTitle == "Translations")
{
var nf = elem.closest(".NavFrame").children().first().clone(); nf.find("span").remove();
nymData.gloss = nf.text(); //.slice(1, -3);
try {
langname = elem.closest("li").contents()[0].wholeText.slice(0, -2);//trim ': '
}
catch(e) {
langname = "{{subst:#invoke:languages/templates|getByCode|"+langcode+"|getCanonicalName}}";
}
}
if (headingTitle == "Alternative forms")
{
let poses = [];
$("#mw-normal-catlinks ul li a").each(function(){
let catname = $(this).text();
if (catname.startsWith(langname))
{
let pos = catname.slice(langname.length + 1);
if (basicPartsOfSpeech.includes(pos))
{
let [first, ...rest] = pos.slice(0, -1);
poses.push(first.toUpperCase() + rest.join(""));
}
}
});
partOfSpeech = poses[0];
}
nymData.from = mw.config.values.wgTitle;
nymData.heading = headingTitle;
nymData.langname = langname;
nymData.partOfSpeech = partOfSpeech;
nymData.langcode = langcode;
elem.attr("href", elem.attr("href") + "&" + $.param(nymData)).css("color", "rgb(34, 204, 0)");
});
}
});
//</nowiki>