Move click and webrequest handlers outside of onInstalled Since onInstalled is called when the extension is initially installed, updated, or when the browser updates, I believe this was causing the bug where clicking the page action icon was not working. The handlers should be installed whenever background.js is loaded. Change-Id: I88687185c32751d3afe6d0dab783cf4a39154ab2
diff --git a/background.js b/background.js index f547edd..73ec736 100644 --- a/background.js +++ b/background.js
@@ -86,56 +86,56 @@ ]); }); }); +}); - // There is no option to update a tab URL while also bypassing the cache. Hack - // around this limitation by setting a tab ID to be reloaded in the tab update - // callback below. - var tabIDToReload = null; - chrome.webRequest.onCompleted.addListener( - function(details) { - if (details.type == 'main_frame' && tabIDToReload != null) { - chrome.tabs.reload(tabIDToReload, {bypassCache: true}); - tabIDToReload = null; - } - }, - { - urls: [ - '*://*.googlesource.com/*', - "*://*.git.corp.google.com/*", - ] +// There is no option to update a tab URL while also bypassing the cache. Hack +// around this limitation by setting a tab ID to be reloaded in the tab update +// callback below. +var tabIDToReload = null; +chrome.webRequest.onCompleted.addListener( + function(details) { + if (details.type == 'main_frame' && tabIDToReload != null) { + chrome.tabs.reload(tabIDToReload, {bypassCache: true}); + tabIDToReload = null; } - ); + }, + { + urls: [ + '*://*.googlesource.com/*', + "*://*.git.corp.google.com/*", + ] + } +); - chrome.pageAction.onClicked.addListener(function(tab) { - var cookieID = { - name: 'GERRIT_UI', - url: tab.url, - }; - chrome.cookies.get(cookieID, function(cookie) { - if (cookie) { - _gaq.push(['_trackEvent', 'Page Action', 'Switch to Gerrit']); - chrome.cookies.remove(cookieID, function() { - // The GWT UI does not handle PolyGerrit URL redirection. - chrome.tabs.update(tab.id, { - url: tab.url.replace('.com/', '.com/#/') - }, function(tab) { - tabIDToReload = tab.id; - }); +chrome.pageAction.onClicked.addListener(function(tab) { + var cookieID = { + name: 'GERRIT_UI', + url: tab.url, + }; + chrome.cookies.get(cookieID, function(cookie) { + if (cookie) { + _gaq.push(['_trackEvent', 'Page Action', 'Switch to Gerrit']); + chrome.cookies.remove(cookieID, function() { + // The GWT UI does not handle PolyGerrit URL redirection. + chrome.tabs.update(tab.id, { + url: tab.url.replace('.com/', '.com/#/') + }, function(tab) { + tabIDToReload = tab.id; }); - } else { - _gaq.push(['_trackEvent', 'Page Action', 'Switch to PolyGerrit']); - chrome.cookies.set({ - url: tab.url, - name: 'GERRIT_UI', - value: 'polygerrit', - httpOnly: true, - path: '/', - secure: true, - }, function() { - // PolyGerrit handles Gerrit URL -> PolyGerrit URL redirection. - chrome.tabs.reload(tab.id, {bypassCache: true}); - }); - } - }); + }); + } else { + _gaq.push(['_trackEvent', 'Page Action', 'Switch to PolyGerrit']); + chrome.cookies.set({ + url: tab.url, + name: 'GERRIT_UI', + value: 'polygerrit', + httpOnly: true, + path: '/', + secure: true, + }, function() { + // PolyGerrit handles Gerrit URL -> PolyGerrit URL redirection. + chrome.tabs.reload(tab.id, {bypassCache: true}); + }); + } }); });
diff --git a/manifest.json b/manifest.json index ee12272..7e0a2c7 100644 --- a/manifest.json +++ b/manifest.json
@@ -2,7 +2,7 @@ "manifest_version": 2, "name": "Gerrit UI Switcher", "description": "Easily switch between the current Gerrit UI and the new PolyGerrit UI", - "version": "0.4.1", + "version": "0.4.2", "background": { "scripts": [ "background.js"