Skip to content

Instantly share code, notes, and snippets.

@martinheidegger
Last active Aug 3, 2020
Embed
What would you like to do?
Replace {{domain}} with the server domain you set up. This script will change the classname of the body element in a html document to either `tpc-ok` or `tpc-fail` if third-party-cookies are enabled or not. Adopted from https://stackoverflow.com/questions/3550790/check-if-third-party-cookies-are-enabled
.tpc-ok-only {
display: none;
}
.tpc-fail-only {
display: none;
}
.tpc-loading {
display: block;
}
body.tpc-ok .tpc-loading,
body.tpc-fail .tpc-loading {
display: none;
}
body.tpc-ok .tpc-ok-only {
display: block;
}
body.tpc-fail .tpc-fail-only {
display: block;
}
;(function () {
if (document.getElementById('tpc-test')) {
// Prevent duplicate execution
return
}
var body = document.body
var tpcDomain = '//{domain}/'
var step1 = document.createElement('script')
step1.setAttribute('src', tpcDomain + 'step1.js')
var step2 = document.createElement('script')
step2.setAttribute('src', tpcDomain + 'step2.js')
window._3rd_party_test_step1_loaded = function () {
body.classList.add('tpc-step-2')
body.classList.remove('tpc-step-1')
body.removeChild(step1)
body.appendChild(step2)
}
window._3rd_party_test_step2_loaded = function (cookieSuccess) {
body.classList.toggle('tpc-ok', cookieSuccess)
body.classList.toggle('tpc-fail', !cookieSuccess)
body.classList.remove('tpc-step-2')
body.removeChild(step2)
}
body.classList.add('tpc-step-1')
body.appendChild(step1)
})()
server {
listen 80;
server_name {domain}
# don't allow user's browser to cache these replies
expires -1;
add_header Cache-Control "private";
etag off;
location = /step1.js {
add_header Content-Type 'application/javascript; charset=UTF-8';
add_header Set-Cookie "third_party_c_t=hey there!;Max-Age=172800";
return 200 'window._3rd_party_test_step1_loaded();';
}
location = /step2.js {
add_header Content-Type 'application/javascript; charset=UTF-8';
set $test 'false';
if ($cookie_third_party_c_t = 'hey there!') {
set $test 'true';
# clear the cookie
add_header Set-Cookie "third_party_c_t=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
return 200 'window._3rd_party_test_step2_loaded($test);';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment