-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnhanced DWAI Card Button for React-0.1.user.js
153 lines (129 loc) · 4.17 KB
/
Enhanced DWAI Card Button for React-0.1.user.js
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
// ==UserScript==
// @name Enhanced DWAI Card Button for React
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add quick-open buttons to DWAI cards in React app
// @author Your name
// @match *://node.dawuai.buzz/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 创建样式
function addStyles() {
const style = document.createElement('style');
style.textContent = `
.dwai-quick-open {
position: absolute !important;
top: 5px;
right: 5px;
background: rgba(255, 255, 255, 0.9) !important;
border: 1px solid #ccc !important;
border-radius: 4px !important;
padding: 2px 6px !important;
cursor: pointer;
font-size: 12px !important;
z-index: 100;
opacity: 0;
transition: opacity 0.2s;
color: #666 !important;
}
.dwai-card-wrapper {
position: relative !important;
}
.dwai-card-wrapper:hover .dwai-quick-open {
opacity: 1;
}
.dwai-quick-open:hover {
background: rgba(255, 255, 255, 1) !important;
color: #333 !important;
}
`;
document.head.appendChild(style);
}
// 创建按钮
function createButton(url) {
const button = document.createElement('button');
button.className = 'dwai-quick-open';
button.innerHTML = '↗️';
button.title = '在新标签页打开';
button.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
window.open(url, '_blank');
});
return button;
}
// 获取链接URL
function getCardUrl(element) {
// 检查data属性
const dataUrl = element.dataset.url;
if (dataUrl) return dataUrl;
// 检查href属性
const href = element.getAttribute('href');
if (href) return href;
// 检查子元素中的链接
const link = element.querySelector('a');
if (link && link.href) return link.href;
// 检查onclick事件
const onclick = element.getAttribute('onclick');
const match = onclick?.match(/window\.location=['"]([^'"]+)['"]/);
if (match) return match[1];
// 检查父元素
const parentLink = element.closest('a');
if (parentLink && parentLink.href) return parentLink.href;
return null;
}
// 处理卡片
function processCard(card) {
// 如果已经处理过,跳过
if (card.querySelector('.dwai-quick-open')) {
return;
}
// 获取URL
const url = getCardUrl(card);
if (!url) {
return;
}
// 添加包装器类名
card.classList.add('dwai-card-wrapper');
// 创建并添加按钮
const button = createButton(url);
card.appendChild(button);
}
// 初始化函数
function init() {
// 添加样式
addStyles();
// 查找可能的卡片选择器
const selectors = [
'[class*="card"]',
'[class*="TEAM"]',
'[class*="PLUS"]',
'a[href*="/detail/"]',
'div[onclick*="window.location"]'
];
// 创建观察器
const observer = new MutationObserver((mutations) => {
// 等待React渲染完成
setTimeout(() => {
document.querySelectorAll(selectors.join(',')).forEach(processCard);
}, 100);
});
// 开始观察
observer.observe(document.body, {
childList: true,
subtree: true
});
// 初始处理
setTimeout(() => {
document.querySelectorAll(selectors.join(',')).forEach(processCard);
}, 1000);
}
// 当DOM加载完成时初始化
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();