Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Jul 20, 2018
1 parent 4738721 commit c57c760
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
11 changes: 9 additions & 2 deletions src/js/commands.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2017 Raymond Hill
Copyright (C) 2017-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
Expand All @@ -25,8 +25,15 @@

/******************************************************************************/

µBlock.canUseShortcuts = vAPI.commands instanceof Object;

µBlock.canUpdateShortcuts = µBlock.canUseShortcuts &&
typeof vAPI.commands.update === 'function';

/******************************************************************************/

(function() {
if ( vAPI.commands === undefined ) { return; }
if ( µBlock.canUseShortcuts === false ) { return; }

vAPI.commands.onCommand.addListener(function(command) {
var µb = µBlock;
Expand Down
38 changes: 19 additions & 19 deletions src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,28 +917,28 @@ var modifyRuleset = function(details) {
// Shortcuts pane

let getShortcuts = function(callback) {
if ( vAPI.commands !== undefined ) {
vAPI.commands.getAll(commands => {
let response = [];
for ( let command of commands ) {
let desc = command.description;
let match = /^__MSG_(.+?)__$/.exec(desc);
if ( match !== null ) {
desc = vAPI.i18n(match[1]);
}
if ( desc === '' ) { continue; }
command.description = desc;
response.push(command);
}
callback(response);
});
} else {
callback([]);
if ( µb.canUseShortcuts === false ) {
return callback([]);
}

vAPI.commands.getAll(commands => {
let response = [];
for ( let command of commands ) {
let desc = command.description;
let match = /^__MSG_(.+?)__$/.exec(desc);
if ( match !== null ) {
desc = vAPI.i18n(match[1]);
}
if ( desc === '' ) { continue; }
command.description = desc;
response.push(command);
}
callback(response);
});
};

let setShortcut = function(details) {
if ( typeof vAPI.commands.update !== 'function' ) { return; }
if ( µb.canUpdateShortcuts === false ) { return; }
if ( details.shortcut === undefined ) {
vAPI.commands.reset(details.name);
µb.commandShortcuts.delete(details.name);
Expand Down Expand Up @@ -981,7 +981,7 @@ var onMessage = function(request, sender, callback) {

switch ( request.what ) {
case 'canUpdateShortcuts':
response = typeof vAPI.commands.update === 'function';
response = µb.canUpdateShortcuts;
break;

case 'getRules':
Expand Down
9 changes: 2 additions & 7 deletions src/js/start.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2014-2018 Raymond Hill
Copyright (C) 2014-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
Expand Down Expand Up @@ -106,12 +106,7 @@ var onPSLReady = function() {
var onCommandShortcutsReady = function(commandShortcuts) {
if ( Array.isArray(commandShortcuts) === false ) { return; }
µb.commandShortcuts = new Map(commandShortcuts);
if (
vAPI.commands === undefined ||
typeof vAPI.commands.update !== 'function'
) {
return;
}
if ( µb.canUpdateShortcuts === false ) { return; }
for ( let entry of commandShortcuts ) {
vAPI.commands.update({ name: entry[0], shortcut: entry[1] });
}
Expand Down

0 comments on commit c57c760

Please sign in to comment.