From 9570650589a2b641a64436045731b9f280cde16c Mon Sep 17 00:00:00 2001 From: Tai Zhou <21986946+Tai-Zhou@users.noreply.github.com> Date: Wed, 8 May 2024 14:30:20 +0800 Subject: [PATCH] add idSet and update scroll button --- CHANGELOG.md | 15 ++++++- package.json | 9 ++++- src/extension.ts | 103 +++++++++++++++++++++++++++-------------------- 3 files changed, 80 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8563a1b..7983cb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,19 @@ 3. 根据消息源屏蔽新闻 4. 评论翻页功能 +## [1.9.4] - 2024-05-08 +### 新增 + +* 最新:隐藏广告文章开关 + +### 变动 + +* 查看内容:回到顶部按钮样式与 VS Code 统一 + +### 修正 + +* 最新:解决了因文章顺序变动导致 ID 冲突的问题 + ## [1.9.3] - 2024-05-07 ### 新增 @@ -83,7 +96,7 @@ ## [1.6.0] - 2022-07-18 ### 新增 -* 查看内容:文章内相关阅读、相关文章及评论中之家文章链接现可在 VSCode 内跳转 +* 查看内容:文章内相关阅读、相关文章及评论中之家文章链接现可在 VS Code 内跳转 ### 修正 diff --git a/package.json b/package.json index 32fa97e..c0a31b9 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "ith2ome", "displayName": "ITH2Ome", "description": "IT之家第三方插件,划水特供版", - "version": "1.9.3", + "version": "1.9.4", "author": "https://github.com/Tai-Zhou", "repository": { "type": "git", @@ -290,10 +290,15 @@ "default": true, "description": "“热评”是否显示点赞数。" }, + "ith2ome.hideAd": { + "type": "boolean", + "default": false, + "description": "“最新”列表是否隐藏广告文章。" + }, "ith2ome.hideAdTips": { "type": "boolean", "default": true, - "description": "查看内容是否隐藏广告声明" + "description": "查看内容是否隐藏广告声明。" } } } diff --git a/src/extension.ts b/src/extension.ts index eb14689..5be200c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -29,6 +29,7 @@ let blockWords: string[]; // 屏蔽词列表 let period: number; // “热榜”榜单,仅在启动时从设置中读取 const periodDic = ['48', 'weekhot', 'weekcomment', 'month']; // “热榜”榜单字典 let showThumbs: boolean; // “热评”显示点赞数 +let hideAd: boolean; // 文章列表隐藏广告 let hideAdTips: boolean; // 查看内容隐藏广告声明 let latestNewsId: number = 0; // “最新”最新消息标记,用于显示上次阅读位置 let lastReadId: number = 0; // “最新”最后阅读标记,用于显示上次阅读位置 @@ -66,12 +67,15 @@ function refreshConfig() { // 刷新设置,仅在手动刷新时运行 keysLength[i] = keyWords[i].length; blockWords = config.get('blockWords'); showThumbs = config.get('showThumbs'); + hideAd = config.get('hideAd'); hideAdTips = config.get('hideAdTips'); } -function show(title: string): boolean { // 返回是否显示该条新闻 - for (let i in blockWords) - if (title.search(RegExp(blockWords[i], 'i')) != -1) +function show(title: string, ad: boolean): boolean { // 返回是否显示该条新闻 + if (hideAd && ad) + return false; + for (let word of blockWords) + if (title.search(RegExp(word, 'i')) != -1) return false; return true; } @@ -114,12 +118,12 @@ function titleFormat(title: string): string { } function linkFormat(text: string): string { - let linkList = text.match(RegExp('${title}`); + let linkList = text.match(RegExp('${title}`); } return text; } @@ -223,6 +227,7 @@ class contentProvider implements vscode.TreeDataProvider { // 为 V update = new vscode.EventEmitter(); // 用于触发刷新 readonly onDidChangeTreeData = this.update.event; list: ith2omeItem[] = []; // 项目列表 + idSet: Set = new Set(); // 文章 ID 集合 mode: number; // 工作模式,0 为“通行证”,1 为“最新”,2 为“热榜”,3 为“热评” refreshTimer: NodeJS.Timeout | undefined; // 自动刷新计时器 @@ -233,8 +238,10 @@ class contentProvider implements vscode.TreeDataProvider { // 为 V refresh(refreshType: number = 0) { // 0 为手动刷新,1 为自动刷新,其余为加载更多的时间戳(仅用于“最新”) if (this.refreshTimer) // 若为手动刷新 clearTimeout(this.refreshTimer); // 清除下一次自动刷新计时器 - if (refreshType < 2) + if (refreshType < 2) { this.list = []; // 清除项目列表 + this.idSet.clear(); + } if (this.mode == 0) { // “通行证” if (userHash == '') { // Cookie 为空 this.list = [{ @@ -289,9 +296,11 @@ class contentProvider implements vscode.TreeDataProvider { // 为 V lastReadId = -lastReadId; superagent.get('https://api.ithome.com/json/newslist/news').end((err, res) => { let topList = res.body.toplist; - for (let i in topList) - if (show(topList[i].title)) - this.list.push(newsFormat(topList[i], 'pinned')); + for (let top of topList) + if (show(top.title, false)) { + this.idSet.add(top.newsid); + this.list.push(newsFormat(top, 'pinned')); + } let newsList = res.body.newslist; for (let i in newsList) { let orderTime = new Date(newsList[i].orderdate).getTime(); @@ -301,8 +310,10 @@ class contentProvider implements vscode.TreeDataProvider { // 为 V this.list.push(lastRead); lastReadId = -lastReadId; } - if (show(newsList[i].title)) + if (show(newsList[i].title, newsList[i].aid) && !this.idSet.has(newsList[i].newsid)) { + this.idSet.add(newsList[i].newsid); this.list.push(newsFormat(newsList[i], newsList[i].aid ? 'tag' : (newsList[i].v == '100' ? 'device-camera-video' : 'preview'))); + } } this.list.push({ label: '加载更多数据', @@ -315,13 +326,15 @@ class contentProvider implements vscode.TreeDataProvider { // 为 V this.list.pop(); superagent.get('https://m.ithome.com/api/news/newslistpageget?ot=' + refreshType).end((err, res) => { let newsList = res.body.Result; - for (let i in newsList) { - if (lastReadId > 0 && new Date(newsList[i].orderdate).getTime() <= lastReadId) { + for (let news of newsList) { + if (lastReadId > 0 && new Date(news.orderdate).getTime() <= lastReadId) { this.list.push(lastRead); lastReadId = -lastReadId; } - if (show(newsList[i].title)) - this.list.push(newsFormat(newsList[i], newsList[i].url.search('lapin') != -1 ? 'tag' : (newsList[i].v == '100' ? 'device-camera-video' : 'preview'))); + if (show(news.title, news.url.search('lapin') != -1) && !this.idSet.has(news.newsid)) { + this.idSet.add(news.newsid); + this.list.push(newsFormat(news, news.url.search('lapin') != -1 ? 'tag' : (news.v == '100' ? 'device-camera-video' : 'preview'))); + } } this.list.push({ label: '加载更多数据', @@ -336,28 +349,28 @@ class contentProvider implements vscode.TreeDataProvider { // 为 V } else if (this.mode == 2) { // “热榜” superagent.get('https://api.ithome.com/json/newslist/rank').end((err, res) => { let rankList = res.body['channel' + periodDic[period] + 'rank']; - for (let i in rankList) - this.list.push(newsFormat(rankList[i], 'flame')); + for (let rank of rankList) + this.list.push(newsFormat(rank, 'flame')); this.update.fire(); }); this.refreshTimer = setTimeout(() => { this.refresh(); }, 86400000); // 设置自动刷新时间 } else if (this.mode == 3) { // “热评” superagent.get('http://cmt.ithome.com/api/comment/hotcommentlist/').end((err, res) => { let commentList = res.body.content.commentlist; - for (let i in commentList) { - let time = new Date(commentList[i].Comment.T).toLocaleString('zh-CN'); - let locLength = commentList[i].Comment.Y.length; - let user = commentList[i].Comment.N + (locLength > 6 ? ` @ ${commentList[i].Comment.Y.substring(4, locLength - 2)}` : ''); + for (let comment of commentList) { + let time = new Date(comment.Comment.T).toLocaleString('zh-CN'); + let locLength = comment.Comment.Y.length; + let user = comment.Comment.N + (locLength > 6 ? ` @ ${comment.Comment.Y.substring(4, locLength - 2)}` : ''); this.list.push({ - label: (showThumbs ? `${commentList[i].Comment.S} | ` : '') + commentList[i].Comment.C.replace(RegExp('[\n]+', 'g'), ' '), + label: (showThumbs ? `${comment.Comment.S} | ` : '') + comment.Comment.C.replace(RegExp('[\n]+', 'g'), ' '), contextValue: 'ith2ome.article', iconPath: new vscode.ThemeIcon('thumbsup'), - id: 'comment' + commentList[i].Comment.Ci, + id: 'comment' + comment.Comment.Ci, description: time, - resourceUri: linkCheck(commentList[i].News.NewsLink), - tooltip: new vscode.MarkdownString(`*${commentList[i].Comment.C.replace(RegExp('[\n]+', 'g'), '*\n\n*')}*\n\n**${commentList[i].News.NewsTitle}**\n\n*${time}*\n\n${user}`), - command: { title: '查看内容', command: 'ith2ome.showContent', arguments: [commentList[i].News.NewsTitle, commentList[i].News.NewsId] }, - shareInfo: `${commentList[i].Comment.C}\n\n标题:${commentList[i].News.NewsTitle}\n时间:${time}\n用户:${user}\n` + resourceUri: linkCheck(comment.News.NewsLink), + tooltip: new vscode.MarkdownString(`*${comment.Comment.C.replace(RegExp('[\n]+', 'g'), '*\n\n*')}*\n\n**${comment.News.NewsTitle}**\n\n*${time}*\n\n${user}`), + command: { title: '查看内容', command: 'ith2ome.showContent', arguments: [comment.News.NewsTitle, comment.News.NewsId] }, + shareInfo: `${comment.Comment.C}\n\n标题:${comment.News.NewsTitle}\n时间:${time}\n用户:${user}\n` }); } this.update.fire(); @@ -433,18 +446,18 @@ export function activate(context: vscode.ExtensionContext) { } superagent.get('https://api.ithome.com/json/newscontent/' + id).end((errNews, resNews) => { // 获取新闻内容 panel!.title = titleFormat(resNews.body.title); - let BiliVideoList = resNews.body.detail.match(RegExp('', 'g')) ?? []; // 匹配B站视频 + for (let BiliVideo of BiliVideoList) { + let BVID = BiliVideo.match(RegExp('(?<=bvid=)[0-9a-z]+', 'i')); if (BVID) { - resNews.body.detail = resNews.body.detail.replace(BiliVideoList[i], ''); + resNews.body.detail = resNews.body.detail.replace(BiliVideo, ''); superagent.get('https://api.bilibili.com/x/web-interface/view?bvid=' + BVID).end((errBiliVideo, resBiliVideo) => { // 加载B站视频信息 let biliVideoData = resBiliVideo.body.data; panel!.webview.html = panel!.webview.html.replace(RegExp(`

