「Google Apps Script でアクセスカウンターを作る」中に登場するソースコードのコメントなし版です。
Created
May 1, 2021 12:22
-
-
Save Meiryo7743/2958ef7c0ffa3d2a502284edb02c9f5b to your computer and use it in GitHub Desktop.
A web counter implemented with Google Apps Script.
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
const config = () => { | |
const ALLOW_LIST = ["https://example.com"]; | |
const scriptProps = PropertiesService.getScriptProperties(); | |
scriptProps.setProperty("ALLOW_LIST", JSON.stringify(ALLOW_LIST)); | |
}; |
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
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); | |
}; |
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
<!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> |
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
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