2018-07-19 00:00:55 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2018-present Raymond Hill
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
(( ) => {
|
2018-07-19 00:00:55 +02:00
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/commands#Shortcut_values
|
2019-09-17 21:15:01 +02:00
|
|
|
const validStatus0Keys = new Map([
|
2018-07-22 14:14:50 +02:00
|
|
|
[ 'alt', 'Alt' ],
|
|
|
|
[ 'control', 'Ctrl' ],
|
2018-07-19 00:00:55 +02:00
|
|
|
]);
|
2019-09-17 21:15:01 +02:00
|
|
|
const validStatus1Keys = new Map([
|
2018-07-21 00:03:32 +02:00
|
|
|
[ 'a', 'A' ],
|
|
|
|
[ 'b', 'B' ],
|
|
|
|
[ 'c', 'C' ],
|
|
|
|
[ 'd', 'D' ],
|
|
|
|
[ 'e', 'E' ],
|
|
|
|
[ 'f', 'F' ],
|
|
|
|
[ 'g', 'G' ],
|
|
|
|
[ 'h', 'H' ],
|
|
|
|
[ 'i', 'I' ],
|
|
|
|
[ 'j', 'J' ],
|
|
|
|
[ 'k', 'K' ],
|
|
|
|
[ 'l', 'L' ],
|
|
|
|
[ 'm', 'M' ],
|
|
|
|
[ 'n', 'N' ],
|
|
|
|
[ 'o', 'O' ],
|
|
|
|
[ 'p', 'P' ],
|
|
|
|
[ 'q', 'Q' ],
|
|
|
|
[ 'r', 'R' ],
|
|
|
|
[ 's', 'S' ],
|
|
|
|
[ 't', 'T' ],
|
|
|
|
[ 'u', 'U' ],
|
|
|
|
[ 'v', 'V' ],
|
|
|
|
[ 'w', 'W' ],
|
|
|
|
[ 'x', 'X' ],
|
|
|
|
[ 'y', 'Y' ],
|
|
|
|
[ 'z', 'Z' ],
|
|
|
|
[ '0', '0' ],
|
|
|
|
[ '1', '1' ],
|
|
|
|
[ '2', '2' ],
|
|
|
|
[ '3', '3' ],
|
|
|
|
[ '4', '4' ],
|
|
|
|
[ '5', '5' ],
|
|
|
|
[ '6', '6' ],
|
|
|
|
[ '7', '7' ],
|
|
|
|
[ '8', '8' ],
|
|
|
|
[ '9', '9' ],
|
|
|
|
[ 'f1', 'F1' ],
|
|
|
|
[ 'f2', 'F2' ],
|
|
|
|
[ 'f3', 'F3' ],
|
|
|
|
[ 'f4', 'F4' ],
|
|
|
|
[ 'f5', 'F5' ],
|
|
|
|
[ 'f6', 'F6' ],
|
|
|
|
[ 'f7', 'F7' ],
|
|
|
|
[ 'f8', 'F8' ],
|
|
|
|
[ 'f9', 'F9' ],
|
|
|
|
[ 'f10', 'F10' ],
|
|
|
|
[ 'f11', 'F11' ],
|
|
|
|
[ 'f12', 'F12' ],
|
|
|
|
[ ' ', 'Space' ],
|
|
|
|
[ ',', 'Comma' ],
|
|
|
|
[ '.', 'Period' ],
|
|
|
|
[ 'home', 'Home' ],
|
|
|
|
[ 'end', 'End' ],
|
|
|
|
[ 'pageup', 'PageUp' ],
|
|
|
|
[ 'pagedown', 'PageDown' ],
|
|
|
|
[ 'insert', 'Insert' ],
|
|
|
|
[ 'delete', 'Delete' ],
|
|
|
|
[ 'arrowup', 'Up' ],
|
|
|
|
[ 'arrowdown', 'Down' ],
|
|
|
|
[ 'arrowleft', 'Left' ],
|
|
|
|
[ 'arrowright', 'Right' ],
|
|
|
|
[ 'shift', 'Shift' ],
|
2018-07-19 00:00:55 +02:00
|
|
|
]);
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const commandNameFromElement = function(elem) {
|
2018-07-19 00:00:55 +02:00
|
|
|
while ( elem !== null ) {
|
2019-09-17 21:15:01 +02:00
|
|
|
const name = elem.getAttribute('data-name');
|
2018-07-19 00:00:55 +02:00
|
|
|
if ( typeof name === 'string' && name !== '' ) { return name; }
|
|
|
|
elem = elem.parentElement;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const captureShortcut = function(ev) {
|
|
|
|
const input = ev.target;
|
|
|
|
const name = commandNameFromElement(input);
|
2018-07-19 00:00:55 +02:00
|
|
|
if ( name === undefined ) { return; }
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const before = input.value;
|
|
|
|
const after = new Set();
|
2018-07-19 00:00:55 +02:00
|
|
|
let status = 0;
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const updateCapturedShortcut = function() {
|
2018-07-19 00:00:55 +02:00
|
|
|
return (input.value = Array.from(after).join('+'));
|
|
|
|
};
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const blurHandler = function() {
|
2018-07-19 00:00:55 +02:00
|
|
|
input.removeEventListener('blur', blurHandler, true);
|
|
|
|
input.removeEventListener('keydown', keydownHandler, true);
|
|
|
|
input.removeEventListener('keyup', keyupHandler, true);
|
|
|
|
if ( status === 2 ) {
|
2019-09-17 21:15:01 +02:00
|
|
|
vAPI.messaging.send('dashboard', {
|
|
|
|
what: 'setShortcut',
|
|
|
|
name,
|
|
|
|
shortcut: updateCapturedShortcut(),
|
|
|
|
});
|
2018-07-19 00:00:55 +02:00
|
|
|
} else {
|
|
|
|
input.value = before;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const keydownHandler = function(ev) {
|
2018-07-19 00:00:55 +02:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopImmediatePropagation();
|
|
|
|
if ( ev.code === 'Escape' ) {
|
|
|
|
input.blur();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( status === 0 ) {
|
2019-09-17 21:15:01 +02:00
|
|
|
const keyName = validStatus0Keys.get(ev.key.toLowerCase());
|
2018-07-22 14:14:50 +02:00
|
|
|
if ( keyName !== undefined ) {
|
|
|
|
after.add(keyName);
|
2018-07-19 00:00:55 +02:00
|
|
|
updateCapturedShortcut();
|
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-07-19 12:46:40 +02:00
|
|
|
if ( status === 1 ) {
|
2018-07-21 00:03:32 +02:00
|
|
|
if ( ev.key === 'Shift' ) {
|
2018-07-19 12:46:40 +02:00
|
|
|
after.add('Shift');
|
|
|
|
updateCapturedShortcut();
|
|
|
|
return;
|
|
|
|
}
|
2018-07-21 00:03:32 +02:00
|
|
|
let keyName = validStatus1Keys.get(ev.key.toLowerCase());
|
|
|
|
if ( keyName !== undefined ) {
|
|
|
|
after.add(keyName);
|
2018-07-19 12:46:40 +02:00
|
|
|
updateCapturedShortcut();
|
|
|
|
status = 2;
|
|
|
|
input.blur();
|
|
|
|
return;
|
|
|
|
}
|
2018-07-19 00:00:55 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const keyupHandler = function(ev) {
|
2018-07-19 00:00:55 +02:00
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopImmediatePropagation();
|
|
|
|
if ( status !== 1 ) { return; }
|
2019-09-17 21:15:01 +02:00
|
|
|
const keyName = validStatus0Keys.get(ev.key.toLowerCase());
|
2018-07-22 14:14:50 +02:00
|
|
|
if ( keyName !== undefined && after.has(keyName) ) {
|
2018-07-19 00:00:55 +02:00
|
|
|
after.clear();
|
|
|
|
updateCapturedShortcut();
|
|
|
|
status = 0;
|
|
|
|
return;
|
|
|
|
}
|
2018-07-21 00:03:32 +02:00
|
|
|
if ( ev.key === 'Shift' ) {
|
2018-07-19 00:00:55 +02:00
|
|
|
after.delete('Shift');
|
|
|
|
updateCapturedShortcut();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
input.value = '';
|
|
|
|
input.addEventListener('blur', blurHandler, true);
|
|
|
|
input.addEventListener('keydown', keydownHandler, true);
|
|
|
|
input.addEventListener('keyup', keyupHandler, true);
|
|
|
|
};
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const resetShortcut = function(ev) {
|
|
|
|
const name = commandNameFromElement(ev.target);
|
2018-07-19 00:00:55 +02:00
|
|
|
if ( name === undefined ) { return; }
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const input = document.querySelector('[data-name="' + name + '"] input');
|
2018-07-19 00:00:55 +02:00
|
|
|
if ( input === null ) { return; }
|
|
|
|
input.value = '';
|
2019-09-17 21:15:01 +02:00
|
|
|
vAPI.messaging.send('dashboard', {
|
|
|
|
what: 'setShortcut',
|
|
|
|
name,
|
|
|
|
});
|
2018-07-19 00:00:55 +02:00
|
|
|
};
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const onShortcutsReady = function(commands) {
|
2018-07-19 00:00:55 +02:00
|
|
|
if ( Array.isArray(commands) === false ) { return; }
|
2019-09-17 21:15:01 +02:00
|
|
|
const template = document.querySelector('#templates .commandEntry');
|
|
|
|
const tbody = document.querySelector('.commandEntries tbody');
|
|
|
|
for ( const command of commands ) {
|
2018-07-19 00:00:55 +02:00
|
|
|
if ( command.description === '' ) { continue; }
|
2019-09-17 21:15:01 +02:00
|
|
|
const tr = template.cloneNode(true);
|
2018-07-19 00:00:55 +02:00
|
|
|
tr.setAttribute('data-name', command.name);
|
|
|
|
tr.querySelector('.commandDesc').textContent = command.description;
|
2019-09-17 21:15:01 +02:00
|
|
|
const input = tr.querySelector('.commandShortcut input');
|
2018-07-19 00:00:55 +02:00
|
|
|
input.setAttribute('data-name', command.name);
|
|
|
|
input.value = command.shortcut;
|
|
|
|
input.addEventListener('focus', captureShortcut);
|
|
|
|
tr.querySelector('.commandReset').addEventListener('click', resetShortcut);
|
|
|
|
tbody.appendChild(tr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
vAPI.messaging.send('dashboard', {
|
|
|
|
what: 'getShortcuts',
|
|
|
|
}).then(commands => {
|
|
|
|
onShortcutsReady(commands);
|
|
|
|
});
|
2018-07-19 00:00:55 +02:00
|
|
|
})();
|