From a61b1278b98b23b52b5af239ebd3e8d1cc165f0b Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Fri, 3 Nov 2023 12:33:16 +0100 Subject: [PATCH 1/3] Add test for surrogate included in head tag --- privacy-protections/surrogates/index.html | 1 + privacy-protections/surrogates/main.js | 56 ++++++++++++++--------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/privacy-protections/surrogates/index.html b/privacy-protections/surrogates/index.html index a63a5ef..a7b72e7 100644 --- a/privacy-protections/surrogates/index.html +++ b/privacy-protections/surrogates/index.html @@ -24,6 +24,7 @@ color: black; } +

[Home][Privacy Protections Tests][Surrogates Test Page]

diff --git a/privacy-protections/surrogates/main.js b/privacy-protections/surrogates/main.js index e9a4f75..1cfdbf3 100644 --- a/privacy-protections/surrogates/main.js +++ b/privacy-protections/surrogates/main.js @@ -49,6 +49,14 @@ function checkSurrogate () { } const surrogates = { + 'head': { + notes: 'Loading surrogate in ', + load: () => Promise.resolve(checkSurrogate()), // included in the html + cleanUp: () => { + document.getElementById('head-tag').remove(); + delete window.ga; + } + }, 'main-frame': { url: 'https://google-analytics.com/analytics.js', notes: 'Loading surrogate in the main frame.', @@ -111,33 +119,37 @@ const surrogates = { } }; -(async function loadSurrogates () { - for (const [name, testData] of Object.entries(surrogates)) { - if (testData.url) { - await new Promise((resolve, reject) => { - const s = document.createElement('script'); +async function injectSurrogate(testData) { + return new Promise((resolve, reject) => { + const s = document.createElement('script'); - if (testData.crossOrigin) { - s.crossOrigin = testData.crossOrigin; - } - if (testData.integrity) { - s.integrity = testData.integrity; - } + if (testData.crossOrigin) { + s.crossOrigin = testData.crossOrigin; + } + if (testData.integrity) { + s.integrity = testData.integrity; + } - s.onload = () => { - updateTable({ name, testData }); - resolve(); - }; + s.onload = () => { + updateTable({ name, testData }); + resolve(); + }; - s.onerror = (error) => { - updateTable({ name, testData, error }); - resolve(); - }; + s.onerror = (error) => { + updateTable({ name, testData, error }); + resolve(); + }; - s.src = testData.url; + s.src = testData.url; - document.body.appendChild(s); - }); + document.body.appendChild(s); + }) +} + +(async function loadSurrogates () { + for (const [name, testData] of Object.entries(surrogates)) { + if (testData.url) { + await injectSurrogate(testData) } else { testData.load().then(result => { testData.test = () => result; From 94c77d103459357f8c6a624979528b0f3b12a176 Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Fri, 3 Nov 2023 14:24:40 +0100 Subject: [PATCH 2/3] Fix lint --- privacy-protections/surrogates/main.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/privacy-protections/surrogates/main.js b/privacy-protections/surrogates/main.js index 1cfdbf3..fe944b7 100644 --- a/privacy-protections/surrogates/main.js +++ b/privacy-protections/surrogates/main.js @@ -49,7 +49,7 @@ function checkSurrogate () { } const surrogates = { - 'head': { + head: { notes: 'Loading surrogate in ', load: () => Promise.resolve(checkSurrogate()), // included in the html cleanUp: () => { @@ -119,7 +119,7 @@ const surrogates = { } }; -async function injectSurrogate(testData) { +async function injectSurrogate (testData) { return new Promise((resolve, reject) => { const s = document.createElement('script'); @@ -143,13 +143,13 @@ async function injectSurrogate(testData) { s.src = testData.url; document.body.appendChild(s); - }) + }); } (async function loadSurrogates () { for (const [name, testData] of Object.entries(surrogates)) { if (testData.url) { - await injectSurrogate(testData) + await injectSurrogate(testData); } else { testData.load().then(result => { testData.test = () => result; From c6835cf331819b6b5ac66eb7c5cab0b70d4f8681 Mon Sep 17 00:00:00 2001 From: Sam Macbeth Date: Fri, 3 Nov 2023 16:19:48 +0100 Subject: [PATCH 3/3] Add delayed src setter test --- privacy-protections/surrogates/main.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/privacy-protections/surrogates/main.js b/privacy-protections/surrogates/main.js index fe944b7..7205e84 100644 --- a/privacy-protections/surrogates/main.js +++ b/privacy-protections/surrogates/main.js @@ -116,6 +116,13 @@ const surrogates = { return promise; } + }, + 'delayed-set': { + notes: 'Set script src after insert', + url: 'https://google-analytics.com/analytics.js', + delay: true, + test: checkSurrogate, + cleanUp: () => { delete window.ga; } } }; @@ -140,9 +147,17 @@ async function injectSurrogate (testData) { resolve(); }; - s.src = testData.url; + if (!testData.delay) { + s.src = testData.url; + } document.body.appendChild(s); + + if (testData.delay) { + setTimeout(() => { + s.src = testData.url; + }, 500); + } }); }