diff options
| author | Niklas Baumstark | 2012-01-27 19:21:54 +0100 |
|---|---|---|
| committer | Niklas Baumstark | 2012-04-10 23:59:54 +0200 |
| commit | 3449af0461782c24c8577fe4a5938f35f417cbb1 (patch) | |
| tree | fbf4f352f1679599c4fa8f7d84cafd50e05870e6 /background_page.html | |
| parent | 0ab64a092def03ccd462802f040d83ce2911ea1e (diff) | |
| download | vimium-3449af0461782c24c8577fe4a5938f35f417cbb1.tar.bz2 | |
move completion logic to background page
This has the following advantages:
* searching is done in the background, UI responsiveness is improved
* caches are no longer duplicated. This saves RAM and improves performance
Diffstat (limited to 'background_page.html')
| -rw-r--r-- | background_page.html | 94 |
1 files changed, 31 insertions, 63 deletions
diff --git a/background_page.html b/background_page.html index a56028bc..f760ea70 100644 --- a/background_page.html +++ b/background_page.html @@ -4,6 +4,7 @@ <script type="text/javascript" src="lib/clipboard.js"></script> <script type="text/javascript" src="lib/utils.js"></script> <script type="text/javascript" src="background/settings.js"></script> +<script type="text/javascript" src="lib/completion.js"></script> <script type="text/javascript" charset="utf-8"> var currentVersion = utils.getCurrentVersion(); @@ -27,9 +28,7 @@ getCurrentTabUrl: getCurrentTabUrl, settings: handleSettings, getBookmarks: getBookmarks, - getAllBookmarks: getAllBookmarks, - getHistory: getHistory, - getTabsInCurrentWindow: getTabsInCurrentWindow, + filterCompleter: filterCompleter, }; var sendRequestHandlers = { @@ -46,6 +45,7 @@ isEnabledForUrl: isEnabledForUrl, saveHelpDialogSettings: saveHelpDialogSettings, selectSpecificTab: selectSpecificTab, + refreshCompleter: refreshCompleter, }; // Event handlers @@ -53,6 +53,28 @@ var getScrollPositionHandlers = {}; // tabId -> function(tab, scrollX, scrollY); var tabLoadedHandlers = {}; // tabId -> function() + var completionSources = { + smart: new completion.SmartCompletionSource({ + 'wiki ': [ 'Wikipedia (en)', 'http://en.wikipedia.org/wiki/%s' ], + 'luck ': [ 'Google Lucky (en)', 'http://www.google.com/search?q=%s&btnI=I%27m+Feeling+Lucky' ], + 'cc ' : [ 'dict.cc', 'http://www.dict.cc/?s=%s' ], + ';' : [ 'goto', '%s' ], + '?' : [ 'search', function(query) { return utils.createSearchUrl(query) } ], + }), + bookmarks: new completion.FuzzyBookmarkCompletionSource(), + history: new completion.FuzzyHistoryCompletionSource(20000), + tabs: new completion.FuzzyTabCompletionSource(), + } + var completers = { + omni: new completion.MultiCompleter([ + completionSources.smart, + completionSources.bookmarks, + completionSources.history, + completionSources.tabs, + ], 1), + tabs: new completion.MultiCompleter([ completionSources.tabs ], 0), + }; + chrome.extension.onConnect.addListener(function(port, name) { var senderTabId = port.sender.tab ? port.sender.tab.id : null; // If this is a tab we've been waiting to open, execute any "tab loaded" handlers, e.g. to restore @@ -74,9 +96,6 @@ if (portHandlers[port.name]) port.onMessage.addListener(portHandlers[port.name]); - - // prefetch history - useHistory(function() {}); }); chrome.extension.onRequest.addListener(function (request, sender, sendResponse) { @@ -289,66 +308,15 @@ }) } - function getAllBookmarks(args, port) { - function traverseTree(bookmarks, callback) { - for (var i = 0; i < bookmarks.length; ++i) { - callback(bookmarks[i]); - if (typeof bookmarks[i].children === "undefined") - continue; - traverseTree(bookmarks[i].children, callback); - } - }; - - chrome.bookmarks.getTree(function(bookmarks) { - var results = []; - traverseTree(bookmarks, function(bookmark) { - results.push(bookmark); - }); - port.postMessage({bookmarks:results}); - }); - }; - - var cachedHistory = null; - function useHistory(callback) { - if (cachedHistory !== null) { - callback(cachedHistory); - return; - } - - chrome.history.search({ - text: '', - maxResults: 20000, - startTime: 0, - }, function(history) { - // sorting in ascending order, so we can push new items to the end later - history.sort(function(a, b) { - return (a.lastVisitTime|| 0) - (b.lastVisitTime || 0); - }); - cachedHistory = history; - callback(cachedHistory); - }); + function refreshCompleter(request) { + completers[request.name].refresh(); } - chrome.history.onVisited.addListener(function(item) { - if (cachedHistory === null) return; - // only cache newly visited sites - if (item.visitCount === 1) - cachedHistory.push(item); - }); - - function getHistory(args, port) { - useHistory(function(history) { - port.postMessage({ - history: history.slice(Math.max(history.length - args.maxResults, 0)) - }); + function filterCompleter(args, port) { + completers[args.name].filter(args.query, args.maxResults, function(results) { + port.postMessage({ id: args.id, results: results }); }); - }; - - function getTabsInCurrentWindow(args, port) { - chrome.tabs.getAllInWindow(null, function(tabs) { - port.postMessage({tabs:tabs}); - }); - }; + } /* * Used by everyone to get settings from local storage. |
