From c57c760b1e9f1419137c70be8c1bb9bf9e713aa3 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 20 Jul 2018 13:52:14 -0400 Subject: [PATCH] code review for https://github.com/uBlockOrigin/uBlock-issues/issues/106 --- src/js/commands.js | 11 +++++++++-- src/js/messaging.js | 38 +++++++++++++++++++------------------- src/js/start.js | 9 ++------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/js/commands.js b/src/js/commands.js index d65f0d70c..faf074f79 100644 --- a/src/js/commands.js +++ b/src/js/commands.js @@ -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 @@ -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; diff --git a/src/js/messaging.js b/src/js/messaging.js index bbe7fd89f..bece1ee63 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -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); @@ -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': diff --git a/src/js/start.js b/src/js/start.js index 46acbbb27..e6099f43d 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -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 @@ -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] }); }