跳到主要内容

使用示例

(https://hitokoto-api.jerry.ink/) (从7种分类中随机抽取) (https://hitokoto-api.jerry.ink/?c=b) (请求获得一个分类是漫画的句子) (https://hitokoto-api.jerry.ink/?c=f&encode=text) (请求获得一个来自网络的句子,并以纯文本格式输出)

网页

演示

请注意:
本例所有用法均使用 ES6 语法。如果您想考虑兼容非现代浏览器(如:IE),需要您自行转换。

我们假设您的网页中存在一个块级元素用于显示一言的文本内容,且我们想让它能跳转到一言的指定页面用于后续的收藏、反馈。

<!-- 请注意,以下的示例包含超链接,您可能需要手动配置样式使其不变色。如果您嫌麻烦,可以移除。 -->
<p id="hitokoto">
<a href="#" id="hitokoto_text">:D 获取中...</a>
</p>

那我们可以在 <script></script> 中 或者 .js 文件中使用我们的接口:

https://docs.jerry.ink/
<!-- 本例不能添加链接内容,放在此处只是因为此接口比较方便,也许能够解决大部分的需求-->
<script src="https://hitokoto-api.jerry.ink/?encode=js&select=%23hitokoto" defer></script>

我们之所以推荐基于 Promise 的 API,是因为自 ES2017(ES8) 引入 async/await 后,您可以类似这样使用:

async function fetchHitokoto() {
const response = await fetch('https://hitokoto-api.jerry.ink/')
const { uuid, hitokoto: hitokotoText } = await response.json()
const hitokoto = document.querySelector('#hitokoto_text')
hitokoto.href = `https://hitokoto-api.jerry.ink/?uuid=${uuid}`
hitokoto.innerText = hitokotoText
}

这样是不是更符合其他语言的同步使用的直觉?

完整示例(以 Fetch API 为例)

<p id="hitokoto">
<a href="#" id="hitokoto_text">:D 获取中...</a>
</p>
<script>
fetch('https://hitokoto-api.jerry.ink/')
.then(response => response.json())
.then(data => {
const hitokoto = document.querySelector('#hitokoto_text')
hitokoto.href = `https://hitokoto-api.jerry.ink/?uuid=${data.uuid}`
hitokoto.innerText = data.hitokoto
})
.catch(console.error)
</script>

插件:网易云的使用

警告

该插件未启用

JavaScript

// 本示例需要浏览器支持 Promise,fetch 以及 ES6 语法。
function fetch163Playlist(playlistId) {
return new Promise((ok, err) => {
fetch(`https://hitokoto-api.jerry.ink/nm/playlist/${playlistId}`)
.then(response => response.json())
.then(data => {
const arr = [];
data.playlist.trackIds.forEach(function (value) { // 参考:https://neteasecloudmusicapi.vercel.app/#/?id=%e8%8e%b7%e5%8f%96%e6%ad%8c%e5%8d%95%e8%af%a6%e6%83%85
arr.push(value.id);
});
return arr;
})
.then(fetch163Songs)
.then(ok)
.catch(err);
});
}

function fetch163Songs(Ids) {
return new Promise(function (ok, err) {
let ids;
switch (typeof Ids) {
case 'number':
ids = [Ids];
break;
case 'object':
if (!Array.isArray(Ids)) {
err(new Error('Please enter array or number'));
return;
}
ids = Ids;
break;
default:
err(new Error('Please enter array or number'));
return;
break;
}
fetch(`https://hitokoto-api.jerry.ink/nm/summary/${ids.join(',')}?lyric=true&common=true`)
.then(response => response.json())
.then(data => {
var songs = [];
data.songs.forEach(function (song) {
songs.push({
name: song.name,
url: song.url,
artist: song.artists.join('/'),
album: song.album.name,
pic: song.album.picture,
lrc: song.lyric
});
});
return songs;
})
.then(ok)
.catch(err);
});
}

// 使用测试
fetch163Playlist(2158283120)
.then(console.log)
.catch(console.error);

fetch163Songs([28391863, 22640061])
.then(console.log)
.catch(console.error);