序言:效果展示

去 jquery 版本

https://akilar.top/posts/4e39cf4a/

基础版本

3.0 模拟时钟小部件主要包含:

  • 日期(年、月、日、星期)

  • 天气(天气图标、温度)

  • 湿度(百分比)

  • 空气质量(US AQI)

  • 时间(当地时间、AM/PM)

  • 地理位置(IP 地址地理位置)

  • 每日访问上限:约 500 次

  • 每月访问上限:10000 次

预览地址:https://zfe.space

调用过猛可能会上限不显示。

相比1.0 版本,加快了访问速度并优化了加载效果。

相比2.0 版本,通过 vue 替换 jquery 的 dom 操作提升了性能。

解决了 date class 会导致相关文章的时间样式出错的 BUG。

资源包地址:https://github.com/Zfour/Butterfly-clock/releases/tag/3.01

资源包下载

内测 3.1 版本

3.1 内测模拟时钟小部件主要包含:

  • 日期(年、月、日、星期)

  • 天气(天气图标、温度)

  • 湿度(百分比)

  • 时间(当地时间、AM/PM)

  • 地理位置(相比 3.0 优化了 ip 信息定位的不准确问题)

  • ip 信息(替代了空气质量信息)

  • 每日访问上限:无限次

  • 相比 3.0 优化了加载动画和信息显示效果。

资源包地址:https://github.com/Zfour/Butterfly-clock/

资源包下载


步骤 1:申请 api key

基础版本

这里介绍下如何申请 AirVisual api key。

① 打开网站,注册帐号。

https://www.iqair.com/auth/login

② 登录后访问,”https://www.iqair.com/dashboard/api"

https://www.iqair.com/dashboard/api

③ 点击“+ NEW KEY”按钮。选择“Community”得到对应的 key。

内测 3.1 版本

① 打开网站https://openweathermap.org,注册帐号,验证邮箱。
② 打开https://home.openweathermap.org/api_keys得到对应的 key。


步骤 2:添加小部件 pug

基础 3.0 版本资源包下载
内测 3.1 版本资源包下载

将资源包中 pug 文件夹的 card_clock.pug 文件放入”\themes\butterfly\layout\includes\widget”目录下。

将资源包 pug 文件夹的 index.pug 文件放入”\themes\butterfly\layout\includes\widget”目录下替换。(适用于 Butterfly3.3 版本以上)

若你的版本较低或存在侧边栏魔改,请打开”\themes\butterfly\layout\includes\widget”路径下的”index.pug”

将 card_clock 代码添加至 card_author 代码块后。添加后的代码如下:

1
2
if theme.aside.card_clock.enable
!=partial('includes/widget/card_clock', {}, {cache:theme.fragment_cache})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#aside_content.aside_content
if theme.aside.card_author.enable
!=partial('includes/widget/card_author', {}, {cache:theme.fragment_cache})
if theme.aside.card_announcement.enable
!=partial('includes/widget/card_announcement', {}, {cache:theme.fragment_cache})
if theme.aside.card_clock.enable
!=partial('includes/widget/card_clock', {}, {cache:theme.fragment_cache})
.sticky_layout
if is_post()
if showToc
include ./card_post_toc.pug
if theme.aside.card_recent_post.enable
!=partial('includes/widget/card_recent_post', {}, {cache:theme.fragment_cache})
if theme.ad && theme.ad.aside
!=partial('includes/widget/card_ad', {}, {cache:theme.fragment_cache})
else
if theme.aside.card_recent_post.enable
!=partial('includes/widget/card_recent_post', {}, {cache:theme.fragment_cache})
if theme.ad && theme.ad.aside
!=partial('includes/widget/card_ad', {}, {cache:theme.fragment_cache})
if theme.newest_comments.enable
!=partial('includes/widget/card_newest_comment', {}, {cache:theme.fragment_cache})
if theme.aside.card_categories.enable
!=partial('includes/widget/card_categories', {}, {cache:theme.fragment_cache})
if theme.aside.card_tags.enable
!=partial('includes/widget/card_tags', {}, {cache:theme.fragment_cache})
if theme.aside.card_archives.enable
!=partial('includes/widget/card_archives', {}, {cache:theme.fragment_cache})
if theme.aside.card_webinfo.enable
!=partial('includes/widget/card_webinfo', {}, {cache:theme.fragment_cache})

打开”\themes\butterfly"路径下的”_config.yml”

搜索到”aside:”设置处

添加 clock 开关代码:


步骤 3:导入 CSS、JS、图片等资源文件

将下载包中的 clock 文件夹放入根目录的”source”文件夹下。

如果报错,请将文件夹中的 pug 文件夹删除!!!

基础版本

打开 clock 文件夹中的 clock.js。

1
2
let clockurl =
"https://api.airvisual.com/v2/nearest_city?key=888888888-8888-8888-8888-888888888888";

将该部分链接的 key 值(888888888-8888-8888-8888-888888888888)改为步骤一在 airvisual 官网申请的 key 值。

内测 3.1 版本

打开 clock 文件夹中的 clock.js。

1
const mykey = { weather: "888888888888888888888" };

将该部分链接的 key 值(888888888888888888888)改为步骤一在 openweathermap.org 官网申请的 key 值。


步骤 4:接入 CSS 与 JS

打开”\themes\butterfly"路径下的”_config.yml”

搜索到”inject:”设置处

添加以下代码:

1
2
3
4
5
6
7
inject:
head:
- <link rel="stylesheet" href="/clock/css/clock.css"/>

bottom:
- <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
- <script src="/clock/js/clock.js"></script>

butterfly 3.4 以上的版本请引入 jquery。
或参考https://akilar.top/posts/4e39cf4a/ 使用无 jquery 版本。

1
2
3
4
5
6
7
8
inject:
head:
- <link rel="stylesheet" href="/clock/css/clock.css"/>

bottom:
- <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
- <script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script>
- <script src="/clock/js/clock.js"></script>

总结

在本地进行开发时基础版建议关掉”card_clock: enable: true”选项,以免出现多次调用上限的问题。

3.1 内测版无限制。