哔哩哔哩视频:${biliVideoData.title}

` + (imageWidth > 0 ? `哔哩哔哩视频封面' : '/>') : '') + `
观看弹幕评论点赞投币收藏转发发布时间
${numberFormat(biliVideoData.stat.view)}${numberFormat(biliVideoData.stat.danmaku)}${numberFormat(biliVideoData.stat.reply)}${numberFormat(biliVideoData.stat.like)}${numberFormat(biliVideoData.stat.coin)}${numberFormat(biliVideoData.stat.favorite)}${numberFormat(biliVideoData.stat.share)}${new Date(biliVideoData.pubdate * 1000).toLocaleString('zh-CN')}
` + (imageWidth > 0 ? `

` : '
') + `${biliVideoData.owner.name}

${biliVideoData.desc}

`); }); } } - let weiboVideoList = resNews.body.detail.match(RegExp('', 'g')); // 匹配微博视频 + let weiboVideoList = resNews.body.detail.match(RegExp('', 'g')) ?? []; // 匹配微博视频 for (let i in weiboVideoList) { let weiboHref = weiboVideoList[i].match(RegExp('(?<=href=").*?(?=")'))[0]; if (weiboHref) { @@ -463,26 +476,28 @@ export function activate(context: vscode.ExtensionContext) { }); } } - let miaopaiVideoList = resNews.body.detail.match(RegExp('