Skip to content

Instantly share code, notes, and snippets.

@dmdboi
Created February 22, 2021 04:38
Show Gist options
  • Save dmdboi/5395205a31b4fbfad498450292bf27b6 to your computer and use it in GitHub Desktop.
Save dmdboi/5395205a31b4fbfad498450292bf27b6 to your computer and use it in GitHub Desktop.
Use this script to show a drinks stats on your website from Cawfee
//If you're reading this, say hi on Twitter @dmdboi
async function getData(url) {
// Default options are marked with *
const response = await fetch(url, {
method: "GET", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, *cors, same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
headers: {
"Content-Type": "application/json"
},
redirect: "follow", // manual, *follow, error
referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
})
let res = response.json()
return res; // parses JSON response into native JavaScript objects
}
(function getSummary(data) {
// your page initialization code here
// the DOM will be available here
var script_tag = document.getElementById("cawfee");
var user = script_tag.getAttribute("data-webhook");
var drink = script_tag.getAttribute("data-drink");
var time = script_tag.getAttribute("data-time")
getData(
`https://cawfee.dmdboi.me/api/stats?w=${user}&d=${drink}`
).then((data) => {
let counter = document.getElementById("counter")
counter.innerText = data.stats[time]
});
})();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!--Element must have id="cawfee" -->
<!--data-time options include "today", "week" and "total -->
<script type="text/javascript" id="cawfee" src="script.js" data-webhook="your-webhook" data-drink="drink" data-time="week"></script>
</head>
<body>
<!--Element must have id="counter" -->
<p id="counter">Drink</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment