diff options
| -rw-r--r-- | background_page.html | 74 | ||||
| -rw-r--r-- | vimiumFrontend.js | 22 |
2 files changed, 83 insertions, 13 deletions
diff --git a/background_page.html b/background_page.html index 9b10172e..e98c7453 100644 --- a/background_page.html +++ b/background_page.html @@ -1,6 +1,10 @@ <html> <head> <script type="text/javascript" charset="utf-8"> + // Currently we need to remember to change this each time we push. Chromium #15242 will enable us + // to retrieve this programmatically. + var currentVersion = "1.15"; + var tabQueue = {}; // windowId -> Array var keyQueue = ""; // Queue of keys typed var validFirstKeys = {}; @@ -49,7 +53,8 @@ var sendRequestHandlers = { getCompletionKeys: getCompletionKeys, - getLinkHintCss: getLinkHintCss + getLinkHintCss: getLinkHintCss, + upgradeNotificationClosed: upgradeNotificationClosed }; // Event handlers @@ -62,11 +67,17 @@ // If this is a tab we've been waiting to open, execute any "tab loaded" handlers, e.g. to restore // the tab's scroll position. Wait until domReady before doing this; otherwise operations like restoring // the scroll position will not be possible. - if (port.name == "domReady" && senderTabId && tabLoadedHandlers[senderTabId]) { - var toCall = tabLoadedHandlers[senderTabId]; - // Delete first to be sure there's no circular events. - delete tabLoadedHandlers[senderTabId]; - toCall.call(); + if (port.name == "domReady" && senderTabId != null) { + if (tabLoadedHandlers[senderTabId]) { + var toCall = tabLoadedHandlers[senderTabId]; + // Delete first to be sure there's no circular events. + delete tabLoadedHandlers[senderTabId]; + toCall.call(); + } + + // domReady is the appropriate time to show the "vimium has been upgraded" message. + if (shouldShowUpgradeMessage()) + chrome.tabs.sendRequest(senderTabId, { name: "showUpgradeNotification", version: currentVersion }); } if (portHandlers[port.name]) @@ -76,7 +87,6 @@ chrome.extension.onRequest.addListener(function (request, sender, sendResponse) { var senderTabId = sender.tab ? sender.tab.id : null; - if (sendRequestHandlers[request.handler]) sendResponse(sendRequestHandlers[request.handler](request)); }); @@ -143,6 +153,15 @@ } /* + * Called when the user has clicked the close icon on the "Vimium has been updated" message. + * We should now dismiss that message in all tabs. + */ + function upgradeNotificationClosed(request) { + localStorage.previousVersion = currentVersion; + sendRequestToAllTabs({ name: "hideUpgradeNotification" }); + } + + /* * Used by the content scripts to get settings from the local storage. */ function getSetting(args, port) { @@ -411,12 +430,51 @@ return newKeyQueue; } + /* + * Message all tabs. Args should be the arguments hash used by the Chrome sendRequest API. + */ + function sendRequestToAllTabs(args) { + chrome.windows.getAll({ populate: true }, function(windows) { + for (var i = 0; i < windows.length; i++) + for (var j = 0; j < windows[i].tabs.length; j++) + chrome.tabs.sendRequest(windows[i].tabs[j].id, args, null); + }); + } + + // Compares two version strings (e.g. "1.1" and "1.5") and returns + // -1 if versionA is < versionB, 0 if they're equal, and 1 if versionA is > versionB. + function compareVersions(versionA, versionB) { + versionA = versionA.split("."); + versionB = versionB.split("."); + for (var i = 0; i < Math.max(versionA.length, versionB.length); i++) { + var a = parseInt(versionA[i] || 0); + var b = parseInt(versionB[i] || 0); + if (a < b) return -1; + else if (a > b) return 1; + } + return 0; + } + + /* + * Returns true if the current extension version is greater than the previously recorded version in + * localStorage, and false otherwise. + */ + function shouldShowUpgradeMessage() { + // Avoid showing the upgrade notification when localStorage.previousVersion is undefined, which is the + // case for new installs. + if (!localStorage.previousVersion) + localStorage.previousVersion = currentVersion; + return compareVersions(currentVersion, localStorage.previousVersion) == 1; + } + function init() { populateValidFirstKeys(); populateSingleKeyCommands(); + if (shouldShowUpgradeMessage()) + sendRequestToAllTabs({ name: "showUpgradeNotification", version: currentVersion }); } init(); </script> </head> -</html> +</html>
\ No newline at end of file diff --git a/vimiumFrontend.js b/vimiumFrontend.js index 6a864cc4..4d380d2d 100644 --- a/vimiumFrontend.js +++ b/vimiumFrontend.js @@ -82,6 +82,14 @@ function initializePreDomReady() { else platform = "Windows"; + chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { + if (request.name == "hideUpgradeNotification") + HUD.hideUpgradeNotification(); + else if (request.name == "showUpgradeNotification" && isEnabledForUrl) + HUD.showUpgradeNotification(request.version); + sendResponse({}); // Free up the resources used by this open connection. + }); + chrome.extension.onConnect.addListener(function(port, name) { if (port.name == "executePageCommand") { port.onMessage.addListener(function(args) { @@ -536,15 +544,19 @@ HUD = { showUpgradeNotification: function(version) { HUD.upgradeNotificationElement().innerHTML = "Vimium has been updated to " + "<a href='https://chrome.google.com/extensions/detail/dbepggeogbaibhgnhhndojpepiihcmeb'>" + - version + ".</a><a class='close-button' href='#'>x</a>"; - var closeLink = HUD.upgradeNotificationElement().getElementsByClassName("close-button")[0]; - closeLink.addEventListener("click", HUD.onCloseNotificationClick, false); + version + "</a>.<a class='close-button' href='#'>x</a>"; + var links = HUD.upgradeNotificationElement().getElementsByTagName("a"); + links[0].addEventListener("click", HUD.onUpdateLinkClicked, false); + links[1].addEventListener("click", function(event) { + event.preventDefault(); + HUD.onUpdateLinkClicked(); + }); Tween.fade(HUD.upgradeNotificationElement(), 1.0, 150); }, - onCloseNotificationClick: function(event) { - event.preventDefault(); + onUpdateLinkClicked: function(event) { HUD.hideUpgradeNotification(); + chrome.extension.sendRequest({ handler: "upgradeNotificationClosed" }); }, hideUpgradeNotification: function(clickEvent) { |
