-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplanet-follow-button.user.js
More file actions
214 lines (185 loc) · 6.99 KB
/
Copy pathplanet-follow-button.user.js
File metadata and controls
214 lines (185 loc) · 6.99 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// ==UserScript==
// @name V2EX Planet 关注按钮
// @namespace https://github.com/tampermonkey/tampermonkey
// @version 1.0.0
// @description 为V2EX Planet页面的博客添加关注按钮,支持一键添加到Planet应用中进行阅读和管理
// @author JoeJoeJoe
// @homepage https://github.com/HelloWorldImJoe/TampermonkeyScripts
// @supportURL https://github.com/HelloWorldImJoe/TampermonkeyScripts/issues
// @match https://www.v2ex.com/planet
// @match https://www.v2ex.com/planet?p=*
// @match https://v2ex.com/planet
// @match https://v2ex.com/planet?p=*
// @icon https://www.v2ex.com/static/favicon.ico
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// @license MIT
// @created 2025-09-19
// @updated 2025-09-19
// ==/UserScript==
(function() {
'use strict';
// 添加自定义样式
GM_addStyle(`
.follow-button {
position: absolute;
top: 8px;
right: 8px;
background-color: #4CAF50;
color: white;
border: none;
padding: 4px 8px;
border-radius: 12px;
cursor: pointer;
font-size: 11px;
font-weight: 500;
transition: all 0.2s ease;
z-index: 10;
box-shadow: 0 1px 3px rgba(0,0,0,0.2);
min-width: 48px;
text-align: center;
}
.follow-button:hover {
background-color: #45a049;
transform: translateY(-1px);
box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}
.follow-button.following {
background-color: #f44336;
cursor: pointer;
opacity: 1;
}
.follow-button.following:hover {
background-color: #da190b;
transform: translateY(-1px);
}
.planet-post {
position: relative;
}
/* 夜间模式适配 */
.Night .follow-button {
background-color: #555;
color: #fff;
box-shadow: 0 1px 3px rgba(255,255,255,0.1);
}
.Night .follow-button:hover {
background-color: #666;
box-shadow: 0 2px 6px rgba(255,255,255,0.15);
}
.Night .follow-button.following {
background-color: #d32f2f;
cursor: pointer;
opacity: 1;
}
.Night .follow-button.following:hover {
background-color: #b71c1c;
transform: translateY(-1px);
}
`);
// 获取已关注的站点列表
function getFollowedSites() {
const followed = GM_getValue('followedSites', '[]');
return JSON.parse(followed);
}
// 保存已关注的站点列表
function saveFollowedSites(sites) {
GM_setValue('followedSites', JSON.stringify(sites));
}
// 切换关注状态
function toggleFollow(siteAddress, siteName, button) {
const followedSites = getFollowedSites();
const siteIndex = followedSites.findIndex(site => site.address === siteAddress);
if (siteIndex > -1) {
// 取消关注
followedSites.splice(siteIndex, 1);
button.textContent = '关注';
button.classList.remove('following');
console.log(`已取消关注: ${siteName}`);
saveFollowedSites(followedSites);
} else {
// 添加关注
followedSites.push({
address: siteAddress,
name: siteName,
followedAt: new Date().toISOString()
});
button.textContent = '已关注';
button.classList.add('following');
console.log(`已关注: ${siteName}`);
saveFollowedSites(followedSites);
// 只有在添加关注时才打开 planet:// 链接
const planetUrl = `planet://${siteAddress}`;
try {
window.open(planetUrl, '_blank');
} catch (e) {
// 如果浏览器不支持自定义协议,在控制台显示链接
console.log(`请复制此链接到支持的应用中打开: ${planetUrl}`);
alert(`已关注 ${siteName}!\n\n如需在Planet应用中打开,请复制此链接:\n${planetUrl}`);
}
}
}
// 创建关注按钮
function createFollowButton(siteAddress, siteName) {
const followedSites = getFollowedSites();
const isFollowing = followedSites.some(site => site.address === siteAddress);
const button = document.createElement('button');
button.className = 'follow-button';
button.textContent = isFollowing ? '已关注' : '关注';
if (isFollowing) {
button.classList.add('following');
}
button.addEventListener('click', function(e) {
e.stopPropagation(); // 防止触发博客点击事件
e.preventDefault();
toggleFollow(siteAddress, siteName, button);
});
return button;
}
// 为所有博客文章添加关注按钮
function addFollowButtons() {
const blogPosts = document.querySelectorAll('.planet-post');
blogPosts.forEach(post => {
// 检查是否已经添加了关注按钮
if (post.querySelector('.follow-button')) {
return;
}
const siteAddress = post.getAttribute('data-site-address');
const siteNameElement = post.querySelector('.planet-site-title');
if (siteAddress && siteNameElement) {
const siteName = siteNameElement.textContent.trim();
const followButton = createFollowButton(siteAddress, siteName);
post.appendChild(followButton);
}
});
}
// 页面加载完成后执行
function init() {
// 等待页面元素加载完成
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', addFollowButtons);
} else {
addFollowButtons();
}
// 监听页面变化(处理动态加载的内容)
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
// 检查新增的节点是否包含博客文章
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1 && node.classList && node.classList.contains('planet-post')) {
// 为新加载的博客文章添加关注按钮
setTimeout(addFollowButtons, 100);
}
});
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}
// 启动脚本
init();
})();