From 10718c86f4c85e5e710b7808c392620f6b13b2bd Mon Sep 17 00:00:00 2001 From: Konrad Dzwinel Date: Sat, 5 Nov 2022 12:48:24 +0100 Subject: [PATCH] Show cookie expiry if cookieStore available. (#106) * Show cookie expiry if cookieStore available. * await works on non-promises Co-authored-by: Sam Macbeth * await works on non-promises Co-authored-by: Sam Macbeth Co-authored-by: Sam Macbeth --- .../storage-blocking/3rdparty.js | 10 +++++- .../storage-blocking/helpers/commonTests.js | 14 ++++++++ .../storage-blocking/iframe.js | 33 ++++++++----------- privacy-protections/storage-blocking/main.js | 17 ++++++++-- .../storage-blocking/style.css | 4 +++ 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/privacy-protections/storage-blocking/3rdparty.js b/privacy-protections/storage-blocking/3rdparty.js index abffe6e..e11c64e 100644 --- a/privacy-protections/storage-blocking/3rdparty.js +++ b/privacy-protections/storage-blocking/3rdparty.js @@ -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'; + }); + } } }); })(); diff --git a/privacy-protections/storage-blocking/helpers/commonTests.js b/privacy-protections/storage-blocking/helpers/commonTests.js index 3428d0f..1d06c4c 100644 --- a/privacy-protections/storage-blocking/helpers/commonTests.js +++ b/privacy-protections/storage-blocking/helpers/commonTests.js @@ -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'; + }); + } } }, { @@ -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'; + }); + } } } ]; diff --git a/privacy-protections/storage-blocking/iframe.js b/privacy-protections/storage-blocking/iframe.js index 4c5ad4d..5bd7c0e 100644 --- a/privacy-protections/storage-blocking/iframe.js +++ b/privacy-protections/storage-blocking/iframe.js @@ -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 - }); + }; } })); } diff --git a/privacy-protections/storage-blocking/main.js b/privacy-protections/storage-blocking/main.js index 1661bc6..8991fb8 100644 --- a/privacy-protections/storage-blocking/main.js +++ b/privacy-protections/storage-blocking/main.js @@ -283,8 +283,9 @@ function retrieveData () { const li = document.createElement('li'); li.id = `test-${test.id.replace(' ', '-')}`; - li.innerHTML = `${test.id} - `; + li.innerHTML = `${test.id} - `; const valueSpan = li.querySelector('.value'); + const extraSpan = li.querySelector('.extra'); testsDetailsElement.appendChild(li); @@ -295,7 +296,7 @@ function retrieveData () { result .then(data => { if (Array.isArray(data)) { - valueSpan.innerHTML = `
    ${data.map(r => `
  • ${r.test} - ${r.value} ${r.error ? '(❌ ' + r.error + ')' : ''}
  • `).join('')}
`; + valueSpan.innerHTML = `
    ${data.map(r => `
  • ${r.test} - ${r.value} ${r.error ? '(❌ ' + r.error + ')' : ''} ${r.extra ? '(' + r.extra + ')' : ''}
  • `).join('')}
`; data.forEach(item => addTestResult(`${test.id} - ${item.test}`, item.value)); } else { @@ -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(); diff --git a/privacy-protections/storage-blocking/style.css b/privacy-protections/storage-blocking/style.css index 14dd8bf..2d2f68b 100644 --- a/privacy-protections/storage-blocking/style.css +++ b/privacy-protections/storage-blocking/style.css @@ -5,3 +5,7 @@ .value { color: gray; } + +.extra { + color: silver; +} \ No newline at end of file