From 4a898dfe139865b6150771ff54c8f05cac47a828 Mon Sep 17 00:00:00 2001 From: Kevin Cox Date: Wed, 9 May 2018 10:03:27 +0100 Subject: [PATCH] Fix findBar and DeveloperToolbar. (#920) getFindBar() was changed to return a promise: https://bugzil.la/1358815 window.DeveloperToolbar was removed: https://bugzil.la/1382661 --- extension/lib/commands.coffee | 108 +++++++++++++++++----------------- extension/lib/modes.coffee | 13 ++-- extension/lib/utils.coffee | 11 ++++ 3 files changed, 74 insertions(+), 58 deletions(-) diff --git a/extension/lib/commands.coffee b/extension/lib/commands.coffee index 7ad5e3b..917ef86 100644 --- a/extension/lib/commands.coffee +++ b/extension/lib/commands.coffee @@ -16,6 +16,7 @@ translate = require('./translate') utils = require('./utils') viewportUtils = require('./viewport') +gDevToolsBrowser = Cu.import('resource://devtools/client/framework/gDevTools.jsm').browser {ContentClick} = Cu.import('resource:///modules/ContentClick.jsm', {}) {FORWARD, BACKWARD} = SelectionManager @@ -885,16 +886,16 @@ helper_find = ({highlight, linksOnly = false}, {vim}) -> helper_find_from_top_of_viewport(vim, FORWARD, -> return unless vim.mode == 'find' - findBar = vim.window.gBrowser.getFindBar() - - mode = if linksOnly then findBar.FIND_LINKS else findBar.FIND_NORMAL - findBar.startFind(mode) - utils.focusElement(findBar._findField, {select: true}) - - return if linksOnly - return unless highlightButton = findBar.getElement('highlight') - if highlightButton.checked != highlight - highlightButton.click() + utils.getFindBar(vim.window.gBrowser).then((findBar) -> + mode = if linksOnly then findBar.FIND_LINKS else findBar.FIND_NORMAL + findBar.startFind(mode) + utils.focusElement(findBar._findField, {select: true}) + + return if linksOnly + return unless highlightButton = findBar.getElement('highlight') + if highlightButton.checked != highlight + highlightButton.click() + ) ) commands.find = helper_find.bind(null, {highlight: false}) @@ -906,46 +907,47 @@ commands.find_links_only = helper_find.bind(null, {linksOnly: true}) helper_find_again = (direction, {vim}) -> return if findStorage.busy - findBar = vim.window.gBrowser.getFindBar() - if findStorage.lastSearchString.length == 0 - vim.notify(translate('notification.find_again.none')) - return - - findStorage.busy = true - - helper_mark_last_scroll_position(vim) - helper_find_from_top_of_viewport(vim, direction, -> - findBar._findField.value = findStorage.lastSearchString - - # `.onFindResult` is temporarily hacked to be able to know when the - # asynchronous `.onFindAgainCommand` is done. When PDFs are shown using - # PDF.js, `.updateControlState` is called instead of `.onFindResult`, so - # hack that one too. - originalOnFindResult = findBar.onFindResult - originalUpdateControlState = findBar.updateControlState - - findBar.onFindResult = (data) -> - # Prevent the find bar from re-opening if there are no matches. - data.storeResult = false - findBar.onFindResult = originalOnFindResult - findBar.updateControlState = originalUpdateControlState - findBar.onFindResult(data) - callback() - - findBar.updateControlState = (args...) -> - # Firefox inconsistently _doesn’t_ re-open the find bar if there are no - # matches here, so no need to take care of that in this case. - findBar.onFindResult = originalOnFindResult - findBar.updateControlState = originalUpdateControlState - findBar.updateControlState(args...) - callback() - - callback = -> - message = findBar._findStatusDesc.textContent - vim.notify(message) if message - findStorage.busy = false + utils.getFindBar(vim.window.gBrowser).then((findBar) -> + if findStorage.lastSearchString.length == 0 + vim.notify(translate('notification.find_again.none')) + return - findBar.onFindAgainCommand(not direction) + findStorage.busy = true + + helper_mark_last_scroll_position(vim) + helper_find_from_top_of_viewport(vim, direction, -> + findBar._findField.value = findStorage.lastSearchString + + # `.onFindResult` is temporarily hacked to be able to know when the + # asynchronous `.onFindAgainCommand` is done. When PDFs are shown using + # PDF.js, `.updateControlState` is called instead of `.onFindResult`, so + # hack that one too. + originalOnFindResult = findBar.onFindResult + originalUpdateControlState = findBar.updateControlState + + findBar.onFindResult = (data) -> + # Prevent the find bar from re-opening if there are no matches. + data.storeResult = false + findBar.onFindResult = originalOnFindResult + findBar.updateControlState = originalUpdateControlState + findBar.onFindResult(data) + callback() + + findBar.updateControlState = (args...) -> + # Firefox inconsistently _doesn’t_ re-open the find bar if there are no + # matches here, so no need to take care of that in this case. + findBar.onFindResult = originalOnFindResult + findBar.updateControlState = originalUpdateControlState + findBar.updateControlState(args...) + callback() + + callback = -> + message = findBar._findStatusDesc.textContent + vim.notify(message) if message + findStorage.busy = false + + findBar.onFindAgainCommand(not direction) + ) ) commands.find_next = helper_find_again.bind(null, FORWARD) @@ -1038,7 +1040,7 @@ commands.esc = ({vim}) -> vim.window.gURLBar.closePopup() utils.blurActiveBrowserElement(vim) - vim.window.gBrowser.getFindBar().close() + utils.getFindBar(vim.window.gBrowser).then((findBar) => findBar.close()) # Better safe than sorry. MarkerContainer.remove(vim.window) @@ -1046,9 +1048,9 @@ commands.esc = ({vim}) -> # Calling `.hide()` when the toolbar is not open can destroy it for the rest # of the Firefox session. The code here is taken from the `.toggle()` method. - {DeveloperToolbar} = vim.window - if DeveloperToolbar.visible - DeveloperToolbar.hide().catch(console.error) + developerToolbar = gDevToolsBrowser.getDeveloperToolbar(vim.window) + if developerToolbar.visible + developerToolbar.hide().catch(console.error) unless help.getSearchInput(vim.window)?.getAttribute('focused') help.removeHelp(vim.window) diff --git a/extension/lib/modes.coffee b/extension/lib/modes.coffee index c8fb2ed..1bb07f2 100644 --- a/extension/lib/modes.coffee +++ b/extension/lib/modes.coffee @@ -338,16 +338,19 @@ mode('find', { onEnter: -> onLeave: ({vim}) -> - findBar = vim.window.gBrowser.getFindBar() - findStorage.lastSearchString = findBar._findField.value - findStorage.busy = false + utils.getFindBar(vim.window.gBrowser).then((findBar) -> + findStorage.lastSearchString = findBar._findField.value + findStorage.busy = false + ) onInput: (args, match) -> {vim} = args switch when match.type == 'full' - args.findBar = args.vim.window.gBrowser.getFindBar() - match.command.run(args) + utils.getFindBar(vim.window.gBrowser, (findBar) -> + args.findBar = findBar + match.command.run(args) + ) return true when match.type == 'partial' return true diff --git a/extension/lib/utils.coffee b/extension/lib/utils.coffee index 949c9b9..49f749e 100644 --- a/extension/lib/utils.coffee +++ b/extension/lib/utils.coffee @@ -663,6 +663,16 @@ getCurrentLocation = -> # This function might return `null` on startup. getCurrentWindow = -> nsIWindowMediator.getMostRecentWindow('navigator:browser') +# gBrowser getFindBar() used to return the findBar directly, but in recent +# versions it returns a promise. This function should be removed once these old +# versions are no longer supported. +getFindBar = (gBrowser) -> + promiseOrFindBar = gBrowser.getFindBar() + if promiseOrFindBar instanceof Promise + promiseOrFindBar + else + Promise.resolve(promiseOrFindBar) + hasEventListeners = (element, type) -> for listener in nsIEventListenerService.getListenerInfoFor(element) if listener.listenerObject and listener.type == type @@ -770,6 +780,7 @@ module.exports = { expandPath getCurrentLocation getCurrentWindow + getFindBar hasEventListeners loadCss observe -- 2.39.3