From 528354f5945d12fdff5dc7c4a699c4d6aec76125 Mon Sep 17 00:00:00 2001 From: gorhill Date: Sat, 1 Aug 2015 11:30:54 -0400 Subject: [PATCH] this fixes #507 --- platform/chromium/vapi-background.js | 31 ++++++++-- platform/firefox/vapi-background.js | 10 +++ src/js/logger-ui.js | 93 ++++++++++++++++++---------- src/js/popup.js | 54 +++++++++------- 4 files changed, 127 insertions(+), 61 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index 9f0b03d6a..8bedd1519 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -294,6 +294,7 @@ vAPI.tabs.open = function(details) { if ( typeof targetURL !== 'string' || targetURL === '' ) { return null; } + // extension pages if ( /^[\w-]{2,}:/.test(targetURL) !== true ) { targetURL = vAPI.getURL(targetURL); @@ -370,15 +371,33 @@ vAPI.tabs.open = function(details) { return; } - chrome.tabs.query({ url: targetURL }, function(tabs) { + // https://developer.chrome.com/extensions/tabs#method-query + // "Note that fragment identifiers are not matched." + // It's a lie, fragment identifiers ARE matched. So we need to remove the + // fragment. + var targetURLWithoutHash = targetURL; + var pos = targetURL.indexOf('#'); + if ( pos !== -1 ) { + targetURLWithoutHash = targetURL.slice(0, pos); + } + + chrome.tabs.query({ url: targetURLWithoutHash }, function(tabs) { var tab = tabs[0]; - if ( tab ) { - chrome.tabs.update(tab.id, { active: true }, function(tab) { - chrome.windows.update(tab.windowId, { focused: true }); - }); - } else { + if ( !tab ) { wrapper(); + return; } + + var _details = { + active: true, + url: undefined + }; + if ( targetURL !== tab.url ) { + _details.url = targetURL; + } + chrome.tabs.update(tab.id, _details, function(tab) { + chrome.windows.update(tab.windowId, { focused: true }); + }); }); }; diff --git a/platform/firefox/vapi-background.js b/platform/firefox/vapi-background.js index 8ea2036b8..16d796b8a 100644 --- a/platform/firefox/vapi-background.js +++ b/platform/firefox/vapi-background.js @@ -669,7 +669,17 @@ vAPI.tabs.open = function(details) { continue; } + // Or simply .equals if we care about the fragment + if ( URI.equalsExceptRef(browser.currentURI) === false ) { + continue; + } + this.select(tab); + + // Update URL if fragment is different + if ( URI.equals(browser.currentURI) === false ) { + browser.loadURI(URI.asciiSpec); + } return; } } diff --git a/src/js/logger-ui.js b/src/js/logger-ui.js index 83d47c2ad..3c2c8faa4 100644 --- a/src/js/logger-ui.js +++ b/src/js/logger-ui.js @@ -688,6 +688,10 @@ var onLogBufferRead = function(response) { allTabIdsToken = response.tabIdsToken; } + // https://github.com/gorhill/uBlock/issues/507 + // Ensure tab selector is in sync with URL hash + pageSelectorFromURLHash(); + renderLogEntries(response); if ( rowVoided ) { @@ -719,26 +723,55 @@ var readLogBuffer = function() { /******************************************************************************/ var pageSelectorChanged = function() { - var style = uDom.nodeFromId('tabFilterer'); - var tabClass = uDom.nodeFromId('pageSelector').value; - var sheet = style.sheet; - while ( sheet.cssRules.length !== 0 ) { - sheet.deleteRule(0); - } - if ( tabClass !== '' ) { - sheet.insertRule( - '#netInspector tr:not(.' + tabClass + ') { display: none; }', - 0 - ); - } - uDom('.needtab').toggleClass( - 'disabled', - tabClass === '' || tabClass === 'tab_bts' - ); + window.location.replace('#' + uDom.nodeFromId('pageSelector').value); + pageSelectorFromURLHash(); }; /******************************************************************************/ +var pageSelectorFromURLHash = (function() { + var lastHash = ''; + + return function() { + var hash = window.location.hash; + if ( hash === lastHash ) { + return; + } + + var tabClass = hash.slice(1); + var select = uDom.nodeFromId('pageSelector'); + var option = select.querySelector('option[value="' + tabClass + '"]'); + if ( option === null ) { + hash = window.location.hash = ''; + tabClass = ''; + option = select.options[0]; + } + + lastHash = hash; + + select.selectedIndex = option.index; + select.value = option.value; + + var style = uDom.nodeFromId('tabFilterer'); + var sheet = style.sheet; + while ( sheet.cssRules.length !== 0 ) { + sheet.deleteRule(0); + } + if ( tabClass !== '' ) { + sheet.insertRule( + '#netInspector tr:not(.' + tabClass + ') { display: none; }', + 0 + ); + } + uDom('.needtab').toggleClass( + 'disabled', + tabClass === '' || tabClass === 'tab_bts' + ); + }; +})(); + +/******************************************************************************/ + var reloadTab = function() { var tabClass = uDom.nodeFromId('pageSelector').value; var tabId = tabIdFromClassName(tabClass); @@ -1720,25 +1753,21 @@ var popupManager = (function() { /******************************************************************************/ -uDom.onLoad(function() { - readLogBuffer(); +readLogBuffer(); - uDom('#pageSelector').on('change', pageSelectorChanged); - uDom('#refresh').on('click', reloadTab); - uDom('#showdom').on('click', toggleInspectors); +uDom('#pageSelector').on('change', pageSelectorChanged); +uDom('#refresh').on('click', reloadTab); +uDom('#showdom').on('click', toggleInspectors); - uDom('#compactViewToggler').on('click', toggleCompactView); - uDom('#clean').on('click', cleanBuffer); - uDom('#clear').on('click', clearBuffer); - uDom('#maxEntries').on('change', onMaxEntriesChanged); - uDom('#netInspector table').on('click', 'tr.canMtx > td:nth-of-type(2)', popupManager.toggleOn); - uDom('#netInspector').on('click', 'tr.canLookup > td:nth-of-type(3)', reverseLookupManager.toggleOn); - uDom('#netInspector').on('click', 'tr.cat_net > td:nth-of-type(4)', netFilteringManager.toggleOn); +uDom('#compactViewToggler').on('click', toggleCompactView); +uDom('#clean').on('click', cleanBuffer); +uDom('#clear').on('click', clearBuffer); +uDom('#maxEntries').on('change', onMaxEntriesChanged); +uDom('#netInspector table').on('click', 'tr.canMtx > td:nth-of-type(2)', popupManager.toggleOn); +uDom('#netInspector').on('click', 'tr.canLookup > td:nth-of-type(3)', reverseLookupManager.toggleOn); +uDom('#netInspector').on('click', 'tr.cat_net > td:nth-of-type(4)', netFilteringManager.toggleOn); - // https://github.com/gorhill/uBlock/issues/404 - // Ensure page state is in sync with the state of its various widgets. - pageSelectorChanged(); -}); +window.addEventListener('hashchange', pageSelectorFromURLHash); /******************************************************************************/ diff --git a/src/js/popup.js b/src/js/popup.js index f8a4af7e9..fa68cda32 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -433,6 +433,15 @@ var renderPopup = function() { } uDom.nodeFromId('total-blocked').textContent = text; + // https://github.com/gorhill/uBlock/issues/507 + // Convenience: open the logger with current tab automatically selected + if ( popupData.tabId ) { + uDom.nodeFromSelector('.statName > a[href^="logger-ui.html"]').setAttribute( + 'href', + 'logger-ui.html#tab_' + popupData.tabId + ); + } + // This will collate all domains, touched or not renderPrivacyExposure(); @@ -825,33 +834,32 @@ var onHideTooltip = function() { /******************************************************************************/ -// Make menu only when popup html is fully loaded +// Popup DOM is assumed to be loaded at this point -- because this script +// is loaded after everything else.. -uDom.onLoad(function () { - // If there's no tab id specified in the query string, - // it will default to current tab. - var tabId = null; +// If there's no tab id specified in the query string, +// it will default to current tab. +var tabId = null; - // Extract the tab id of the page this popup is for - var matches = window.location.search.match(/[\?&]tabId=([^&]+)/); - if ( matches && matches.length === 2 ) { - tabId = matches[1]; - } - getPopupData(tabId); +// Extract the tab id of the page this popup is for +var matches = window.location.search.match(/[\?&]tabId=([^&]+)/); +if ( matches && matches.length === 2 ) { + tabId = matches[1]; +} +getPopupData(tabId); - uDom('#switch').on('click', toggleNetFilteringSwitch); - uDom('#gotoPick').on('click', gotoPick); - uDom('a[href]').on('click', gotoURL); - uDom('h2').on('click', toggleFirewallPane); - uDom('#refresh').on('click', reloadTab); - uDom('.hnSwitch').on('click', toggleHostnameSwitch); - uDom('#saveRules').on('click', saveFirewallRules); - uDom('#revertRules').on('click', revertFirewallRules); - uDom('[data-i18n="popupAnyRulePrompt"]').on('click', toggleMinimize); +uDom('#switch').on('click', toggleNetFilteringSwitch); +uDom('#gotoPick').on('click', gotoPick); +uDom('a[href]').on('click', gotoURL); +uDom('h2').on('click', toggleFirewallPane); +uDom('#refresh').on('click', reloadTab); +uDom('.hnSwitch').on('click', toggleHostnameSwitch); +uDom('#saveRules').on('click', saveFirewallRules); +uDom('#revertRules').on('click', revertFirewallRules); +uDom('[data-i18n="popupAnyRulePrompt"]').on('click', toggleMinimize); - uDom('body').on('mouseenter', '[data-tip]', onShowTooltip) - .on('mouseleave', '[data-tip]', onHideTooltip); -}); +uDom('body').on('mouseenter', '[data-tip]', onShowTooltip) + .on('mouseleave', '[data-tip]', onHideTooltip); /******************************************************************************/