User:TheTVExpert/searchForSection.js
Appearance
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:TheTVExpert/searchForSection. |
function autocomplete(inp, arr) {
var currentFocus;
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.appendChild(a);
for (i = 0; i < arr.length; i++) {
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
b = document.createElement("DIV");
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
b.addEventListener("click", function(e) {
inp.value = this.getElementsByTagName("input")[0].value;
closeAllLists();
});
a.appendChild(b);
}
}
});
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) {
currentFocus--;
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
var makeInput = function(plain,other)
{
var div = document.createElement("DIV");
div.setAttribute("class", "autocomplete");
div.style.width = "300px";
var input = document.createElement("INPUT");
input.setAttribute("id", "input");
input.setAttribute("type", "text");
input.setAttribute("placeholder", "Enter a Section Name and Press Enter");
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
for(i=0;i<plain.length;i++){
if(plain[i]==this.value)
{
window.open("#"+other[i],"_self");
this.value = "";
document.getElementById("modal").style.display = "none";
break;
}
}
}
});
input.style.left = "20%";
div.appendChild(input);
document.getElementById("modalContent").appendChild(div);
var style = document.createElement("STYLE");
style.innerHTML = `
* { box-sizing: border-box; }
.autocomplete {
position: relative;
display: inline-block;
}
input {
border: 1px solid transparent;
background-color: #f1f1f1;
padding: 10px;
font-size: 16px;
}
input[type=text] {
background-color: #f1f1f1;
width: 100%;
}
.autocomplete-items {
position: absolute;
border: 1px solid #d4d4d4;
border-bottom: none;
border-top: none;
z-index: 99;
top: 100%;
left: 0;
right: 0;
}
.autocomplete-items div {
padding: 10px;
cursor: pointer;
background-color: #fff;
border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
/*when hovering an item:*/
background-color: #e9e9e9;
}
.autocomplete-active {
background-color: DodgerBlue !important;
color: #ffffff;
}
`;
document.head.appendChild(style);
};
var makeModal = function(title)
{
var modal = document.createElement("DIV");
modal.id = "modal";
modal.style.display = "none";
modal.style.position = "fixed";
modal.style.zIndex = "1";
modal.style.left = "0";
modal.style.top = "0";
modal.style.width = "100%";
modal.style.height = "100%";
modal.style.overflow = "auto";
modal.style.backgroundColor = "rgb(0,0,0)";
modal.style.backgroundColor = "rgba(0,0,0,0.4)";
var content = document.createElement("DIV");
content.id = "modalContent";
content.style.backgroundColor = "#fefefe";
content.style.margin = "15% auto";
content.style.padding = "20px";
content.style.border = "1px solid #888";
content.style.width = "350px";
var name = document.createElement("H3");
name.innerHTML = title;
name.style.textAlign = "center";
name.style.textDecoration = "underline";
content.appendChild(name);
modal.appendChild(content);
document.body.appendChild(modal);
//modal.style.display = "block";
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
};
var listSections = function(title,read)
{
var result;
var list = [];
$('.mw-headline').each(function() {
result = this.id;
if(read)
{
result = result.replace(/_/g," ");
}
//alert( result );
list.push(result);
});
/*alert(title);
for(i=0;i<list.length;i++)
{
alert(list[i]);
console.log(list[i]);
}*/
return list;
};
var check = document.getElementsByClassName('mw-headline')[0];
if(check !== undefined && !document.getElementById("wikiPreview"))
{
var first = true;
var page = mw.config.get('wgPageName');
var pageNoUnderscores = page.replace(/_/g," ");
var plain;
var other;
/*var intervalId = setInterval(function(){
var x = document.activeElement.id;
if(x == "input"){
//alert("");
}
})*/
var button = document.createElement("BUTTON");
button.id = "button";
button.style.position = "fixed";
button.style.zIndex = "1";
button.style.left = "0";
button.style.bottom = "0";
button.innerHTML = "Search for a section.";
document.body.appendChild(button);
$("button").click(function()
{
if(first)
{
plain = listSections(pageNoUnderscores,true);
other = listSections(pageNoUnderscores,false);
makeModal(pageNoUnderscores);
first = false;
makeInput(plain,other);
}
document.getElementById("modal").style.display = "block";
autocomplete(document.getElementById("input"), plain);
});
document.onkeyup = function(e) {
if (e.altKey && e.shiftKey && e.which == 65) {
if(first)
{
plain = listSections(pageNoUnderscores,true);
other = listSections(pageNoUnderscores,false);
makeModal(pageNoUnderscores);
first = false;
makeInput(plain,other);
}
document.getElementById("modal").style.display = "block";
autocomplete(document.getElementById("input"), plain);
}
};
}