Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satyr/83233 to your computer and use it in GitHub Desktop.
Save satyr/83233 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name OperaStyle_menuAccessKey.uc.js
// @namespace http://d.hatena.ne.jp/Griever/
// @include main
// ==/UserScript==
var menuAccessKey = {
HINTKEYS : new String('abcdefghijklmnopqrstuvwxyz0123456789'),
init : function(){
window.addEventListener('popupshown', this, false);
window.addEventListener('unload', this, false);
},
uninit : function(){
window.removeEventListener('popupshown', this, false);
window.removeEventListener('unload', this, false);
},
getItems : function(elem){
if (!/^(menu|menuitem)$/i.test(elem.localName)) return false;
if (elem.hasAttribute("accesskey")){
this.hintKeys = this.hintKeys.replace(elem.getAttribute("accesskey"), '');
return false;
}
return true
},
setAccessKey : function(elem){
if (this.hintKeys == '') return true;
var key = elem.label.match( RegExp('[' + this.hintKeys + ']', 'i') );
if (key){
elem.setAttribute("accesskey", key[0]);
this.hintKeys = this.hintKeys.replace(key[0].toLowerCase(), '');
}else{
elem.setAttribute("accesskey", this.hintKeys[0]);
this.hintKeys = this.hintKeys.substr(1);
}
},
handleEvent : function(event){
if (event.type == 'unload'){
this.uninit();
return false;
}
this.hintKeys = this.HINTKEYS;
Array.filter(event.target.children, this.getItems , this)
.some(this.setAccessKey, this);
},
}
menuAccessKey.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment