Skip to content

Instantly share code, notes, and snippets.

@Meiryo7743
Created May 1, 2021 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Meiryo7743/2958ef7c0ffa3d2a502284edb02c9f5b to your computer and use it in GitHub Desktop.
Save Meiryo7743/2958ef7c0ffa3d2a502284edb02c9f5b to your computer and use it in GitHub Desktop.
A web counter implemented with Google Apps Script.
const config = () => {
const ALLOW_LIST = ["https://example.com"];
const scriptProps = PropertiesService.getScriptProperties();
scriptProps.setProperty("ALLOW_LIST", JSON.stringify(ALLOW_LIST));
};
const doGet = (e) => {
const template = HtmlService.createTemplateFromFile("index.html");
template.webCounter = webCounter(e.parameter.url);
return template
.evaluate()
.addMetaTag("viewport", "width=device-width, initial-scale=1.0")
.setTitle("Web Counter")
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
};
<!DOCTYPE html>
<html>
<head>
<style>
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.web-counter {
font-family: monospace;
line-height: 1;
text-align: center;
}
</style>
</head>
<body>
<div class="web-counter"><?= webCounter ?></div>
</body>
</html>
const isUrl = (url) => {
const REGEXP = /^https?:\/\/([A-zd-]+.)+[A-z]+\/?(.+)*/g;
return typeof url === "string" && REGEXP.test(url);
};
const isKiriban = (n) => {
const REGEXP = /^(\d)\1+$|^\d0+$/g;
return typeof n === "string" && REGEXP.test(n);
};
const count = (n) => {
return typeof n === "string" ? `${Number(n) + 1}` : "1";
};
const webCounter = (url) => {
const scriptProps = PropertiesService.getScriptProperties();
const allowList = scriptProps.getProperty("ALLOW_LIST");
if (isUrl(url) && allowList.includes(url)) {
const result = count(scriptProps.getProperty(url));
scriptProps.setProperty(url, result);
return isKiriban(result) ? `☆${result}☆` : result;
} else {
return Math.PI.toString();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment