/* Internet Archive directory listing searcher */ if(document.location.pathname.search("/download/") == 0) /* if directory listing viewed */ { var listing = document.getElementsByClassName("download-directory-listing")[0]; // create search bar in memory before inserting var search_bar = document.createElement("input"); search_bar.placeholder="search files"; search_bar.onkeyup=search_list; search_bar.style.marginRight="0.5em"; // create clear button and search result counter in memory before inserting var search_clear_button = document.createElement("button"); search_clear_button.style.marginRight="1em"; search_clear_button.innerHTML="clear"; search_clear_button.onclick=clear_search; // create "inverted" check box in memory var inverted_checkbox = document.createElement("input"); inverted_checkbox.type = "checkbox"; inverted_checkbox.onclick = search_list; // repeat search after changing mode inverted_checkbox.style.marginRight="0.5em"; // create checkbox label var inverted_checkbox_label = document.createElement("label"); inverted_checkbox_label.appendChild(inverted_checkbox); var span = document.createElement("span"); inverted_checkbox_label.appendChild(span); inverted_checkbox_label.lastChild.innerHTML="invert results"; inverted_checkbox_label.style.marginRight="1em"; // create "case-sensitive" check box in memory var case_sensitive_checkbox = document.createElement("input"); case_sensitive_checkbox.type = "checkbox"; case_sensitive_checkbox.onclick = search_list; // repeat search after changing mode case_sensitive_checkbox.style.marginRight="0.5em"; // create checkbox label var case_sensitive_checkbox_label = document.createElement("label"); case_sensitive_checkbox_label.appendChild(case_sensitive_checkbox); var span = document.createElement("span"); case_sensitive_checkbox_label.appendChild(span); case_sensitive_checkbox_label.lastChild.innerHTML="case-sensitive"; case_sensitive_checkbox_label.style.marginRight="1em"; // create result counter field in memory var result_counter = document.createElement("span"); var listing_hr = listing.getElementsByTagName("hr")[0]; listing.insertBefore(search_bar, listing_hr); // insert search bar into page listing.insertBefore(search_clear_button, listing_hr); // insert "clear" button into page listing.insertBefore(inverted_checkbox_label, listing_hr); // insert "invert results" checkbox listing.insertBefore(case_sensitive_checkbox_label, listing_hr); // insert "case-sensitive" checkbox listing.insertBefore(result_counter, listing_hr); // insert result counter into page search_bar.focus(); // actuate search bar without clicking or tapping on it var list_items = listing.getElementsByTagName("tr"); var count = 0; // initialize counter for "for" loops var result_count = 0; // initialize result counter } function search_list() { result_count = 0; // reset result counter search_bar.setAttribute("value",search_bar.value); // compatibility var search_input = search_bar.value; var searched_content = ""; // initialize to eliminate JSHint error if (! case_sensitive_checkbox.checked) { // convert to case-insensitive search_input = search_input.toLowerCase(); } // The inverted checkbox status is not checked inside the "for" loop for efficiency. A pice of code that needs to be run many times should be a short as possible for improving performance. if (! inverted_checkbox.checked) /* meaning unchecked */ { for (count=2 /* skip "go to parent directory" */; count < list_items.length; count++) { // perform search searched_content=list_items[count].innerHTML; if (! case_sensitive_checkbox.checked) { // convert to case-insensitive if checkbox not check searched_content = searched_content.toLowerCase(); } if (searched_content.search(search_input)<0) /* if no match */ { list_items[count].style.display="none"; // hide } else { list_items[count].style.display=""; // fall back to original CSS by resetting to undefined result_count++; // count results } } } else { for (count=2 /* skip "go to parent directory" */; count < list_items.length; count++) { // perform inverted search searched_content=list_items[count].innerHTML; if (! case_sensitive_checkbox.checked) { // convert to case-insensitive if checkbox not check searched_content = searched_content.toLowerCase(); } if (searched_content.search(search_input)>-1) /* if match */ { list_items[count].style.display="none"; // hide } else { list_items[count].style.display=""; // fall back to original CSS by resetting to undefined result_count++; // count results } } } result_counter.innerHTML = result_count; if (result_count == 1) result_counter.innerHTML += " result"; // singular else result_counter.innerHTML += " results"; // plural } function clear_search() { search_bar.value=""; search_list(); }