PoetryTab 相关API 及代码
【摘要】
PoetryTab 相关API 及代码
manifest.json
{ "manifest_version": 2, "name": "PoetryTab", "version": "2.0", "description": "PoetryTab:为你的浏览增加诗意", "author": ...
PoetryTab 相关API 及代码
manifest.json
-
{
-
"manifest_version": 2,
-
"name": "PoetryTab",
-
"version": "2.0",
-
"description": "PoetryTab:为你的浏览增加诗意",
-
"author": "Fizz",
-
"icons": {
-
"16": "img/logo.png",
-
"48": "img/logo.png",
-
"128": "img/logo.png"
-
},
-
"chrome_url_overrides": {
-
"newtab": "PoetryTab.html"
-
}
-
}
PoetryTab.html
-
<!DOCTYPE html>
-
<html>
-
<head>
-
<meta charset="utf-8"/>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
-
<title>你问我最美的诗是什么 我不知道 我只是想起了你</title>
-
<link rel="stylesheet" href="css/index.css" />
-
</head>
-
<body>
-
<div class="container" id="main-container">
-
<h1 class="poetry-title" id="poetry-title">独坐敬亭山</h1>
-
<div id="poetry-content-wrap">
-
<p class="poetry-content">众鸟高飞尽,孤云独去闲。</p>
-
<p class="poetry-content">相看两不厌,只有敬亭山。</p>
-
</div>
-
</div>
-
<script src="./js/index.js"></script>
-
</body>
-
</html>
index.js
-
// for Poetry Js @Fizz
-
-
// 封装一个chrome专用ajax
-
function ajax(config) {
-
let xhr = new XMLHttpRequest()
-
xhr.open(config.methods, config.url)
-
if (config.methods === 'POST') {
-
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-
}
-
if (config.methods === 'GET') {
-
xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
-
// for config.data
-
}
-
xhr.send()
-
xhr.onreadystatechange = function () {
-
console.log(xhr.responseText)
-
if (xhr.readyState === 4 && xhr.status === 200) {
-
config.successCb(xhr.responseText)
-
} else {
-
// console.log('xhr error', xhr.responseText)
-
}
-
}
-
}
-
-
// 请求诗数据
-
function loadPoetry() {
-
let sendData = {
-
url: 'http://localhost:3000/poetry/getOnePoetry',
-
methods: 'GET',
-
successCb: function (res) {
-
let poetryData = JSON.parse(res).data
-
document.querySelector("#poetry-title").innerText = poetryData.title
-
let contentHtml = []
-
for (let i = 0; i < poetryData.content.length; i++) {
-
let item = poetryData.content[i]
-
contentHtml.push(`<p class="poetry-content">${item}</p>`)
-
}
-
document.querySelector("#poetry-content-wrap").innerHTML = contentHtml.join('')
-
}
-
}
-
ajax(sendData)
-
}
-
loadPoetry()
效果图
欢迎在下方留言 与我讨论
文章来源: fizzz.blog.csdn.net,作者:拿我格子衫来,版权归原作者所有,如需转载,请联系作者。
原文链接:fizzz.blog.csdn.net/article/details/89712499
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)