先放一个 《油猴脚本 之 网教通直播评论记录抓取》那篇文章的传送门 。
修复内容
- 将所有表情转为
[符号表情]
字样,而非删除; - 修复被禁言用户读取异常,现在被禁言用户表示为
张三 [已禁言]
; - 修复被主播发言读取异常,现在主播发言表示为
张三 [主播]
; - 在导出的文件名后添上导出时间;
- 添加表头;
- 汉化所有弹窗;
- 保存文件时可以取消;
- 支持在到处之后按 Esc 键重新导出;
- 实现在加载完所有评论时,自动弹窗询问是否保存;
- 将评论中所有换行符全部转为
[换行]
,保证可读性的同时防止格式错乱; - 过滤
noscript
标签,防止格式错乱; - 若评论区无内容,弹窗提示。
源代码
// ==UserScript==
// @name 评论记录批量加载
// @namespace https://www.cnblogs.com/henrylin/
// @version 2.0
// @description 评论记录批量加载
// @author 林洪平
// @match https://recreation-chat.sdp.101.com/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
var x = false;
var timer = setInterval(function () {
if (document.querySelector('div._3Bl-4NTv1A._3bGFQ6biuP'))
document.querySelector('div._3Bl-4NTv1A._3bGFQ6biuP').click();
}, 100);
var stopper = setInterval(function () {
if (document.querySelector('._2PJO3f--Nd + span') && document.querySelector('._2PJO3f--Nd + span').innerHTML == '没有更多消息') {
clearInterval(timer);
if (confirm('已全部加载完成。是否导出?')) exporter(); clearInterval(stopper);
}
}, 300)
document.onkeydown = function (e) {
var ev = e || window.event;
if (ev.keyCode == 27) {//esc
if (x ? confirm('确定重新导出?') : confirm('确定停止记录并导出?')) {
exporter();
}
}
}
function exporter() {
x = true;
clearInterval(timer);
var data = document.getElementById('msg-flow-wrapper').innerHTML;
const linebreaker = '\n';
const attrbreaker = ',';
data = data.replace(/<img src="https:\/\/gcdncs\.101\.com\/v0\.1\/static\/im_emotion\/emoji[^>]+>/g, "[符号表情]");
data = data.replace(/<img src="http:\/\/gcdncs\.101\.com\/v0\.1\/static\/recreation_chat_assets\/smiley[^>]+>/g, "[符号表情]");
data = data.replace(/<span[^>]+>已禁言<\/span>/g, "\uFFFF\uFFFE\uFFFF");
data = data.replace(/<span[^>]+>主播<\/span>/g, "\uFFFF\uFFFD\uFFFF");
data = data.replace(/<img[^>]+>/g, "");
data = data.replace(/<\/?noscript[^>]*>/g, "");
data = data.replace(/<br[^>]+>/g, "[换行]");
data = data.replace(/<[^>]+>/g, "\uFFFF");
data = data.replace(/\uFFFF{19}/g, linebreaker);
data = data.replace(/\uFFFF{15}/g, linebreaker);
data = data.replace(/^\uFFFF[^\uFFFF]+\uFFFF{2}/, "");
data = data.replace(/\uFFFF{5}/g, attrbreaker);
data = data.replace(/\uFFFF{4}/g, attrbreaker);
data = data.replace(/\uFFFF/g, "");
data = data.replace(/^,{2}/, "");
data = data.replace(/,{2}$/, "");
data = data.replace(/\n,+/g, "\n");
data = data.replace(/\n+/g, "\n");
data = data.replace(/\uFFFE/g, " [已禁言],");
data = data.replace(/\uFFFD/g, " [主播],");
if (data == '欢迎来到直播间') {
alert('未抓取到任何内容。将不会导出文件。');
x = true;
return;
}
data = '发言者,时间,内容' + linebreaker + data;
var day = new Date();
var time = day.getHours() + '时' + day.getMinutes() + '分' + day.getSeconds() + '秒';
var date = day.getFullYear() + "年" + (day.getMonth() + 1) + "月" + day.getDate() + '日';
var element = document.createElement('a');
const blob = new Blob([data]);
element.download = '评论区记录_' + date + '_' + time + '.csv';
element.style = "display: none";
element.href = URL.createObjectURL(blob);
document.body.appendChild(element);
element.click();
setTimeout(function () {
document.body.removeChild(element);
window.URL.revokeObjectURL(blob);
}, 100);
}
})();