diff options
| author | Phil Crosby | 2010-01-30 00:48:49 -0800 |
|---|---|---|
| committer | Phil Crosby | 2010-01-30 00:55:55 -0800 |
| commit | 781da8b0cbf735fa0f6183d8db58cf59d00e7cb1 (patch) | |
| tree | d0a78e81e8b4c3c3781ce82b058a1dae4e0656f4 /background_page.html | |
| parent | 8028cfbf170454f52d8174c4a7152d06f8e3d92d (diff) | |
| download | vimium-781da8b0cbf735fa0f6183d8db58cf59d00e7cb1.tar.bz2 | |
Save the current version to localStorage, and when an update arrives, show a user notification.
This closes #64. Note that updates won't trigger until we've saved the user's
version to their localStorage, which means this change will go into effect for
our users the version *after* the next release (so 1.16).
Diffstat (limited to 'background_page.html')
| -rw-r--r-- | background_page.html | 74 |
1 files changed, 66 insertions, 8 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 |
