Skip to content

Commit

Permalink
Show cookie expiry if cookieStore available. (#106)
Browse files Browse the repository at this point in the history
* Show cookie expiry if cookieStore available.

* await works on non-promises

Co-authored-by: Sam Macbeth <sammacbeth@users.noreply.github.com>

* await works on non-promises

Co-authored-by: Sam Macbeth <sammacbeth@users.noreply.github.com>

Co-authored-by: Sam Macbeth <sammacbeth@users.noreply.github.com>
  • Loading branch information
kdzwinel and sammacbeth authored Nov 5, 2022
1 parent 0086b7b commit 10718c8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
10 changes: 9 additions & 1 deletion privacy-protections/storage-blocking/3rdparty.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@
(function () {
const src = document.currentScript.src;
const trackingDomain = src.indexOf('https://broken.third-party.site/') === 0;
const cookieName = trackingDomain ? 'tptdata' : 'tpsdata';

commonTests.push({
id: `JS cookie (3rd party ${trackingDomain ? 'tracking' : 'safe'} script)`,
store: (data) => {
document.cookie = `tp${trackingDomain ? 't' : 's'}data=${data}; expires= Wed, 21 Aug 2030 20:00:00 UTC;`;
document.cookie = `${cookieName}=${data}; expires= Wed, 21 Aug 2030 20:00:00 UTC;`;
},
retrive: () => {
return trackingDomain ? document.cookie.match(/tptdata=([0-9]+)/)[1] : document.cookie.match(/tpsdata=([0-9]+)/)[1];
},
extra: () => {
if (window.cookieStore) {
return window.cookieStore.get(cookieName).then(cookie => {
return 'expires in ' + ((cookie.expires - Date.now()) / (1000 * 60 * 60 * 24)).toFixed(2) + ' days';
});
}
}
});
})();
14 changes: 14 additions & 0 deletions privacy-protections/storage-blocking/helpers/commonTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ const commonTests = [
},
retrive: () => {
return document.cookie.match(/jsdata=([0-9]+)/)[1];
},
extra: () => {
if (window.cookieStore) {
return cookieStore.get('jsdata').then(cookie => {
return 'expires in ' + ((cookie.expires - Date.now()) / (1000 * 60 * 60 * 24)).toFixed(2) + ' days';
});
}
}
},
{
Expand Down Expand Up @@ -105,6 +112,13 @@ const commonTests = [
},
retrive: async () => {
return (await cookieStore.get('cookiestoredata')).value;
},
extra: () => {
if (window.cookieStore) {
return cookieStore.get('cookiestoredata').then(cookie => {
return 'expires in ' + ((cookie.expires - Date.now()) / (1000 * 60 * 60 * 24)).toFixed(2) + ' days';
});
}
}
}
];
33 changes: 14 additions & 19 deletions privacy-protections/storage-blocking/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,28 @@ function storeData (randomNumber) {
}

function retrieveData () {
return Promise.all(commonTests.map(test => {
return Promise.all(commonTests.map(async (test) => {
try {
const result = test.retrive();
const value = await result;
let extra;

if (result instanceof Promise) {
return result
.then(value => ({
test: test.id,
value: value
}))
.catch(e => ({
test: test.id,
value: null,
error: e.message
}));
} else {
return Promise.resolve({
test: test.id,
value: result
});
if (test.extra) {
const extraResult = test.extra();
extra = await extraResult;
}

return {
test: test.id,
value,
extra
};
} catch (e) {
return Promise.resolve({
return {
test: test.id,
value: null,
error: e.message ? e.message : e
});
};
}
}));
}
Expand Down
17 changes: 15 additions & 2 deletions privacy-protections/storage-blocking/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ function retrieveData () {

const li = document.createElement('li');
li.id = `test-${test.id.replace(' ', '-')}`;
li.innerHTML = `${test.id} - <span class='value'>…</span>`;
li.innerHTML = `${test.id} - <span class='value'>…</span> <span class='extra'></span>`;
const valueSpan = li.querySelector('.value');
const extraSpan = li.querySelector('.extra');

testsDetailsElement.appendChild(li);

Expand All @@ -295,7 +296,7 @@ function retrieveData () {
result
.then(data => {
if (Array.isArray(data)) {
valueSpan.innerHTML = `<ul>${data.map(r => `<li>${r.test} - ${r.value} ${r.error ? '(❌ ' + r.error + ')' : ''}</li>`).join('')}</ul>`;
valueSpan.innerHTML = `<ul>${data.map(r => `<li>${r.test} - ${r.value} ${r.error ? '(❌ ' + r.error + ')' : ''} <span class='extra'>${r.extra ? '(' + r.extra + ')' : ''}</span></li>`).join('')}</ul>`;

data.forEach(item => addTestResult(`${test.id} - ${item.test}`, item.value));
} else {
Expand All @@ -321,6 +322,18 @@ function retrieveData () {
valueSpan.innerHTML = `❌ error thrown ("${e.message ? e.message : e}")`;
addTestResult(test.id, null);
}

if (test.extra) {
const result = test.extra();

if (result instanceof Promise) {
result.then(actualResult => {
extraSpan.innerText = actualResult ? `(${actualResult})` : '';
});
} else if (result) {
extraSpan.innerText = `(${result})`;
}
}
});

updateSummary();
Expand Down
4 changes: 4 additions & 0 deletions privacy-protections/storage-blocking/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
.value {
color: gray;
}

.extra {
color: silver;
}

0 comments on commit 10718c8

Please sign in to comment.