Instantly share code, notes, and snippets.
Created
October 8, 2019 13:06
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save joffilyfe/236b985fdc522f9c0b666d1b28f09715 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
Verificar o encoding onde esse script será incluído. | |
Se UTF-8 ou outro. | |
Caso necessário, os acentos devem ser alterados. | |
*/ | |
var cookiePolicy = { | |
isActive: true, | |
// Aparência do texto | |
bgColor: "#1f1f1f", | |
textColor: "#ffffff", | |
// Mensagem nos idiomas | |
msgPt: "Este site usa cookies para garantir que você obtenha uma melhor experiência de navegação.", | |
msgEn: "This site uses cookies to ensure you get a better browsing experience.", | |
msgEs: "Este sitio utiliza cookies para garantizar una mejor experiencia de navegación.", | |
setCookie: function (cname, cvalue, exdays) { | |
var d = new Date(); | |
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); | |
var expires = "expires=" + d.toUTCString(); | |
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; | |
}, | |
getCookie: function (cname) { | |
var name = cname + "="; | |
var decodedCookie = decodeURIComponent(document.cookie); | |
var ca = decodedCookie.split(';'); | |
for (var i = 0; i < ca.length; i++) { | |
var c = ca[i]; | |
while (c.charAt(0) == ' ') { | |
c = c.substring(1); | |
} | |
if (c.indexOf(name) == 0) { | |
return c.substring(name.length, c.length); | |
} | |
} | |
return ""; | |
}, | |
checkLanguageAtUrl: function () { | |
var url = window.location.href, | |
urlPart = url.split("/"), | |
langParam = "", | |
splitParam = url.split("?"), | |
justParams = "", | |
theParam = "", | |
langParam = ""; | |
if (splitParam.length > 1) { | |
justParams = splitParam[1].split("&"); | |
for (var i = 0; i < justParams.length; i++) { | |
theParam = justParams[i].split("="); | |
if (theParam[0] == "lang" || theParam[0] == "language" || theParam[0] == "lng") { | |
langParam = theParam[1]; | |
} | |
} | |
} | |
if (langParam == "") { | |
// verifica idioma no caso do scielo.org, onde o idioma vem logo apos a primeira barra | |
if (urlPart[3] == "pt" || urlPart[3] == "pt_BR" || urlPart[3] == "pt-BR" || urlPart[3] == "pt_Br" || urlPart[3] == "pt-Br") { | |
langParam = urlPart[3]; | |
} else if (urlPart[3] == "en" || urlPart[3] == "EN" || urlPart[3] == "En") { | |
langParam = urlPart[3]; | |
} else if (urlPart[3] == "es" || urlPart[3] == "ES" || urlPart[3] == "Es") { | |
langParam = urlPart[3]; | |
} | |
} | |
if (langParam != "") { | |
return langParam; | |
} else { | |
return false; | |
} | |
}, | |
// hasScriptParam | |
hasParam: function () { | |
var thisScript = document.currentScript.src, | |
splitScript = thisScript.split("?"); | |
if (splitScript.length > 1) { | |
return true; | |
} else { | |
return false; | |
} | |
}, | |
// checkScriptParam | |
checkLanguageAtParam: function () { | |
if (cookiePolicy.hasParam()) { | |
var thisScript = document.currentScript.src, | |
splitParam = thisScript.split("?"), | |
justParams = splitParam[1], | |
langParam = "", | |
theParam = ""; | |
// verifica se existe mais de um parametro | |
if (justParams.split("&").length > 1) { | |
//console.log(justParams.split("&")); | |
justParams = justParams.split("&"); | |
nParams = justParams.length; | |
/* | |
console.log("temos mais de um parametro aqui. Veja: " + justParams + "\n"); | |
console.log("O total de parâmetros é: " + justParams.length + "\n"); | |
console.log("Os parâmetros são os seguintes: \n") | |
*/ | |
for (var i = 0; i < nParams; i++) { | |
theParam = justParams[i].split("="); | |
if (theParam[0] == "lang" || theParam[0] == "language") { | |
langParam = theParam[1]; | |
} | |
// console.log(justParams[i]); | |
} | |
} else { | |
theParam = justParams.split("="); | |
if (theParam[0] == "lang" || theParam[0] == "language") { | |
langParam = theParam[1]; | |
} | |
} | |
} | |
if (langParam != "") { | |
return langParam; | |
} else { | |
return false; | |
} | |
}, | |
checkCookie: function (cname) { | |
var cookieName = cookiePolicy.getCookie(cname); | |
// se não houver o cookie aceitando policitas de cookie, seta ele. | |
if (!(cookieName != "" && cookieName != null)) { | |
// procura parametro na url | |
if (cookiePolicy.checkLanguageAtUrl()) { | |
cookiePolicy.createElementCookieBar(cookiePolicy.checkLanguageAtUrl()); | |
//}else if(tem lang no parametro){ | |
} else if (cookiePolicy.checkLanguageAtParam()) { | |
cookiePolicy.createElementCookieBar(cookiePolicy.checkLanguageAtParam()); | |
// senao, procura no cookie language | |
} else if (cookiePolicy.getCookie("language")) { | |
cookiePolicy.createElementCookieBar(cookiePolicy.getCookie("language")); | |
// senão, procura no cookie lang | |
} else if (cookiePolicy.getCookie("lang")) { | |
cookiePolicy.createElementCookieBar(cookiePolicy.getCookie("lang")); | |
} else { | |
var userLang = navigator.language || navigator.userLanguage; | |
if (userLang != "" && userLang != null) { | |
cookiePolicy.createElementCookieBar(userLang); | |
/* | |
se não houver: | |
- idioma na url | |
- idioma no parametro da chamada do script | |
- cookie language | |
- cookie lang | |
- idioma do navegador | |
*/ | |
} else { | |
cookiePolicy.createElementCookieBar("en"); | |
} | |
} | |
} | |
}, | |
clearCookie: function (cname) { | |
cookiePolicy.setCookie(cname, "no", -365); | |
}, | |
createElementCookieBar: function (lang) { | |
//cria o elemento div | |
var div = document.createElement('div'); | |
div.setAttribute('class', 'alert-cookie-notification'); | |
//poe conteúdo | |
if (lang == "pt" || lang == "pt_BR" || lang == "pt-BR" || lang == "pt_Br" || lang == "pt-Br") { | |
div.innerHTML = cookiePolicy.msgPt; | |
} else if (lang == "en" || lang == "EN" || lang == "En") { | |
div.innerHTML = cookiePolicy.msgEn; | |
} else if (lang == "es" || lang == "ES" || lang == "Es") { | |
div.innerHTML = cookiePolicy.msgEs; | |
} | |
//estilisa o elemento | |
div.style.backgroundColor = cookiePolicy.bgColor; | |
div.style.fontFamily = "Helvetica, Sans-Serif"; | |
div.style.fontWeight = 100; | |
div.style.color = cookiePolicy.textColor; | |
div.style.fontSize = "16px"; | |
div.style.display = "block"; | |
div.style.position = "fixed"; | |
div.style.bottom = "0px"; | |
div.style.width = "100%"; | |
div.style.textAlign = "center"; | |
div.style.padding = "13px 10px"; | |
//cria elemento botão em formato de link | |
var link = document.createElement('a'); | |
link.setAttribute('class', 'btn'); | |
link.innerHTML = "OK"; | |
//aparência | |
link.style.margin = "5px"; | |
link.style.backgroundColor = "white"; | |
link.style.color = "black"; | |
link.style.borderRadius = 0; | |
link.style.border = "1px solid black"; | |
link.style.fontWeight = 400; | |
link.style.lineHeight = "20px"; | |
link.style.padding = "6px 16px"; | |
link.addEventListener("click", function () { | |
cookiePolicy.setCookie("cookie-policy-accepted", "yes", 365); | |
this.parentNode.style.display = "none"; | |
}); | |
div.appendChild(link); | |
//coloca ele dentro do body | |
document.body.appendChild(div); | |
}, | |
Init: function () { | |
if (cookiePolicy.isActive) { | |
cookiePolicy.checkCookie("cookie-policy-accepted"); | |
} else { | |
cookiePolicy.clearCookie(); | |
} | |
} | |
} | |
cookiePolicy.Init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment