User:Yair rand/FindTrans.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • 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.

if (mw.config.get("wgPageName") === "Special:Search")
	$(document).ready(function($) {
		var redlink = true;
		var $a = $(".mw-search-createlink .new");
		if ($a.length === 0) {
			$a = $(".mw-search-createlink a");
			redlink = false;
		}
		if ($a.length === 0) {
			return;
		}
		var htitle = $a.text(),
			href = $a.attr("href"),
			$s = $(".searchresults");
		$.get("/w/api.php", {
			format: "json",
			action: 'query',
			generator: 'backlinks',
			gbltitle: htitle,
			gblnamespace: 0,
			prop: 'revisions',
			rvprop: 'content'
		}, function(b) {
			if (!b || !b.query) {
				return;
			}
			b = b.query.pages;
			for (var i in b) {
				var c = b[i].revisions[0]['*'],
					title = b[i].title,
					r = new RegExp("\\n\\*+:?\\s+\\[?\\[?([^\\]:\\n]+)\\]?\\]?:[^\\n]*\\{\\{t[-+ø]?\\|([^|]+)\\|" + htitle + "(\\|[^\\}\\|]+)*\\}\\}", "g"),
					m,
					rr = /\{\{trans-top(\|?[^\}]*)\}\}/g,
					transtops = [];
				for (; m = rr.exec(c);) {
					transtops.push(m);
				}
				for (; m = r.exec(c);) {
					for (; transtops[1] && m.index > transtops[1].index;) {
						transtops.shift();
					}
					var mo = {
						gloss: transtops[0][1] && transtops[0][1].substr(1),
						langname: m[1],
						langcode: m[2]
					};
					if (mo.langname.substr(0, 6) === "\{\{ttbc") {
						continue;
					}
					for (; m.length > 3;) {
						var p = m.pop();
						if (p && (p = p.split("=")).length === 2) {
							mo[p[0].substr(1)] = p[1];
						}
					}
					$s.prepend(
					
					
					$("<p>")
					
					.append(
					$("<a>", {
						addClass: (mo.sc || "Latn") + " mention-" + (mo.sc || "Latn") + (redlink ? " new" : ""),
						lang: mo.langcode,
						href: href + (redlink ? "" : "#" + mo.langname),
						style: "font-weight: 700;"
					}).text(mo.alt || htitle))
					
					.append(document.createTextNode(" is a" + ("AEIOU".indexOf(mo.langname.charAt(0)) === -1 ? " " : "n ") + mo.langname + " translation of the word "))
					.append(
					$("<a>", {
						style: "font-weight: 700;",
						href: "/wiki/" + title
					}).text(title).addClass("mention-Latn"))
					.append(document.createTextNode((mo.gloss ? " (\"" + mo.gloss + "\")" : "") + ".")));
				}
			}
		});
	});