-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
157 lines (147 loc) · 4.07 KB
/
popup.js
File metadata and controls
157 lines (147 loc) · 4.07 KB
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
148
149
150
151
152
153
154
155
156
157
// hard-coded to match pdslib-firefox's capacities
const FILTER_CAPACITIES = { Nc: 1.0, C: 8.0, QTrigger: 2.0, QSource: 4.0 };
const DAY_MS = 1000 * 60 * 60 * 24;
const EPOCH_MS = 7 * DAY_MS;
/**
* @returns {number} current epoch number
*/
function getCurrentEpoch() {
return Math.floor(Date.now() / EPOCH_MS);
}
/**
* @param {string} type
* @param {number} epoch
* @param {string} uri
* @returns {number} used budget for this filter
*/
function computeUsedBudget(type, epoch, uri) {
let remaining = navigator.privateAttribution.getBudget(type, epoch, uri);
if (remaining === -1.0) remaining = 0.0;
return FILTER_CAPACITIES[type] - remaining;
}
/**
* @param {Array<string>} sites
* @param {string} filterType
* @returns {Object<string,number>} label -> usedBudget
*/
function buildUsageMap(sites, filterType) {
const epoch = getCurrentEpoch();
return sites.reduce((map, site) => {
const key = site ? site.replace(/^www\./, "") : "global";
map[key] = computeUsedBudget(filterType, epoch, site);
return map;
}, {});
}
function renderBarChart(
canvasId,
xAxisTitle,
leftData,
leftColor,
leftLineY,
rightData,
rightColor,
rightLineY
) {
const ctx = document.getElementById(canvasId).getContext("2d");
const leftKeys = Object.keys(leftData),
rightKeys = Object.keys(rightData);
const leftVals = Object.values(leftData),
rightVals = Object.values(rightData);
const labels = [...leftKeys, ...rightKeys];
const data = [...leftVals, ...rightVals];
const colors = [
...leftVals.map(() => leftColor),
...rightVals.map(() => rightColor),
];
new Chart(ctx, {
type: "bar",
data: {
labels,
datasets: [{ label: "Privacy Loss", data, backgroundColor: colors }],
},
options: {
responsive: true,
plugins: {
legend: { display: false },
annotation: {
annotations: {
lineLeft: {
type: "line",
yMin: leftLineY,
yMax: leftLineY,
borderColor: leftColor,
borderWidth: 2,
},
lineRight: {
type: "line",
yMin: rightLineY,
yMax: rightLineY,
borderColor: rightColor,
borderWidth: 2,
},
},
},
},
scales: {
y: {
beginAtZero: true,
max: Math.max(leftLineY, rightLineY),
title: { display: true, text: "Privacy Loss" },
},
x: { title: { display: true, text: xAxisTitle } },
},
},
});
}
function renderCollusionChart() {
const globalMap = buildUsageMap([""], "C");
const siteMap = buildUsageMap(["shoes.example", "toys.example"], "Nc");
renderBarChart(
"CollusionChart",
"Privacy Filters",
globalMap,
"red",
FILTER_CAPACITIES.C,
siteMap,
"blue",
FILTER_CAPACITIES.Nc
);
}
function renderQuotaChart() {
const convMap = buildUsageMap(["shoes.example", "toys.example"], "QTrigger");
const implMap = buildUsageMap(["blog.example", "news.example"], "QSource");
renderBarChart(
"QuotaChart",
"Quota Filters",
convMap,
"orange",
FILTER_CAPACITIES.QTrigger,
implMap,
"purple",
FILTER_CAPACITIES.QSource
);
}
function showNoApiMessage() {
const container = document.getElementById("chartContainer");
container.style.display = "block";
container.style.color = "red";
container.innerHTML =
"navigator.privateAttribution is undefined! Make sure:<br>" +
"1. Firefox was compiled with MOZ_TELEMETRY_REPORTING=1<br>" +
"2. dom.origin-trials.private-attribution.state = 1 in about:config<br>" +
"3. 'privacy-preserving ad measurement' is enabled.";
}
document.addEventListener("DOMContentLoaded", () => {
if (!navigator.privateAttribution) {
console.error("Private Attribution API unavailable");
showNoApiMessage();
return;
}
renderCollusionChart();
renderQuotaChart();
document.addEventListener("keydown", (e) => {
if (e.key === " " || e.key === "Spacebar" || e.keyCode === 32) {
navigator.privateAttribution.clearBudgets();
}
});
});