-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtut_removing_browsingdata.html
147 lines (130 loc) · 4.34 KB
/
tut_removing_browsingdata.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
---
layout: default-withsidebar
title: Removing browsing data
copyright: opera-google-ccby
originalsource: http://developer.chrome.com/extensions/browsingData.html
---
<h2>Introduction</h2>
<p>You can use the <a href="https://developer.chrome.com/extensions/browsingData">chrome.browsingData API</a> to remove browsing data from a user's local profile.</p>
<h2 id="manifest">Manifest</h2>
<p>
You must declare the "browsingData" permission in the
<a href="manifest.html">extension manifest</a> to use this API.
</p>
<pre class="prettyprint" data-filename="manifest.json">
{
"name": "My extension",
...
<b>"permissions": [
"browsingData",
]</b>,
...
}
</pre>
<h2 id="usage">Usage</h2>
<p>
The simplest use-case for the <a href="https://developer.chrome.com/extensions/browsingData">chrome.browsingData API</a> is a a time-based mechanism for clearing a
user's browsing data. Your code should provide a timestamp which indicates the
historical date after which the user's browsing data should be removed. This
timestamp is formatted as the number of milliseconds since the Unix epoch
(which can be retrieved from a JavaScript <code>Date</code> object via the
<code>getTime</code> method).
</p>
<p>
For example, to clear all of a user's browsing data from the last week, you
might write code as follows:
</p>
<pre class="prettyprint">var callback = function () {
// Do something clever here once data has been removed.
};
var millisecondsPerWeek = 1000 * 60 * 60 * 24 * 7;
var oneWeekAgo = (new Date()).getTime() - millisecondsPerWeek;
chrome.browsingData.remove({
"since": oneWeekAgo
}, {
"appcache": true,
"cache": true,
"cookies": true,
"downloads": true,
"fileSystems": true,
"formData": true,
"history": true,
"indexedDB": true,
"localStorage": true,
"pluginData": true,
"passwords": true,
"webSQL": true
}, callback);</pre>
<p>
The <code>chrome.browsingData.remove</code> method allows you to remove
various types of browsing data with a single call, and will be much faster
than calling multiple more specific methods. If, however, you only want to
clear one specific type of browsing data (cookies, for example), the more
granular methods offer a readable alternative to a call filled with JSON.
</p>
<pre class="prettyprint">var callback = function () {
// Do something clever here once data has been removed.
};
var millisecondsPerWeek = 1000 * 60 * 60 * 24 * 7;
var oneWeekAgo = (new Date()).getTime() - millisecondsPerWeek;
chrome.browsingData.removeCookies({
"since": oneWeekAgo
}, callback);</pre>
<p class="caution">
<strong>Important</strong>: Removing browsing data involves a good deal of
heavy lifting in the background, and can take <em>tens of seconds</em> to
complete, depending on a user's profile. You should use the callback mechanism
to keep your users up to date on the removal's status.
</p>
<h2 id="origin_types">Origin Types</h2>
<p>
Adding an <code>originTypes</code> property to the API's options object allows
you to specify which types of origins ought to be effected. Currently, origins
are divided into three categories:
</p>
<ul>
<li>
<code>unprotectedWeb</code> covers the general case of websites that users
visit without taking any special action. If you don't specify an
<code>originTypes</code>, the API defaults to removing data from unprotected
web origins.
</li>
<li>
<code>extension</code> covers origins under the
<code>chrome-extensions:</code> scheme. Removing extension data is, again,
something you should be very careful about.
</li>
</ul>
<p>
We could adjust the previous example to remove only data from unprotected
websites as follows:
</p>
<pre class="prettyprint">var callback = function () {
// Do something clever here once data has been removed.
};
var millisecondsPerWeek = 1000 * 60 * 60 * 24 * 7;
var oneWeekAgo = (new Date()).getTime() - millisecondsPerWeek;
chrome.browsingData.remove({
"since": oneWeekAgo,
<b>"originTypes": {
"unprotectedWeb": true
}</b>
}, {
"appcache": true,
"cache": true,
"cookies": true,
"downloads": true,
"fileSystems": true,
"formData": true,
"history": true,
"indexedDB": true,
"localStorage": true,
"serverBoundCertificates": true,
"pluginData": true,
"passwords": true,
"webSQL": true
}, callback);</pre>
<p class="caution">
<strong>Seriously</strong>: Be careful when using this functionality. Your users will write you angry emails if they're not well-informed about what to
expect when your extension removes data on their behalf.
</p>