co2offset
Version:
Greenhouse Gas Accounting - ISO14064
89 lines (81 loc) • 3.38 kB
JavaScript
(function ( $ ) {
$.fn.co2offset = function() {
const originalURL = encodeURIComponent(window.location.hostname);
let html = `<a style="text-decoration:none" target"_blank" href="https://co2offset.io/jquery.html?host=${
originalURL
}">`
html += `<img src="https://api.corrently.io/v2.0/ghgmanage/statusimg?host=${
originalURL
}" style="margin-right:5px"/>`
html += "CO<sub>2</sub>offset"
html += "</a>"
this.html(html);
};
}( jQuery ));
$('document').ready(function() {
const getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return typeof sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
}
}
return false;
};
const updateCode = function() {
$('#badgelink').attr('href',"https://co2offset.io/badge.html?host="+encodeURIComponent($('#gencode').val()));
$('#compImage').attr('src',"https://api.corrently.io/v2.0/ghgmanage/statusimg?png=1&host="+encodeURIComponent($('#gencode').val()));
$('#linkText').attr('href',"https://co2offset.io/badge.html?host="+encodeURIComponent($('#gencode').val()));
$('#linkText').html("https://co2offset.io/badge.html?host="+encodeURIComponent($('#gencode').val()));
}
const updateCurrency = function() {
let visits = $('#amount').val() * 9793;
if($('input[name=currency]:checked').val() == 'USD') {
$('.currency').html("$");
visits = visits / 0.82;
} else {
$('.currency').html("€");
}
$('#visits').html("<strong>"+Math.round(visits)+"</strong>");
}
const checkout = function() {
$.getJSON("https://api.corrently.io/v2.0/co2/offset?currency="+$('input[name=currency]:checked').val()+"&v="+($('#amount').val()*100)+"&token="+new Date().getTime()+"_"+getUrlParameter("host")+"&host="+getUrlParameter("host")+"&name=Offset+"+getUrlParameter("host"),function(data) {
location.href=data;
})
}
if(getUrlParameter("host")) {
$('.entity').html(getUrlParameter("host"))
$('.entity-strong').html('<strong>'+getUrlParameter("host")+"</strong>");
$.getJSON("https://api.corrently.io/v2.0/ghgmanage/statusimg?host="+getUrlParameter("host")+"&stats=only",function(data) {
let freedays = 30;
let ts = new Date().getTime();
data.credited *= 1;
if(data.credited > ts) {
ts = data.credited
} else {
if( data.credited > (ts - (86400000 * freedays))) {
ts = data.credited + (86400000 * freedays);
} else {
if(data.debit < freedays) {
ts += (86400000 * (freedays-data.debit));
} else {
ts = data.credited
}
}
}
$('.sponsor').html(data.sponsor);
$('.neutralstrong').html("<strong>"+new Date(ts).toLocaleDateString()+"</strong>");
updateCurrency();
});
}
$('.form-check-input').click(updateCurrency);
$('.form-check-input').change(updateCurrency);
$('#amount').change(updateCurrency);
$('#checkout').click(checkout);
$('#gencode').change(updateCode);
$('#btnCode').click(updateCode);
});