Templater 用户函数记录

使用说明:

  • 安装 Templater
  • 在设置中设定好模板文件夹和脚本文件夹
  • 在模板中用 <% tp.user.脚本文件名(参数) %> 进行调用,其中参数如果没有可以省略,如果参数为字符串,只能用双引号,用单引号会报错

获取今天的日期

Scripts/get_date.js

function get_date () {
  const today = new Date()
  const num = ['日', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  return (
    today.toLocaleDateString('zh-CN')
    +' '+
    today.toLocaleDateString('zh-CN-u-ca-chinese')
    .replace(/(\d+)\s*?年\s*/, '')
    .replace(/月(\d+)/, (m, s)=>{
      s = Number(s)
      if(s===20) return '月 二十'
      if(s===30) return '月 三十'
      
      const lastNum = num[s%10]
      if(s<10) return '月 初'+lastNum
      if(s<20) return '月 十'+lastNum
      if(s<30) return '月 廿'+lastNum
    })
    +' '+
    '星期'+num[today.getDay()]
  )
}
module.exports = get_date;

模板代码:<% tp.user.get_date() %>

执行后获得:2022/10/10 九月 十五 星期一

获取今天的天气

Scripts/get_weather.js

async function get_weather (city) {
  const response = await fetch("https://wttr.in/"+city+"?Tm2&lang=zh-cn&format=天气:%c %C 气温:%t 风力:%w  \\n月相:%m 日出时间:%S 日落时间:%s")
  const data = await response.text()
  return data
}
module.exports = get_weather;

模板代码:<% tp.user.get_weather("beijing") %>

注意:参数为地级市的拼音

执行后获得:

天气:☀️ 晴 气温:+17°C 风力:↘16km/h
月相:🌕 日出时间:06:12:03 日落时间:17:36:08

历史上的今天

Scripts/get_today_history.js

async function get_today_history () {
  const response = await fetch("https://api.oick.cn/lishi/api.php")
  const data = await response.json()
  let result = ''
  data.result.map(e=> result += '- '+e.date+' '+e.title+'\n')
  return result
}
module.exports = get_today_history;

模板代码:<% tp.user.get_today_history() %>

执行后获得:

  • 1731年10月10日 英国化学家和物理学家卡文迪许出生
  • 1813年10月10日 意大利作曲家朱塞佩·威尔第出生
  • 1846年10月10日 海王星的卫星海卫一被发现
  • 1872年10月10日 美国国务卿威廉·亨利·西华德逝世
  • 1895年10月10日 中国现代诗人萧三出生
  • 1895年10月10日 中国著名作家林语堂出生
  • 1903年10月10日 中国生物物理学的奠基人和开拓者贝时璋出生
  • 1911年10月10日 辛亥革命爆发
  • 1913年10月10日 世界七大工程奇迹之一的巴拿马运河开通
  • 1925年10月10日 故宫首次对外开放
  • 1980年10月10日 中国著名电影艺术家赵丹逝世
  • 1985年10月10日 古代印第安日晷遗迹被发现
  • 1992年10月10日 中国著名书法家沙孟海逝世
  • 2009年10月10日 爱尔兰男子乐团“男孩地带”成员斯蒂芬·盖特利度假时猝死
  • 2013年10月10日 中国最高人民法院原院长郑天翔逝世

年进度

Scripts/get_year_progress.js

function get_year_progress(length=20){
  const now = new Date()
  const year = now.getFullYear()
  const yearStart = new Date(year, 0)
  const yearEnd = new Date(year+1, 0)
  const progress = (now-yearStart)/(yearEnd-yearStart)
  const prefix = ''.padStart(Math.round(length*progress), '▓')
  const suffix = ''.padStart(length-Math.round(length*progress), '░')
  return 'Year progress: '+prefix+suffix+' '+(progress*100).toFixed(2)+'%'
}
module.exports = get_year_progress;

模板代码:<% tp.user.get_year_progress() %>

小括号中可以加数字参数来调整进度条总长度,默认是 20。

执行后获得:Year progress: ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░ 77.61%

一言

Scripts/get_sentence.js

async function get_sentence (tp) {
  const apis = {
    '毒鸡汤': 'https://api.oick.cn/dutang/api.php',
    '社会语录': 'https://api.oick.cn/yulu/api.php',
    '舔狗日记': 'https://api.oick.cn/dog/api.php',
    '一言': 'https://api.oick.cn/yiyan/api.php',
  }
  const type = await tp.system.suggester(Object.keys(apis), Object.keys(apis), false, '一言')
  const url = type && apis[type] ? apis[type] : apis['一言']
  const response = await fetch(url)
  const data = await response.text()
  return data
}
module.exports = get_sentence;

模板代码:<% tp.user.get_sentence(tp) %>

这个是交互式的,不知道有没有办法用在自动化里

执行后获得:"再也沒有任何事情,比晚睡更快樂了,除了晚起。"