Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name OperaStyle_menuAccessKey.uc.js
// @namespace http://d.hatena.ne.jp/Griever/
// @author Griever
// @include main
// @version 0.0.2くらい
// @compatibility Firefox 4
// ==/UserScript==
(function(){
window.menuAccessKey = {
hintkeys: new String('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'),
addTarget: [
$('contentAreaContextMenu'),
, $('placesContext')
, $('backForwardMenu')
, $('navigator-toolbox')
, $('browser-bottombox')
, document.getElementById('tabContextMenu') ||
document.getAnonymousElementByAttribute(gBrowser, 'anonid', 'tabContextMenu')
],
init: function(){
this.addTarget.forEach(function(elem){
if (elem)
elem.addEventListener('popupshowing', this, false);
}, this);
window.addEventListener('unload', this, false);
},
uninit: function(){
this.addTarget.forEach(function(elem){
if (elem)
elem.removeEventListener('popupshowing', this, false);
}, this);
window.removeEventListener('unload', this, false);
},
handleEvent: function(e){
switch (e.type) {
case 'unload':
this.uninit();
return;
case 'popupshowing':
this.popupshowing(e);
break;
}
},
popupshowing: function(e) {
var popup = e.originalTarget;
if (popup.localName.indexOf('popup') == -1)
return;
var hintkeys = this.hintkeys.toUpperCase();
// popupshown よりは早く動いて欲しい
var self = this;
setTimeout(function(){
// accesskey を付ける要素を取得し、hintkeys から存在する accesskey を削除
var list = Array.filter(popup.children, function(elem){
if (!elem.hasAttribute('label') || elem.clientHeight === 0)
return false;
var accesskey = elem.getAttribute('accesskey');
if (accesskey) {
hintkeys = hintkeys.replace(accesskey.toUpperCase(), '');
}else{
return true;
}
});
self.addAccessKey(list, hintkeys);
}, 0);
/* アクセスキーを消す
popup.addEventListener('popuphidden', function(e){
if (e.target != popup)
return;
popup.removeEventListener(e.type, arguments.callee, false);
self.removeAccessKey(list);
}, false);
*/
},
addAccessKey: function(list, hintkeys){
list.some(function(elem) {
if (hintkeys === '')
return true;
var key;
if (new RegExp('[' + hintkeys + ']', "i").test(elem.getAttribute('label'))) {
key = RegExp.lastMatch.toUpperCase();
hintkeys = hintkeys.replace(key, '');
} else {
key = hintkeys[0];
hintkeys = hintkeys.substr(1);
}
elem.setAttribute("accesskey", key);
});
},
removeAccessKey: function(list){
list.forEach(function(elem) {
if (elem)
elem.removeAttribute("accesskey");
});
},
}
window.menuAccessKey.init();
function $(id) document.getElementById(id);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment