User:Ahecht/Scripts/TemplateSearch.js
Appearance
< User:Ahecht | Scripts
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Ahecht/Scripts/TemplateSearch. |
/*
Based on [[User:SiBr4/TemplateSearch.js]] by [[User:SiBr4]]
Allows using "TP:" and "{{" as shortcuts for "Template:" in the search box. See
[[Wikipedia:Village pump (proposals)/Archive 127#Prefix suggestion: TP: for Template:]].
Install by adding the following row to your [[Special:MyPage/common.js]] or [[Special:MyPage/skin.js]]:
mw.loader.load( "//en.wikipedia.org/w/index.php?title=User:Ahecht/Scripts/TemplateSearch.js&action=raw&ctype=text/javascript" ); // Linkback: [[:en:User:Ahecht/Scripts/TemplateSearch.js]]
<syntaxhighlight lang="javascript">
/**/
function addSearchBoxChangeListener(searchboxes) {
searchboxes.forEach(function (searchbox) {
if (searchbox.classList.contains("cdx-text-input__input") || searchbox.classList.contains("search") || searchbox.id == "searchInput") {
searchbox.setAttribute("onkeyup","replaceBracesInSearch(this);");
replaceBracesInSearch(searchbox);
}
} );
}
var SearchRegexes = SearchRegexes || {"^(\\\{\\\{|[Tt][Pp]:)":"Template:","\\\}\\\}$":""};
function replaceBracesInSearch(box) {
for (var search in SearchRegexes) {
re = new RegExp(search);
var replace = SearchRegexes[search];
if(re.test(box.value)) {
box.value=box.value.replace(re,replace);
}
}
}
$.when( $.ready ).then(function() {
addSearchBoxChangeListener(document.getElementsByName("search"));
var x = new MutationObserver(function (e) {
addSearchBoxChangeListener(document.getElementsByName("search"));
});
document.querySelectorAll(".search-box, .vector-search-box").forEach(function (box) {
x.observe(box, { subtree: true, childList: true });
} );
} );
//</syntaxhighlight>