1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee
tools/hotband/html/weibo_hotband.html

333 lines
14 KiB
HTML
Raw Normal View History

2022-07-25 18:01:26 +08:00
<!DOCTYPE html>
2022-07-25 22:26:15 +08:00
<html lang="cn">
2022-07-25 18:01:26 +08:00
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>微博热搜</title>
<link rel="stylesheet" href="./assets/css/main.css">
2022-07-25 18:01:26 +08:00
</head>
<body>
2022-07-25 22:26:15 +08:00
<div style="text-align: center;">
<h1>微博热搜榜</h1>
<hr>
<div>
2022-07-26 13:40:19 +08:00
显示字段:
<label for="show_emoticon">
<input type="checkbox" class="filter_checkbox" name="show_emoticon" id="show_emoticon"
detailed-checked="true">热搜表情
</label>
<label for="show_num">
<input type="checkbox" class="filter_checkbox" name="show_num" id="show_num" checked="true"
concise-checked="true" detailed-checked="true">热度
</label>
<label for="show_category">
<input type="checkbox" class="filter_checkbox" name="show_category" id="show_category" checked="true"
detailed-checked="true">分类
</label>
<label for="show_onboard_time">
<input type="checkbox" class="filter_checkbox" name="show_onboard_time" id="show_onboard_time"
checked="true" detailed-checked="true">上线时间
</label>
<label for="show_is_new">
<input type="checkbox" class="filter_checkbox" name="show_is_new" id="show_is_new"
detailed-checked="true">是否新热搜
</label>
2022-07-25 22:26:15 +08:00
<label for="show_detail">
2022-07-26 13:40:19 +08:00
<input type="checkbox" class="filter_checkbox" name="show_detail" id="show_detail"
detailed-checked="true">热搜详情
2022-07-25 22:26:15 +08:00
</label>
<label for="show_mid">
2022-07-26 13:40:19 +08:00
<input type="checkbox" class="filter_checkbox" name="show_mid" id="show_mid">mid
2022-07-25 22:26:15 +08:00
</label>
2022-07-26 13:40:19 +08:00
<br>
<button id="btn_show_all">全选</button>
<button id="btn_show_none">全不选</button>
|
<button id="btn_show_concise">简洁</button>
<button id="btn_show_default">普通</button>
<button id="btn_show_detailed">详细</button>
2022-07-25 22:26:15 +08:00
</div>
</div>
<p id="latestUpdateTime" style="font-size: 12px; display: inline-block; vertical-align: middle;"></p>
<nobr>
<button id="btn_refresh">重新拉取</button>
</nobr>
<nobr>
<label for="auto_refresh">
2022-07-28 23:13:21 +08:00
<input type="checkbox" name="auto_refresh" id="auto_refresh">自动拉取<span id="auto_refresh_countdown"></span>
</label>
</nobr>
<nobr>
<span id="update-finish-info" style="color: green; font-weight: bold; display: none;">拉取成功,数据已更新</span>
</nobr>
2022-07-25 18:01:26 +08:00
<table id="list"></table>
2022-07-29 20:33:57 +08:00
<div class="bottom-placeholder">
2022-07-29 21:49:38 +08:00
<p>
— 到底啦 —<br>
数据来源: https://weibo.com/hot/search
</p>
2022-07-29 20:33:57 +08:00
</div>
2022-07-25 18:01:26 +08:00
<script>
2022-07-25 22:26:15 +08:00
/**
* 全局变量
*/
// 拉取下来的数据
let hotBandData;
2022-07-26 13:40:19 +08:00
// 按钮
const btnShowAll = document.getElementById('btn_show_all');
const btnShowNone = document.getElementById('btn_show_none');
const btnShowConcise = document.getElementById('btn_show_concise');
const btnShowDefault = document.getElementById('btn_show_default');
const btnShowDetailed = document.getElementById('btn_show_detailed');
2022-07-25 22:26:15 +08:00
const btnRefresh = document.getElementById('btn_refresh');
// 复选框
const filterCheckbox = document.getElementsByClassName("filter_checkbox");
2022-07-26 13:40:19 +08:00
const showEmoticon = document.getElementById("show_emoticon");
const showNum = document.getElementById("show_num");
const showCategory = document.getElementById("show_category");
const showOnboardTime = document.getElementById("show_onboard_time");
const showIsNew = document.getElementById("show_is_new");
2022-07-25 22:26:15 +08:00
const showDetail = document.getElementById("show_detail");
const showMid = document.getElementById("show_mid");
2022-07-26 14:00:15 +08:00
const autoRefresh = document.getElementById("auto_refresh");
2022-07-25 22:26:15 +08:00
// 绑定按钮点击事件
2022-07-26 13:40:19 +08:00
btnShowAll.addEventListener('click', function () {
for (let i = 0; i < filterCheckbox.length; i++) {
const element = filterCheckbox[i];
element.checked = true;
}
render();
});
btnShowNone.addEventListener('click', function () {
for (let i = 0; i < filterCheckbox.length; i++) {
const element = filterCheckbox[i];
element.checked = false;
}
render();
});
btnShowConcise.addEventListener('click', function () {
for (let i = 0; i < filterCheckbox.length; i++) {
const element = filterCheckbox[i];
element.checked = element.getAttribute('concise-checked') === 'true';
}
render();
});
btnShowDefault.addEventListener('click', function () {
for (let i = 0; i < filterCheckbox.length; i++) {
const element = filterCheckbox[i];
element.checked = element.getAttribute('checked') === 'true';
}
render();
});
btnShowDetailed.addEventListener('click', function () {
for (let i = 0; i < filterCheckbox.length; i++) {
const element = filterCheckbox[i];
element.checked = element.getAttribute('detailed-checked') === 'true';
}
render();
});
2022-07-25 22:26:15 +08:00
btnRefresh.onclick = function () {
getData();
document.getElementById("update-finish-info").style.display = "";
2022-07-26 14:00:15 +08:00
// btnRefresh.style.display = "none";
btnRefresh.style.visibility = "hidden";
2022-07-25 22:26:15 +08:00
setTimeout(function () {
document.getElementById("update-finish-info").style.display = "none";
2022-07-26 14:00:15 +08:00
// btnRefresh.style.display = "";
btnRefresh.style.visibility = "";
2022-07-25 22:26:15 +08:00
}, 1000);
2022-07-25 18:01:26 +08:00
};
2022-07-25 22:26:15 +08:00
// 绑定复选框改变事件
for (let i = 0; i < filterCheckbox.length; i++) {
// console.log(filterCheckbox[i]);
2022-07-25 22:26:15 +08:00
filterCheckbox[i].onchange = function () {
2022-07-26 13:40:19 +08:00
render();
2022-07-25 22:26:15 +08:00
};
}
2022-07-26 14:00:15 +08:00
let autoRefreshIntreval = null;
const autoRefreshCountDownElement = document.getElementById('auto_refresh_countdown');
const countDown = 20; // 自动拉取间隔时间,单位:秒
let autoRefreshCountDown = countDown;
autoRefresh.onchange = function () {
if (autoRefresh.checked) {
btnRefresh.style.display = "none";
btnRefresh.click();
autoRefreshIntreval = setInterval(function () {
if ((--autoRefreshCountDown) > 0) {
autoRefreshCountDownElement.innerHTML = `(${autoRefreshCountDown}s)`;
} else {
autoRefreshCountDown = countDown;
btnRefresh.click();
autoRefreshCountDownElement.innerHTML = ``;
}
}, 1000);
} else {
clearInterval(autoRefreshIntreval);
btnRefresh.style.display = "";
autoRefreshCountDownElement.innerHTML = ``;
}
};
2022-07-26 13:40:19 +08:00
// 根据屏幕判断要显示哪些字段
// 此时还未拉取数据,所以进入 render 函数会直接返回,不会多次渲染
let initWidth = document.body.offsetWidth;
// console.log(initWidth);
/* if (initWidth < 400) {
2022-07-26 13:40:19 +08:00
btnShowNone.click();
btnShowNone.innerHTML += "(默认)";
} else */ if (initWidth < 600) {
2022-07-26 13:40:19 +08:00
btnShowConcise.click();
btnShowConcise.innerHTML += "(默认)";
} else if (initWidth < 1900) {
btnShowDefault.click();
btnShowDefault.innerHTML += "(默认)";
} else {
btnShowDetailed.click();
btnShowDetailed.innerHTML += "(默认)";
}
2022-07-25 22:26:15 +08:00
// 网页加载后加载榜单
getData();
// 定时刷新
// setInterval(getData, 10 * 1000);
2022-07-25 18:01:26 +08:00
function getData() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "../data/weibo-hotband/latest.json?t=" + Date.now(), true);
2022-07-25 18:01:26 +08:00
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4) return;
if (xhr.status == 200) {
try {
hotBandData = JSON.parse(xhr.responseText);
if (!hotBandData.data || typeof hotBandData.data !== 'object')
throw new Error("data is undefined or not an object");
} catch (e) {
console.error("[error]", "\n", e, "\n", "\n", "[xhr.responseText]", "\n", xhr.responseText);
alert("latest.json 文件解析失败,请检查文件");
return;
}
2022-07-25 22:26:15 +08:00
console.log(hotBandData);
2022-07-26 13:40:19 +08:00
// 更新时间
document.getElementById("latestUpdateTime").innerHTML =
"数据拉取时间:" + new Date().toLocaleString() + "<br/>" +
"热榜更新时间:" + new Date(hotBandData.update_time).toLocaleString();
// 渲染榜单
render();
} else if (xhr.status == 404) {
alert("data 目录下未找到 latest.json 文件,可能的原因:\n您还没有运行脚本拉取数据请先运行脚本然后刷新页面");
2022-07-25 18:01:26 +08:00
}
}
}
2022-07-25 22:26:15 +08:00
2022-07-26 13:40:19 +08:00
function render() {
if (!hotBandData) return;
2022-07-25 22:26:15 +08:00
/**
* 渲染热搜列表
*/
let hotBandList = hotBandData.data;
var str = [];
// 渲染表格
str.push(`<thead>
<tr class="thead" style="top: 0; background-color: white; position: sticky;">
<td>编号</td>
<td>热搜</td>
2022-07-26 13:40:19 +08:00
${showEmoticon.checked ? "<td>表情</td>" : ""}
${showNum.checked ? '<td>热度<br/><span style="font-size: 10px;">(展示/真实)</span></td>' : ""}
${showCategory.checked ? "<td>分类</td>" : ""}
${showOnboardTime.checked ? "<td>上线时间</td>" : ""}
${showIsNew.checked ? "<td>是否新热搜</td>" : ""}
2022-07-25 22:26:15 +08:00
${showDetail.checked ? "<td>热搜详情</td>" : ""}
${showMid.checked ? "<td>mid</td>" : ""}
</tr>
</thead>`);
str.push(`<tbody>`);
for (var i = 0; i < hotBandList.length; i++) {
const hotBand = hotBandList[i];
let hotDelta = hotBand.num - hotBand.raw_hot;
str.push(`<tr>
<!-- 编号 -->
<td>${i + 1}</td>
<!-- 热搜 -->
<td style="text-align: left; font-size: 14px;">
<nobr>
<div style="min-width: 20px; display: inline-block;">
<span class="hotband-label" style="background-color: ${hotBand.more.icon_desc_color};">${hotBand.label_name}</span>
</div>
<a href="${hotBand.url}" target="_blank">${hotBand.word}</a>
</nobr>
</td>
2022-07-26 13:40:19 +08:00
${showEmoticon.checked ? `
2022-07-25 22:26:15 +08:00
<!-- 表情 -->
<td>${hotBand.emoticon}</td>
2022-07-26 13:40:19 +08:00
` : ""}
2022-07-25 22:26:15 +08:00
2022-07-26 13:40:19 +08:00
${showNum.checked ? `
2022-07-25 22:26:15 +08:00
<!-- 热度 -->
<td style="line-height: 12px;">
<nobr><span style="font-size: 14px;">${hotDelta == 0 ? hotBand.num : `${hotBand.num} / ${hotBand.raw_hot}`}</span></nobr><br/>
<nobr>
<span style="font-size: 10px; color: ${hotDelta > 0 ? "red" : "green"}; font-weight: bold;">
${hotDelta != 0 ? `(官方调控 ${hotDelta > 0 ? "+" : ""}${hotDelta})` : ""}
</span>
</nobr>
</td>
2022-07-26 13:40:19 +08:00
` : ""}
2022-07-25 22:26:15 +08:00
2022-07-26 13:40:19 +08:00
${showCategory.checked ? `
2022-07-25 22:26:15 +08:00
<!-- 分类 -->
<td style="font-size: 10px;">${hotBand.category.map((c) => `<nobr>${c}</nobr>`).join('')}</td>
2022-07-26 13:40:19 +08:00
` : ""}
2022-07-25 22:26:15 +08:00
2022-07-26 13:40:19 +08:00
${showOnboardTime.checked ? `
2022-07-25 22:26:15 +08:00
<!-- 热搜上线时间 -->
<td style="font-size: 10px;">${new Date(hotBand.onboard_time * 1000).toLocaleString()}</td>
2022-07-26 13:40:19 +08:00
` : ""}
2022-07-25 22:26:15 +08:00
2022-07-26 13:40:19 +08:00
${showIsNew.checked ? `
2022-07-25 22:26:15 +08:00
<!-- 是否新热搜 -->
<td>${hotBand.more.is_new == 1 ? "是" : ""}</td>
2022-07-26 13:40:19 +08:00
` : ""}
2022-07-25 22:26:15 +08:00
${showDetail.checked ? `
<!-- 热搜详情 -->
<td>
<div style="font-size:10px; max-width: 300px; display: inline-block;">${hotBand.more.detail}</div>
</td>
` : ""}
${showMid.checked ? `
<!-- mid -->
<td style="font-size: 14px;">${hotBand.more.mid}</td>
` : ""}
</tr >`);
}
str.push(`</tbody>`);
document.getElementById('list').innerHTML = str.join('');
}
2022-07-25 18:01:26 +08:00
</script>
</body>
</html>