{"body":{"post":{"id":"2r3o63326w0m736z5h01","createdAt":"2022-10-10T10:16:29.150Z","updatedAt":"2022-10-12T01:33:19.371Z","title":"Templater 用户函数记录","content":"---\ncreated: 2022-10-11 10:02:59\nupdated: 2022-10-12 09:29:41\ntags:\n - Note/Note\n - Note/Obsidian\n - Note/Snippet\n - Published\n---\n\n## 使用说明：\n\n- 安装 [Templater](https://github.com/SilentVoid13/Templater)\n- 在设置中设定好模板文件夹和脚本文件夹\n- 在模板中用 `<% tp.user.脚本文件名(参数) %>` 进行调用，其中参数如果没有可以省略，如果参数为字符串，只能用双引号，用单引号会报错\n\n## 获取今天的日期\n\n> Scripts/get_date.js\n\n```js\nfunction get_date () {\n  const today = new Date()\n  const num = ['日', '一', '二', '三', '四', '五', '六', '七', '八', '九']\n  return (\n    today.toLocaleDateString('zh-CN')\n    +' '+\n    today.toLocaleDateString('zh-CN-u-ca-chinese')\n    .replace(/(\\d+)\\s*?年\\s*/, '')\n    .replace(/月(\\d+)/, (m, s)=>{\n      s = Number(s)\n      if(s===20) return '月 二十'\n      if(s===30) return '月 三十'\n      \n      const lastNum = num[s%10]\n      if(s<10) return '月 初'+lastNum\n      if(s<20) return '月 十'+lastNum\n      if(s<30) return '月 廿'+lastNum\n    })\n    +' '+\n    '星期'+num[today.getDay()]\n  )\n}\nmodule.exports = get_date;\n```\n\n模板代码：`<% tp.user.get_date() %>`\n\n执行后获得：2022/10/10 九月 十五 星期一\n\n## 获取今天的天气\n\n> Scripts/get_weather.js\n\n```js\nasync function get_weather (city) {\n  const response = await fetch(\"https://wttr.in/\"+city+\"?Tm2&lang=zh-cn&format=天气：%c %C 气温：%t 风力：%w  \\\\n月相：%m 日出时间：%S 日落时间：%s\")\n  const data = await response.text()\n  return data\n}\nmodule.exports = get_weather;\n```\n\n模板代码：`<% tp.user.get_weather(\"beijing\") %>`\n\n注意：参数为地级市的拼音\n\n执行后获得：\n\n天气：☀️   晴 气温：+17°C 风力：↘16km/h  \n月相：🌕 日出时间：06:12:03 日落时间：17:36:08\n\n## 历史上的今天\n\n> Scripts/get_today_history.js\n\n```js\nasync function get_today_history () {\n  const response = await fetch(\"https://api.oick.cn/lishi/api.php\")\n  const data = await response.json()\n  let result = ''\n  data.result.map(e=> result += '- '+e.date+' '+e.title+'\\n')\n  return result\n}\nmodule.exports = get_today_history;\n```\n\n模板代码：`<% tp.user.get_today_history() %>`\n\n执行后获得：\n\n- 1731年10月10日 英国化学家和物理学家卡文迪许出生\n- 1813年10月10日 意大利作曲家朱塞佩·威尔第出生\n- 1846年10月10日 海王星的卫星海卫一被发现\n- 1872年10月10日 美国国务卿威廉·亨利·西华德逝世\n- 1895年10月10日 中国现代诗人萧三出生\n- 1895年10月10日 中国著名作家林语堂出生\n- 1903年10月10日 中国生物物理学的奠基人和开拓者贝时璋出生\n- 1911年10月10日 辛亥革命爆发\n- 1913年10月10日 世界七大工程奇迹之一的巴拿马运河开通\n- 1925年10月10日 故宫首次对外开放\n- 1980年10月10日 中国著名电影艺术家赵丹逝世\n- 1985年10月10日 古代印第安日晷遗迹被发现\n- 1992年10月10日 中国著名书法家沙孟海逝世\n- 2009年10月10日 爱尔兰男子乐团“男孩地带”成员斯蒂芬·盖特利度假时猝死\n- 2013年10月10日 中国最高人民法院原院长郑天翔逝世\n\n## 年进度\n\n> Scripts/get_year_progress.js\n\n```js\nfunction get_year_progress(length=20){\n  const now = new Date()\n  const year = now.getFullYear()\n  const yearStart = new Date(year, 0)\n  const yearEnd = new Date(year+1, 0)\n  const progress = (now-yearStart)/(yearEnd-yearStart)\n  const prefix = ''.padStart(Math.round(length*progress), '▓')\n  const suffix = ''.padStart(length-Math.round(length*progress), '░')\n  return 'Year progress: '+prefix+suffix+' '+(progress*100).toFixed(2)+'%'\n}\nmodule.exports = get_year_progress;\n```\n\n模板代码：`<% tp.user.get_year_progress() %>`\n\n小括号中可以加数字参数来调整进度条总长度，默认是 20。\n\n执行后获得：Year progress: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░ 77.61%\n\n## 一言\n\n> Scripts/get_sentence.js\n\n```js\nasync function get_sentence (tp) {\n  const apis = {\n    '毒鸡汤': 'https://api.oick.cn/dutang/api.php',\n    '社会语录': 'https://api.oick.cn/yulu/api.php',\n    '舔狗日记': 'https://api.oick.cn/dog/api.php',\n    '一言': 'https://api.oick.cn/yiyan/api.php',\n  }\n  const type = await tp.system.suggester(Object.keys(apis), Object.keys(apis), false, '一言')\n  const url = type && apis[type] ? apis[type] : apis['一言']\n  const response = await fetch(url)\n  const data = await response.text()\n  return data\n}\nmodule.exports = get_sentence;\n```\n\n模板代码：`<% tp.user.get_sentence(tp) %>`\n\n这个是交互式的，不知道有没有办法用在自动化里\n\n执行后获得：\"再也沒有任何事情，比晚睡更快樂了，除了晚起。\""}}}