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..7205e84 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.', @@ -108,36 +116,55 @@ 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; } } }; -(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(); - }; + if (!testData.delay) { + s.src = testData.url; + } + + document.body.appendChild(s); + if (testData.delay) { + setTimeout(() => { s.src = testData.url; + }, 500); + } + }); +} - 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;