Last active
December 18, 2020 12:47
-
-
Save martinschierle/756fcb6f0a1d2f0084d970b8276faa6d to your computer and use it in GitHub Desktop.
Serverside paywall vs clientside paywall js snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkPaywall() { | |
let structured = JSON.parse(document.querySelector("script[type*='ld+json']").innerText); | |
let hasPart = null; | |
if(structured.hasPart) { | |
hasPart = structured.hasPart; | |
} | |
else if(structured.length) { | |
for(var i = 0; i < structured.length; i++) { | |
if(structured[i].hasPart) { | |
hasPart = structured[i].hasPart; | |
break; | |
} | |
} | |
} | |
if(!hasPart) { | |
window.alert("Doesn't seem paywalled?"); | |
return; | |
} | |
let paywallCss = null; | |
if(hasPart.cssSelector) { | |
paywallCss = hasPart.cssSelector; | |
} | |
else if(hasPart.length) { | |
for(var i = 0; i < hasPart.length; i++) { | |
if(hasPart[i].cssSelector) { | |
paywallCss = hasPart[i].cssSelector; | |
break; | |
} | |
} | |
} | |
if(!paywallCss) { | |
window.alert("Content not paywalled"); | |
return; | |
} | |
let e = document.querySelector(paywallCss); | |
if(!e) { | |
window.alert("Server-Side Paywall"); | |
return; | |
} | |
let text = e.innerText; | |
if(text.length<300) { | |
window.alert("Server-Side Paywall"); | |
return; | |
} | |
window.alert("Client-Side Paywall"); | |
} | |
checkPaywall(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment