User:V111P/PrefLangs.js

// Preferred search languages, https://en.wiktionary.org/wiki/User:V111P/PrefLangs.js, v. 2018/09/06 // // Type the name of your preferred language(s) in the textbox, then search for a word and get redirected // to the heading with the name of your preferred language, if it exists on the page.

(function {    var pageIsSearchResult = localStorage.getItem('PageIsSearchResult');    var langsStr = localStorage.getItem('LangsToSearchFor') || '';

$('#firstHeading').before('');

localStorage.removeItem('PageIsSearchResult'); $('#searchform').submit(function (e) {       var newLangsStr = $('#preferredLangs').val                          .replace(/[\s\uFEFF\xA0]+/g, ' ').replace(/ ?, ?/g, ', ')                          .replace(/^ |[ ,]+$|/g, ).substring(0, 1000);        if (newLangsStr === ) { // search with empty LangNames box to delete the localStorage entries            localStorage.removeItem('LangsToSearchFor');        } else {            localStorage.setItem('LangsToSearchFor', newLangsStr);            localStorage.setItem('PageIsSearchResult', true);        }    });

if (pageIsSearchResult && location.href.indexOf('#') == -1) { var langs = langsStr.split(', '); for (var i = 0; i < langs.length; i++) { var l = langs[i].replace(/ /g, '_'); if (l === '') continue; try { if ($('#' + l).length > 0) { location.href += '#' + l;                   break; }               else { // only works for one-word language names l = l.charAt(0).toUpperCase + l.slice(1).toLowerCase; if ($('#' + l).length > 0) { location.href += '#' + l;                       break; }               }            } catch (e) {} }   }

});