User:Lupin/sectionkeys.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//window.popupShortcutKeys=false;

function getAnchors() {
  var s=[''], anchs=document.getElementsByTagName('A');
  for (var i=0; i<anchs.length; ++i) if (anchs[i].name && anchs[i].name!='top')
    s.push(anchs[i].name);
  return s;
}

window.handleKeys=function(evt){
  if (window.handleKeys.disabled) {
    return true;
  }
  var k = window.event ? window.event.keyCode : ( evt.keyCode ? evt.keyCode : evt.which);
  if (!k) return;
  var letter=String.fromCharCode(k);
  switch (letter) {
    case 'j': changeAnchor(1);       return false;
    case 'J': changeAnchor('last');  return false;
    case 'k': changeAnchor(-1);      return false;
    case 'K': changeAnchor('first'); return false;
    default: return true;
  }
}
window.handleKeys.disabled=false;
window.handleKeys.disableFlags=[];

function setupFocusHandlers(elType, startAt) {
  var inputs=document.getElementsByTagName(elType);
  var j=startAt || 0;
  for (var i=0; i<inputs.length; ++i) {
    if (elType=='input' && inputs[i].type.toLowerCase()!='text') { continue; }
    window.handleKeys.disableFlags[j]=false;
    inputs[i].onfocus=function(){ window.handleKeys.disableFlags[j]=true; updateDisabledFlag(); }
    inputs[i].onblur=function(){ window.handleKeys.disableFlags[j]=false; updateDisabledFlag(); }
    ++j;
  }
  //alert(j + ' handlers installed');
  return j;
}


function updateDisabledFlag() {
  for (var i=0; i<window.handleKeys.disableFlags.length; ++i) {
    if (window.handleKeys.disableFlags[i]===true) {
      window.handleKeys.disabled=true;
      return true;
    }
  }
  window.handleKeys.disabled=false;
  return false;
}


var anchs, pos;
window.changeAnchor=function(step, loop) {
  if(!anchs && !(anchs=getAnchors()) ) return;
  var loc=document.location.toString().split('#');
  switch (step) {
    case 'first': pos=0; break;
    case 'last':  pos=anchs.length-1; break;
    default:
      if (loc.length > 0) {
	var target=loc.slice(1).join('#');
	for (var i=0; i<anchs.length; ++i) { if (anchs[i]==target) { pos=i; break; } }
      }
      if(typeof pos==='number') {
	pos += step;
	if (loop) { while(pos < 0) pos += anchs.length; pos %= anchs.length; }
	else { if (pos<0) pos=0; if(pos>=anchs.length) pos=anchs.length-1; }
      }
      else pos = 0;
  } /* switch */
  var newloc=loc[0] + '#' + anchs[pos];
  if(document.location!=newloc)  document.location=newloc;
}


var sectionKeysPage=( ! /^(Editing|Search -|Confirm|Move page|Logs|E-mail user|All pages|Block user)/.test(document.title)
			&&
			! /([?&]=|wiki\/)[Ss]pecial:/.test(document.location) );
if ( 1 || sectionKeysPage ) {
  addOnloadHook(function(){
      var j=setupFocusHandlers('input');
      setupFocusHandlers('textarea', j);
      document.onkeypress=window.handleKeys;
    });
 }
Retrieved from "https://en.wikipedia.org/w/index.php?title=User:Lupin/sectionkeys.js&oldid=68950846"