]> git.wh0rd.org - chrome-ext/clearhistory-advance-fork.git/blob - javascript/clearhistory.js
clearhistory-1.5
[chrome-ext/clearhistory-advance-fork.git] / javascript / clearhistory.js
1 // Copyright 2011 Google Inc. All Rights Reserved.
2
3 /**
4 * @fileoverview The main script for the Clear History extension for Chrome.
5 * @author arunjit@google.com (Arunjit Singh)
6 */
7
8 /**
9 * Removes a single cookie
10 * @param {Object.<Cookie>} cookie The cookie to delete.
11 * @param {Function} callback The function to invoke when the cookie is deleted.
12 */
13 function removeCookie(cookie, callback) {
14 var url = 'http' + (cookie.secure ? 's' : '') + '://' + cookie.domain +
15 cookie.path;
16 chrome.cookies.remove({url: url, name: cookie.name}, callback);
17 }
18
19 /**
20 * Removes all cookies
21 * @param {Function=} callback The function to invoke when all cookies are
22 * deleted (optional).
23 */
24 function clearCookies(callback) {
25 callback = callback || function() {};
26 chrome.cookies.getAll({}, function(cookies) {
27 var len = cookies.length;
28 // Synchronize the cookie deletion and execute the callback only when all
29 // cookie deletion functions have called back (finished).
30 (function doCookieCleanup(i) {
31 if (i === len) {
32 callback();
33 } else {
34 removeCookie(cookies[i], function() {
35 doCookieCleanup(i + 1);
36 });
37 }
38 })(0);
39 });
40 }
41
42 /**
43 * Callback for when history deletion is successful
44 */
45 function didClearHistory() {
46 var time = ~~(localStorage['time']) || CONSTANTS.DEFAULT_TIME;
47 time = getUnitsForTime(time);
48 var timeString = (time.time === -1) ?
49 chrome.i18n.getMessage('notificationTimeAll') :
50 chrome.i18n.getMessage('notificationTime',
51 [time.time, time.units]);
52 var message = chrome.i18n.getMessage('notificationBody', timeString);
53 var notification = webkitNotifications.createNotification(
54 chrome.extension.getURL('/images/icon48.png'),
55 chrome.i18n.getMessage('notificationTitle'),
56 message);
57 notification.show();
58 setTimeout(function() {
59 notification.cancel();
60 }, 5000);
61 }
62
63 /**
64 * Clears the history for the amount of time stored in {@code localStorage}
65 */
66 function clearHistory() {
67 //Get the values from localStorage
68 // time is in hours
69 var time = ~~(localStorage['time']) || CONSTANTS.DEFAULT_TIME;
70 if (time === -1) {
71 // Delete everything
72 chrome.history.deleteAll(didClearHistory);
73 } else {
74 // Create the range
75 var now = (new Date).getTime();
76 var range = {
77 startTime: (now - time * 60 * 60 * 1000), // time from hrs to ms
78 endTime: now
79 };
80 // Delete history in the range
81 chrome.history.deleteRange(range, didClearHistory);
82 }
83 }
84
85 /**
86 * Executes when the user clicks the browser action. Uses stored values from
87 * {@code localStorage}
88 */
89 chrome.browserAction.onClicked.addListener(function(tab) {
90 //calls the method to provide the Google Analytics Data
91 getAnalyticsData();
92 // Get the value from localStorage
93 var showPrompt = localStorage['prompt'] || CONSTANTS.YES;
94
95 // The confirmation message to ask
96 var message = chrome.i18n.getMessage('confirmPrompt');
97
98 // Clear cookies, and then clear the history
99 if (showPrompt === CONSTANTS.YES) {
100 confirm(message) && clearCookies(clearHistory);
101 } else {
102 clearCookies(clearHistory);
103 }
104 });
105
106 var _gaq = _gaq || [];
107 /**
108 * provides the Google analytics data
109 */
110 function getAnalyticsData() {
111 _gaq.push(['_setAccount', 'UA-28968723-1']);
112 _gaq.push(['_trackPageview']);
113
114 (function() {
115 var ga = document.createElement('script'); ga.type = 'text/javascript';
116 ga.async = true;
117 ga.src = 'https://ssl.google-analytics.com/ga.js';
118 var s = document.getElementsByTagName('script')[0];
119 s.parentNode.insertBefore(ga, s);
120 })();
121 }