-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
165 lines (138 loc) · 4.8 KB
/
extension.js
File metadata and controls
165 lines (138 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const vscode = require("vscode");
const axios = require("axios");
const xmlParser = require("fast-xml-parser");
const RSS_LINKS_CONFIG_KEY = 'allblog.rssLinks';
const RSS_LINK_REGEX = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;
// 记录每个 RSS 的最新文章链接
const latestArticleLinks = {};
// 定时检查 RSS 更新的时间间隔(毫秒)
const RSS_CHECK_INTERVAL = 3600000; // 1小时
async function activate(context) {
let rssLinks = context.globalState.get(RSS_LINKS_CONFIG_KEY, {});
async function parseArticles(link) {
const response = await axios.get(link);
const articles = xmlParser.parse(response.data).rss.channel.item;
return articles;
}
async function getArticleContent(link, customName) {
const articles = await parseArticles(link);
const articleList = articles.map((article, index) => ({
label: article.title,
description: article.description,
link: article.link,
index
}));
const selectedArticle = await vscode.window.showQuickPick(articleList, {
placeHolder: 'Select an article to read'
});
if (selectedArticle) {
const { link } = selectedArticle;
const heightInput = await vscode.window.showInputBox({
prompt: 'Enter the height of the window (in px)',
placeHolder: '500'
});
const webViewPanel = vscode.window.createWebviewPanel(
'blogWebView',
customName, // Use the custom name as the title
vscode.ViewColumn.One,
{
enableScripts: true,
retainContextWhenHidden: true
}
);
let height = parseInt(heightInput) || 500;
webViewPanel.webview.html = `
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<iframe src="${link}" width="100%" height="${height}px"></iframe>
</body>
</html>
`;
}
}
// 定时检查 RSS 更新
setInterval(async () => {
for (const rssLink in rssLinks) {
const response = await axios.get(rssLink);
const articles = xmlParser.parse(response.data).rss.channel.item;
// 获取当前 RSS 的最新文章链接
let latestArticleLink = latestArticleLinks[rssLink];
if (!latestArticleLink) {
latestArticleLink = articles[0].link; // 默认取第一篇文章
}
// 检查是否有新文章
const newArticles = articles.filter(article => article.link !== latestArticleLink);
if (newArticles.length > 0) {
latestArticleLinks[rssLink] = newArticles[0].link; // 更新最新文章链接
// 提示用户有新文章
vscode.window.showInformationMessage(`${rssLinks[rssLink]} 更新了文章`);
}
}
}, RSS_CHECK_INTERVAL);
let disposable = vscode.commands.registerCommand('allblog.searchBlog', async () => {
// 选项
const choiceItems = [
{ label: 'Add new RSS link', isNew: true },
...Object.keys(rssLinks).map(link => ({ label: rssLinks[link], link, isNew: false })),
{ label: 'Delete RSS link', isDelete: true }
];
const selectedChoice = await vscode.window.showQuickPick(choiceItems, {
placeHolder: 'Select an RSS link or add a new one'
});
if (selectedChoice) {
if (selectedChoice.isNew) {
const newLink = await vscode.window.showInputBox({
prompt: 'Enter RSS link',
validateInput: (text) => {
if (!RSS_LINK_REGEX.test(text)) {
return 'Please enter a valid RSS link (starting with http:// or https://)';
}
return null;
}
});
if (newLink) {
const customName = await vscode.window.showInputBox({
prompt: 'Enter a custom name for this RSS link'
});
if (customName) {
// 检查重复的自定义名称
const duplicateCustomName = Object.values(rssLinks).includes(customName);
if (duplicateCustomName) {
vscode.window.showErrorMessage('This custom name is already in use. Please choose a different name.');
return;
}
// 添加新的RSS链接和对应的自定义名字
rssLinks[newLink] = customName;
// 保存订阅信息到用户的本地
context.globalState.update(RSS_LINKS_CONFIG_KEY, rssLinks);
await getArticleContent(newLink, customName);
}
}
}else if (selectedChoice.isDelete) {
const linkToDelete = await vscode.window.showQuickPick(Object.keys(rssLinks).map(link => ({ label: rssLinks[link], link })), {
placeHolder: '选择要删除的RSS链接'
});
if (linkToDelete) {
delete rssLinks[linkToDelete.link];
context.globalState.update(RSS_LINKS_CONFIG_KEY, rssLinks);
}
} else {
await getArticleContent(selectedChoice.link, selectedChoice.label);
}
}
});
context.subscriptions.push(disposable);
}
function deactivate() { }
module.exports = {
activate,
deactivate
};