diff options
author | skerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-06 17:34:14 +0000 |
---|---|---|
committer | skerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-06 17:34:14 +0000 |
commit | f48cc36c2436252def86141a55e4eb4c4abff7a7 (patch) | |
tree | d3098530d37a0f606928db3b59281feb45793df8 /chrome/common/extensions/docs | |
parent | 13a1166eb46a5b98e1cf5fce21ae327f3e059aa5 (diff) | |
download | chromium_src-f48cc36c2436252def86141a55e4eb4c4abff7a7.zip chromium_src-f48cc36c2436252def86141a55e4eb4c4abff7a7.tar.gz chromium_src-f48cc36c2436252def86141a55e4eb4c4abff7a7.tar.bz2 |
Add a simple example history extension.
BUG=32362
TEST=Manual.
Review URL: http://codereview.chromium.org/1568012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43727 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions/docs')
4 files changed, 138 insertions, 0 deletions
diff --git a/chrome/common/extensions/docs/examples/api/history/showHistory/clock.png b/chrome/common/extensions/docs/examples/api/history/showHistory/clock.png Binary files differnew file mode 100644 index 0000000..08682cf --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/history/showHistory/clock.png diff --git a/chrome/common/extensions/docs/examples/api/history/showHistory/manifest.json b/chrome/common/extensions/docs/examples/api/history/showHistory/manifest.json new file mode 100755 index 0000000..2b271df --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/history/showHistory/manifest.json @@ -0,0 +1,10 @@ +{ + "name": "Typed URL History", + "version": "1.0", + "description": "Reads your history, and shows the top ten pages you go to by typing the URL.", + "permissions": ["history", "tabs"], + "browser_action": { + "default_popup": "typedUrls.html", + "default_icon": "clock.png" + } +} diff --git a/chrome/common/extensions/docs/examples/api/history/showHistory/typedUrls.html b/chrome/common/extensions/docs/examples/api/history/showHistory/typedUrls.html new file mode 100644 index 0000000..26c86a3 --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/history/showHistory/typedUrls.html @@ -0,0 +1,15 @@ +<!DOCTYPE HTML> +<html> + <head> + <title>Recently Typed URLs</title> + <style> + body {min-width: 250px;} + </style> + <script src='typedUrls.js'></script> + </head> + + <body onload='buildTypedUrlList("typedUrl_div")' > + <h2>Recently Typed URLs:</h2> + <div id='typedUrl_div'></div> + </body> +</html> diff --git a/chrome/common/extensions/docs/examples/api/history/showHistory/typedUrls.js b/chrome/common/extensions/docs/examples/api/history/showHistory/typedUrls.js new file mode 100644 index 0000000..1b7f03b --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/history/showHistory/typedUrls.js @@ -0,0 +1,113 @@ +// Event listner for clicks on links in a browser action popup. +// Open the link in a new tab of the current window. +function onAnchorClick(event) { + chrome.tabs.create({ + selected: true, + url: event.srcElement.href + }); + return false; +} + +// Given an array of URLs, build a DOM list of those URLs in the +// browser action popup. +function buildPopupDom(divName, data) { + var popupDiv = document.getElementById(divName); + + var ul = document.createElement('ul'); + popupDiv.appendChild(ul); + + for (var i = 0, ie = data.length; i < ie; ++i) { + var a = document.createElement('a'); + a.href = data[i]; + a.appendChild(document.createTextNode(data[i])); + a.addEventListener('click', onAnchorClick); + + var li = document.createElement('li'); + li.appendChild(a); + + ul.appendChild(li); + } +} + +// Search history to find up to ten links that a user has typed in, +// and show those links in a popup. +function buildTypedUrlList(divName) { + // To look for history items visited in the last week, + // subtract a week of microseconds from the current time. + var microsecondsPerWeek = 1000 * 60 * 60 * 24 * 7; + var oneWeekAgo = (new Date).getTime() - microsecondsPerWeek; + + // Track the number of callbacks from chrome.history.getVisits() + // that we expect to get. When it reaches zero, we have all results. + var numRequestsOutstanding = 0; + + chrome.history.search({ + 'text': '', // Return every history item.... + 'startTime': oneWeekAgo // that was accessed less than one week ago. + }, + function(historyItems) { + // For each history item, get details on all visits. + for (var i = 0; i < historyItems.length; ++i) { + var url = historyItems[i].url; + var processVisitsWithUrl = function(url) { + // We need the url of the visited item to process the visit. + // Use a closure to bind the url into the callback's args. + return function(visitItems) { + processVisits(url, visitItems); + }; + }; + chrome.history.getVisits({url: url}, processVisitsWithUrl(url)); + numRequestsOutstanding++; + } + if (!numRequestsOutstanding) { + onAllVisitsProcessed(); + } + }); + + + // Maps URLs to a count of the number of times the user typed that URL into + // the omnibox. + var urlToCount = {}; + + // Callback for chrome.history.getVisits(). Counts the number of + // times a user visited a URL by typing the address. + var processVisits = function(url, visitItems) { + for (var i = 0, ie = visitItems.length; i < ie; ++i) { + // Ignore items unless the user typed the URL. + if (visitItems[i].transition != 'typed') { + continue; + } + + if (!urlToCount[url]) { + urlToCount[url] = 0; + } + + urlToCount[url]++; + } + + // If this is the final outstanding call to processVisits(), + // then we have the final results. Use them to build the list + // of URLs to show in the popup. + if (!--numRequestsOutstanding) { + onAllVisitsProcessed(); + } + }; + + // This function is called when we have the final list of URls to display. + var onAllVisitsProcessed = function() { + // Get the top scorring urls. + urlArray = []; + for (var url in urlToCount) { + urlArray.push(url); + } + + // Sort the URLs by the number of times the user typed them. + urlArray.sort(function(a, b) { + return urlToCount[b] - urlToCount[a]; + }); + + buildPopupDom(divName, urlArray.slice(0, 10)); + }; +} + + |