+// Copyright 2011 Google Inc. All Rights Reserved.
+
+/**
+ * @fileoverview The main script for the Clear History extension for Chrome.
+ * @author arunjit@google.com (Arunjit Singh)
+ */
+
+/**
+ * Removes a single cookie
+ * @param {Object.<Cookie>} cookie The cookie to delete.
+ * @param {Function} callback The function to invoke when the cookie is deleted.
+ */
+function removeCookie(cookie, callback) {
+ var url = 'http' + (cookie.secure ? 's' : '') + '://' + cookie.domain +
+ cookie.path;
+ chrome.cookies.remove({url: url, name: cookie.name}, callback);
+}
+
+/**
+ * Removes all cookies
+ * @param {Function=} callback The function to invoke when all cookies are
+ * deleted (optional).
+ */
+function clearCookies(callback) {
+ callback = callback || function() {};
+ chrome.cookies.getAll({}, function(cookies) {
+ var len = cookies.length;
+ // Synchronize the cookie deletion and execute the callback only when all
+ // cookie deletion functions have called back (finished).
+ (function doCookieCleanup(i) {
+ if (i === len) {
+ callback();
+ } else {
+ removeCookie(cookies[i], function() {
+ doCookieCleanup(i + 1);
+ });
+ }
+ })(0);
+ });
+}
+
+/**
+ * Callback for when history deletion is successful
+ */
+function didClearHistory() {
+ var time = ~~(localStorage['time']) || CONSTANTS.DEFAULT_TIME;
+ time = getUnitsForTime(time);
+ var timeString = (time.time === -1) ?
+ chrome.i18n.getMessage('notificationTimeAll') :
+ chrome.i18n.getMessage('notificationTime',
+ [time.time, time.units]);
+ var message = chrome.i18n.getMessage('notificationBody', timeString);
+ var notification = webkitNotifications.createNotification(
+ chrome.extension.getURL('/images/icon48.png'),
+ chrome.i18n.getMessage('notificationTitle'),
+ message);
+ notification.show();
+ setTimeout(function() {
+ notification.cancel();
+ }, 5000);
+}
+
+/**
+ * Clears the history for the amount of time stored in {@code localStorage}
+ */
+function clearHistory() {
+ //Get the values from localStorage
+ // time is in hours
+ var time = ~~(localStorage['time']) || CONSTANTS.DEFAULT_TIME;
+ if (time === -1) {
+ // Delete everything
+ chrome.history.deleteAll(didClearHistory);
+ } else {
+ // Create the range
+ var now = (new Date).getTime();
+ var range = {
+ startTime: (now - time * 60 * 60 * 1000), // time from hrs to ms
+ endTime: now
+ };
+ // Delete history in the range
+ chrome.history.deleteRange(range, didClearHistory);
+ }
+}
+
+/**
+ * Executes when the user clicks the browser action. Uses stored values from
+ * {@code localStorage}
+ */
+chrome.browserAction.onClicked.addListener(function(tab) {
+ //calls the method to provide the Google Analytics Data
+ getAnalyticsData();
+ // Get the value from localStorage
+ var showPrompt = localStorage['prompt'] || CONSTANTS.YES;
+
+ // The confirmation message to ask
+ var message = chrome.i18n.getMessage('confirmPrompt');
+
+ // Clear cookies, and then clear the history
+ if (showPrompt === CONSTANTS.YES) {
+ confirm(message) && clearCookies(clearHistory);
+ } else {
+ clearCookies(clearHistory);
+ }
+});
+
+ var _gaq = _gaq || [];
+/**
+ * provides the Google analytics data
+ */
+function getAnalyticsData() {
+ _gaq.push(['_setAccount', 'UA-28968723-1']);
+ _gaq.push(['_trackPageview']);
+
+ (function() {
+ var ga = document.createElement('script'); ga.type = 'text/javascript';
+ ga.async = true;
+ ga.src = 'https://ssl.google-analytics.com/ga.js';
+ var s = document.getElementsByTagName('script')[0];
+ s.parentNode.insertBefore(ga, s);
+ })();
+}