From 760df5eb4437056283d2fe983e45be2bbf99b1de Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Tue, 10 Sep 2013 16:29:24 +0200 Subject: [PATCH] Fix #190: Improve blacklisting UI - Fix bug: on `about:` pages `**` was suggested as blacklisting rule - On blacklisted pages, the blacklist button un-blacklists instead - Live validation of the black list setting --- extension/locale/de/vimfx.properties | 1 + extension/locale/el-GR/vimfx.properties | 1 + extension/locale/en-US/vimfx.properties | 1 + extension/locale/hu/vimfx.properties | 1 + extension/locale/id/vimfx.properties | 1 + extension/locale/nl/vimfx.properties | 1 + extension/locale/pl/vimfx.properties | 1 + extension/locale/ru/vimfx.properties | 1 + extension/locale/zh-CN/vimfx.properties | 1 + extension/packages/button.coffee | 42 ++++++++++-------- extension/packages/events.coffee | 2 +- extension/packages/options.coffee | 14 +++++- extension/packages/utils.coffee | 41 ++++++++++++++--- .../resources/icon16-blacklist_inverse.png | Bin 0 -> 412 bytes .../{icon16.png => icon16-normal.png} | Bin 15 files changed, 80 insertions(+), 28 deletions(-) create mode 100644 extension/resources/icon16-blacklist_inverse.png rename extension/resources/{icon16.png => icon16-normal.png} (100%) diff --git a/extension/locale/de/vimfx.properties b/extension/locale/de/vimfx.properties index 11b99b6..a63ff77 100644 --- a/extension/locale/de/vimfx.properties +++ b/extension/locale/de/vimfx.properties @@ -3,6 +3,7 @@ button_tooltip_disabled=VimFx ist deaktiviert. Zum Aktivieren hier klicken. (shi button_tooltip_blacklisted=Diese Seite ist auf der schwarzen Liste item_preferences=Einstellungen item_blacklist_button_tooltip=Schwarze Liste +item_blacklist_button_inverse_tooltip=Remove this rule help_section_urls=Umgang mit URLs help_command_focus=Adressleiste fokussieren diff --git a/extension/locale/el-GR/vimfx.properties b/extension/locale/el-GR/vimfx.properties index 77602c8..8d80851 100644 --- a/extension/locale/el-GR/vimfx.properties +++ b/extension/locale/el-GR/vimfx.properties @@ -3,6 +3,7 @@ button_tooltip_disabled=Το VimFx είναι απενεργοποιημένο. button_tooltip_blacklisted=Το VimFx είναι στην μαύρη λίστα σε αυτήν την σελίδα. item_preferences=Προτιμήσεις item_blacklist_button_tooltip=Μαύρη Λίστα +item_blacklist_button_inverse_tooltip=Remove this rule help_section_urls=Όσον αφορά τις διευθύνσεις help_command_focus=Εστιάζοντας την Γραμμή Διευθύνσεων diff --git a/extension/locale/en-US/vimfx.properties b/extension/locale/en-US/vimfx.properties index c6689c7..72be668 100644 --- a/extension/locale/en-US/vimfx.properties +++ b/extension/locale/en-US/vimfx.properties @@ -3,6 +3,7 @@ button_tooltip_disabled=VimFx is Disabled. Click to Enable (shift-alt-v) button_tooltip_blacklisted=VimFx is Blacklisted on this Page item_preferences=Preferences item_blacklist_button_tooltip=Blacklist +item_blacklist_button_inverse_tooltip=Remove this rule help_section_urls=Dealing with URLs help_command_focus=Focus the Address Bar diff --git a/extension/locale/hu/vimfx.properties b/extension/locale/hu/vimfx.properties index a7858a6..3f58fea 100644 --- a/extension/locale/hu/vimfx.properties +++ b/extension/locale/hu/vimfx.properties @@ -3,6 +3,7 @@ button_tooltip_disabled=VimFx inaktív. Kattints az aktiváláshoz (shift-alt-v) button_tooltip_blacklisted=Az oldal feketelistázva a VimFX-ben. item_preferences=Beállítások item_blacklist_button_tooltip=Feketelista +item_blacklist_button_inverse_tooltip=Remove this rule help_section_urls=URL műveletek help_command_focus=Fókusz a címsorra diff --git a/extension/locale/id/vimfx.properties b/extension/locale/id/vimfx.properties index 0ee69e7..a631049 100644 --- a/extension/locale/id/vimfx.properties +++ b/extension/locale/id/vimfx.properties @@ -3,6 +3,7 @@ button_tooltip_disabled=VimFx telah Nonaktif. Klik untuk Aktifkan (shift-alt-v) button_tooltip_blacklisted=VimFx telah Dilarang pada halaman ini item_preferences=Preferensi item_blacklist_button_tooltip=Larangan +item_blacklist_button_inverse_tooltip=Remove this rule help_section_urls=Penanganan URL help_command_focus=Fokus pada Address Bar diff --git a/extension/locale/nl/vimfx.properties b/extension/locale/nl/vimfx.properties index f0eea53..5f21a7d 100644 --- a/extension/locale/nl/vimfx.properties +++ b/extension/locale/nl/vimfx.properties @@ -3,6 +3,7 @@ button_tooltip_disabled=VimFx is uitgeschakeld. Klik om in te schakelen (shift-a button_tooltip_blacklisted=VimFx is uitgeschakeld op deze pagina item_preferences=Instellingen item_blacklist_button_tooltip=Zwarte lijst +item_blacklist_button_inverse_tooltip=Remove this rule help_section_urls=Omgaan met URLs help_command_focus=Focus de adresbalk diff --git a/extension/locale/pl/vimfx.properties b/extension/locale/pl/vimfx.properties index 5d0ee00..329c2c9 100644 --- a/extension/locale/pl/vimfx.properties +++ b/extension/locale/pl/vimfx.properties @@ -3,6 +3,7 @@ button_tooltip_disabled=VimFx jest wyłączony. Kliknij, aby włączyć (shift-a button_tooltip_blacklisted=Ta strona jest na czarnej liście VimFx item_preferences=Preferencje item_blacklist_button_tooltip=Czarna lista +item_blacklist_button_inverse_tooltip=Remove this rule help_section_urls=Praca z adresami help_command_focus=Przejdź do paska adresu diff --git a/extension/locale/ru/vimfx.properties b/extension/locale/ru/vimfx.properties index 336386e..ec3cee4 100644 --- a/extension/locale/ru/vimfx.properties +++ b/extension/locale/ru/vimfx.properties @@ -3,6 +3,7 @@ button_tooltip_disabled=VimFx не работает. Нажмите чтобы button_tooltip_blacklisted=VimFx содержит текущую страницу в стоп-списке item_preferences=Настройки item_blacklist_button_tooltip=Добавить в стоп-список +item_blacklist_button_inverse_tooltip=Remove this rule help_section_urls=Адресная Строка help_command_focus=Фокус на адресную строку diff --git a/extension/locale/zh-CN/vimfx.properties b/extension/locale/zh-CN/vimfx.properties index ba03f70..8a19ff6 100644 --- a/extension/locale/zh-CN/vimfx.properties +++ b/extension/locale/zh-CN/vimfx.properties @@ -3,6 +3,7 @@ button_tooltip_disabled=VimFx 已禁用。点击启用 (shift-alt-v) button_tooltip_blacklisted=当前页面已被列入黑名单 item_preferences=首选项 item_blacklist_button_tooltip=添加到黑名单 +item_blacklist_button_inverse_tooltip=Remove this rule help_section_urls=处理 URL help_command_focus=聚焦地址栏 diff --git a/extension/packages/button.coffee b/extension/packages/button.coffee index 850d99b..e2be4e3 100644 --- a/extension/packages/button.coffee +++ b/extension/packages/button.coffee @@ -61,26 +61,19 @@ restorePosition = (doc, button) -> toolbar.insertItem(button.id) -iconUrl = do -> - kinds = - normal: utils.getResourceURI('resources/icon16.png').spec - grey: utils.getResourceURI('resources/icon16-grey.png').spec - red: utils.getResourceURI('resources/icon16-red.png').spec - blacklist: utils.getResourceURI('resources/icon16-blacklist.png').spec +iconUrl = (kind) -> + url = utils.getResourceURI("resources/icon16-#{kind}.png").spec + return "url(#{ url })" - return (kind) -> "url(#{ kinds[kind] })" - -createMenupopup = (window) -> +createMenupopup = (window, button) -> doc = window.document blacklistTextbox = doc.createElement('textbox') blacklistTextbox.id = TEXTBOX_BLACKLIST_ID blacklistButton = doc.createElement('toolbarbutton') blacklistButton.id = BUTTON_BLACKLIST_ID - blacklistButton.setAttribute('tooltiptext', _('item_blacklist_button_tooltip')) blacklistButton.setAttribute('class', 'toolbarbutton-1') - blacklistButton.style.listStyleImage = iconUrl('blacklist') hbox = doc.createElement('hbox') hbox.appendChild(blacklistTextbox) hbox.appendChild(blacklistButton) @@ -101,15 +94,26 @@ createMenupopup = (window) -> menupopup.appendChild(itemHelp) onPopupShowing = (event) -> - if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow - blacklistTextbox.value = "*#{ tabWindow.location.host }*" + return unless tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow + + { blacklisted, matchedRule } = button['VimFx_blacklisted'] or {} + if blacklisted + blacklistTextbox.value = matchedRule + blacklistButton.setAttribute('tooltiptext', _('item_blacklist_button_inverse_tooltip')) + blacklistButton.style.listStyleImage = iconUrl('blacklist_inverse') + else + # In `about:` pages, the `host` property is an empty string. Fall back to the whole URL + blacklistTextbox.value = "*#{ tabWindow.location.host or tabWindow.location }*" + blacklistButton.setAttribute('tooltiptext', _('item_blacklist_button_tooltip')) + blacklistButton.style.listStyleImage = iconUrl('blacklist') onBlacklistButtonCommand = (event) -> - blackList = getPref('black_list') - blackList += ', ' if blackList.length > 0 - blackList += blacklistTextbox.value + { blacklisted, matchedRule } = button['VimFx_blacklisted'] or {} + if blacklisted + utils.updateBlacklist({remove: matchedRule}) + else + utils.updateBlacklist({add: blacklistTextbox.value}) - setPref('black_list', blackList) menupopup.hidePopup() if tabWindow = window.gBrowser.selectedTab.linkedBrowser.contentWindow @@ -134,6 +138,7 @@ createMenupopup = (window) -> itemPreferences.addEventListener 'command', onPreferencesCommand, false itemHelp.addEventListener 'command', onHelpCommand, false + button.appendChild(menupopup) return menupopup createButton = (window) -> @@ -155,8 +160,7 @@ createButton = (window) -> button.addEventListener('command', onButtonCommand, false) - menupopup = createMenupopup(window) - button.appendChild(menupopup) + createMenupopup(window, button) vimkey = doc.createElement('key') vimkey.setAttribute('id', KEY_ID) diff --git a/extension/packages/events.coffee b/extension/packages/events.coffee index 580044c..b51c9bf 100644 --- a/extension/packages/events.coffee +++ b/extension/packages/events.coffee @@ -127,7 +127,7 @@ windowsListener = # on black listed urls tabsListener = onLocationChange: (browser, webProgress, request, location) -> - blacklisted = utils.isBlacklisted(location.spec, getPref('black_list')) + blacklisted = utils.isBlacklisted(location.spec) if vim = vimBucket.get(browser.contentWindow) vim.enterNormalMode() vim.blacklisted = blacklisted diff --git a/extension/packages/options.coffee b/extension/packages/options.coffee index 8a68982..0106d9a 100644 --- a/extension/packages/options.coffee +++ b/extension/packages/options.coffee @@ -1,14 +1,17 @@ -{ removeDuplicateCharacters } = require 'utils' +utils = require 'utils' { unload } = require 'unload' { getPref } = require 'prefs' help = require 'help' { commands } = require 'commands' +{ unload } = require 'unload' +{ getPref } = require 'prefs' observer = observe: (document, topic, addon) -> return unless addon == getPref('addon_id') hintCharsInput = document.querySelector('setting[pref="extensions.VimFx.hint_chars"]') + blacklistInput = document.querySelector('setting[pref="extensions.VimFx.black_list"]') customizeButton = document.getElementById('customizeButton') injectHelp = help.injectHelp.bind(undefined, document, commands) @@ -16,16 +19,23 @@ observer = switch topic when 'addon-options-displayed' hintCharsInput.addEventListener('change', filterChars, false) + blacklistInput.addEventListener('change', validateBlacklist, false) customizeButton.addEventListener('command', injectHelp, false) + when 'addon-options-hidden' hintCharsInput.removeEventListener('change', filterChars, false) + blacklistInput.removeEventListener('change', validateBlacklist, false) customizeButton.removeEventListener('command', injectHelp, false) filterChars = (event) -> input = event.target - input.value = removeDuplicateCharacters(input.value).replace(/\s/g, '') + input.value = utils.removeDuplicateCharacters(input.value).replace(/\s/g, '') input.valueToPreference() +validateBlacklist = (event) -> + input = event.target + utils.updateBlacklist() + observe = -> Services.obs.addObserver(observer, 'addon-options-displayed', false) Services.obs.addObserver(observer, 'addon-options-hidden', false) diff --git a/extension/packages/utils.coffee b/extension/packages/utils.coffee index 43806e7..17c61ed 100644 --- a/extension/packages/utils.coffee +++ b/extension/packages/utils.coffee @@ -1,5 +1,6 @@ { unload } = require 'unload' { getPref +, setPref , getDefaultPref } = require 'prefs' @@ -194,15 +195,38 @@ timeIt = (func, msg) -> return result # Checks if the string provided matches one of the black list entries -# `blackList`: comma/space separated list of URLs with wildcards (* and !) -isBlacklisted = (str, blackList) -> - for rule in blackList.split(/[\s,]+/) - rule = rule.replace(/\*/g, '.*').replace(/\!/g, '.') - if str.match ///^#{ rule }$/// - return true +isBlacklisted = (str) -> + for rule in getBlacklist() + # Wildcards: * and ! + regexifiedRule = rule.replace(/\*/g, '.*').replace(/!/g, '.') + if str.match(///^#{ regexifiedRule }$///) + return {blacklisted: true, matchedRule: rule} return false +getBlacklist = -> + # Comma/space separated list + return getPref('black_list').split(/[\s,]+/) + +setBlacklist = (blacklist) -> + setPref('black_list', blacklist.join(', ')) + +updateBlacklist = ({ add, remove} = {}) -> + blacklist = getBlacklist() + + if add + blacklist.push(add) + + # Remove duplicates and empty rules + seen = {} + blacklist = blacklist + .filter((rule) -> if seen[rule] or rule == '' then false else (seen[rule] = true)) + + if remove and remove in blacklist + blacklist.splice(blacklist.indexOf(remove), 1) + + setBlacklist(blacklist) + # Gets VimFx verions. AddonManager only provides async API to access addon data, so it's a bit tricky... getVersion = do -> version = null @@ -288,7 +312,12 @@ exports.WHEEL_MODE_PAGE = WHEEL_MODE_PAGE exports.readFromClipboard = readFromClipboard exports.writeToClipboard = writeToClipboard exports.timeIt = timeIt + exports.isBlacklisted = isBlacklisted +exports.getBlacklist = getBlacklist +exports.setBlacklist = setBlacklist +exports.updateBlacklist = updateBlacklist + exports.getVersion = getVersion exports.parseHTML = parseHTML exports.isURL = isURL diff --git a/extension/resources/icon16-blacklist_inverse.png b/extension/resources/icon16-blacklist_inverse.png new file mode 100644 index 0000000000000000000000000000000000000000..1f80ab74a9424486ed2482da4e4e892251e2f7d7 GIT binary patch literal 412 zcmV;N0b~A&P)r!9nGh+Wq`Boj{zx&m2M zRma(!4pCKAWSZuI9mhF3S(ZiD6-7a!C|=(JN#6Gz8HRCSUDxRxVE)>F5daAp#Ir>_ zzEuDTJZ4EQ31E@j1gnTw@bM*re295PAGZJntOA((7XjOm_kV7ou4_snNfPS&p2nJ{ zp|0y_@h}W0k_TJ-^FZEh+diA7IejSh2@{iL`3*JS0t^6P_kR-oQp#um0000