-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode-hints-userscript.js
More file actions
278 lines (242 loc) · 11.2 KB
/
leetcode-hints-userscript.js
File metadata and controls
278 lines (242 loc) · 11.2 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// ==UserScript==
// @name LeetCode Pattern Hints
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Highlight LeetCode keywords and show algorithm hints
// @author You
// @match https://leetcode.com/*
// @match https://*.leetcode.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Pattern Database
const PATTERN_DATABASE = {
"array": { pattern: "Array/String", color: "🔴", hints: ["Use two pointers for sorted arrays", "Consider prefix sum for range queries"] },
"string": { pattern: "Array/String", color: "🔴", hints: ["Use sliding window for substrings", "Consider hash map for character counting"] },
"linked list": { pattern: "Linked List", color: "🔵", hints: ["Use two pointers for cycle detection", "Consider dummy head for edge cases"] },
"tree": { pattern: "Tree/Graph", color: "🟡", hints: ["Use BFS for level-order traversal", "Consider DFS for path problems"] },
"graph": { pattern: "Tree/Graph", color: "🟡", hints: ["Use BFS for shortest path", "Consider DFS for connected components"] },
"hash": { pattern: "Hash Map/Set", color: "🟢", hints: ["Use for O(1) lookups", "Consider for frequency counting"] },
"hash map": { pattern: "Hash Map/Set", color: "🟢", hints: ["Use for O(1) lookups", "Consider for frequency counting"] },
"hash set": { pattern: "Hash Map/Set", color: "🟢", hints: ["Use for O(1) lookups", "Consider for duplicate detection"] },
"stack": { pattern: "Stack/Queue", color: "🟠", hints: ["Use for LIFO operations", "Consider for parentheses matching"] },
"queue": { pattern: "Stack/Queue", color: "🟠", hints: ["Use for FIFO operations", "Consider for BFS traversal"] },
"heap": { pattern: "Heap/Priority Queue", color: "🟣", hints: ["Use for priority-based operations", "Consider for kth largest/smallest"] },
"priority queue": { pattern: "Heap/Priority Queue", color: "🟣", hints: ["Use for priority-based operations", "Consider for kth largest/smallest"] },
"binary search": { pattern: "Binary Search", color: "🔵", hints: ["Use on sorted arrays", "Consider for search problems"] },
"two pointers": { pattern: "Two Pointers", color: "🔵", hints: ["Use on sorted arrays", "Consider for palindromes"] },
"sliding window": { pattern: "Sliding Window", color: "🟡", hints: ["Use for substring problems", "Consider for subarray problems"] },
"dynamic programming": { pattern: "Dynamic Programming", color: "🟣", hints: ["Use for optimization problems", "Consider for overlapping subproblems"] },
"backtracking": { pattern: "Backtracking", color: "🟠", hints: ["Use for combination problems", "Consider for permutation problems"] },
"greedy": { pattern: "Greedy", color: "🟢", hints: ["Use for optimization problems", "Consider for scheduling problems"] },
"union find": { pattern: "Union Find", color: "🟣", hints: ["Use for connectivity problems", "Consider for cycle detection"] },
"trie": { pattern: "Trie", color: "🟡", hints: ["Use for prefix problems", "Consider for word search"] },
"segment tree": { pattern: "Segment Tree", color: "🟣", hints: ["Use for range queries", "Consider for range updates"] },
"fenwick tree": { pattern: "Fenwick Tree", color: "🟣", hints: ["Use for prefix sum queries", "Consider for range sum updates"] }
};
const keywords = Object.keys(PATTERN_DATABASE);
let isEnabled = true;
// Inject CSS
function injectCSS() {
const style = document.createElement('style');
style.id = 'leetcode-hints-styles';
style.textContent = `
.leetcode-hint-keyword {
background: linear-gradient(120deg, #a8edea 0%, #fed6e3 100%) !important;
padding: 1px 3px !important;
border-radius: 2px !important;
cursor: pointer !important;
transition: all 0.2s ease !important;
position: relative !important;
display: inline !important;
margin: 0 1px !important;
}
.leetcode-hint-keyword:hover {
background: linear-gradient(120deg, #ff9a9e 0%, #fecfef 100%) !important;
transform: scale(1.02) !important;
box-shadow: 0 2px 4px rgba(0,0,0,0.2) !important;
}
.leetcode-hint-tooltip {
position: fixed !important;
background: #1a1a1a !important;
color: #ffffff !important;
padding: 8px 12px !important;
border-radius: 6px !important;
font-size: 12px !important;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
z-index: 99999 !important;
box-shadow: 0 4px 12px rgba(0,0,0,0.4) !important;
opacity: 0 !important;
pointer-events: none !important;
transition: opacity 0.2s ease !important;
max-width: 250px !important;
white-space: normal !important;
line-height: 1.4 !important;
border: 1px solid #333 !important;
}
.hint-pattern {
font-weight: bold !important;
color: #4CAF50 !important;
margin-bottom: 2px !important;
display: block !important;
}
.hint-tips {
font-size: 11px !important;
color: #ccc !important;
margin-top: 4px !important;
}
`;
document.head.appendChild(style);
}
// Highlight keywords in text
function highlightText(text) {
let highlighted = text;
for (const keyword of keywords) {
const regex = new RegExp(`\\b(${keyword})\\b`, 'gi');
highlighted = highlighted.replace(regex, (match) => {
const data = PATTERN_DATABASE[keyword.toLowerCase()];
return `<span class="leetcode-hint-keyword" data-keyword="${keyword.toLowerCase()}">${match}</span>`;
});
}
return highlighted;
}
// Process text nodes
function processTextNode(textNode) {
if (!isEnabled) return false;
const text = textNode.textContent;
if (!text || text.trim().length === 0) return false;
// Check if text contains any keywords
let hasKeyword = false;
for (const keyword of keywords) {
if (text.toLowerCase().includes(keyword)) {
hasKeyword = true;
break;
}
}
if (!hasKeyword) return false;
const parent = textNode.parentNode;
if (!parent || parent.tagName === 'SCRIPT' || parent.tagName === 'STYLE') return false;
if (parent.dataset.leetcodeHints === 'processed') return false;
// Create highlighted content
const highlighted = highlightText(text);
if (highlighted === text) return false;
// Replace text node
const wrapper = document.createElement('span');
wrapper.innerHTML = highlighted;
wrapper.dataset.leetcodeHints = 'processed';
// Add hover events
wrapper.querySelectorAll('.leetcode-hint-keyword').forEach(span => {
span.addEventListener('mouseenter', showTooltip);
span.addEventListener('mouseleave', hideTooltip);
});
parent.replaceChild(wrapper, textNode);
return true;
}
// Show tooltip
function showTooltip(event) {
const keyword = event.target.dataset.keyword;
const data = PATTERN_DATABASE[keyword];
if (!data) return;
// Remove existing tooltip
hideTooltip();
// Create tooltip
const tooltip = document.createElement('div');
tooltip.className = 'leetcode-hint-tooltip';
tooltip.innerHTML = `
<div class="hint-pattern">${data.color} ${data.pattern}</div>
<div class="hint-tips">${data.hints.join(' • ')}</div>
`;
// Position tooltip
const rect = event.target.getBoundingClientRect();
tooltip.style.left = `${rect.left + rect.width / 2}px`;
tooltip.style.top = `${rect.top - 10}px`;
tooltip.style.transform = 'translateX(-50%)';
tooltip.style.opacity = '1';
document.body.appendChild(tooltip);
}
// Hide tooltip
function hideTooltip() {
const tooltips = document.querySelectorAll('.leetcode-hint-tooltip');
tooltips.forEach(tooltip => tooltip.remove());
}
// Scan page for keywords
function scanPage() {
if (!isEnabled) return;
console.log('🔍 Scanning page for keywords...');
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
null,
false
);
let processedCount = 0;
let node;
while (node = walker.nextNode()) {
if (processTextNode(node)) {
processedCount++;
}
}
console.log(`✅ Processed ${processedCount} text nodes`);
}
// Clear highlights
function clearHighlights() {
const highlighted = document.querySelectorAll('[data-leetcode-hints="processed"]');
highlighted.forEach(element => {
if (element.parentNode) {
element.parentNode.replaceChild(
document.createTextNode(element.textContent),
element
);
}
});
hideTooltip();
}
// Toggle function
function toggle() {
isEnabled = !isEnabled;
if (isEnabled) {
scanPage();
} else {
clearHighlights();
}
console.log('Toggle:', isEnabled ? 'ON' : 'OFF');
}
// Add toggle button
function addToggleButton() {
const button = document.createElement('button');
button.textContent = '🔍 LeetCode Hints';
button.style.cssText = `
position: fixed !important;
top: 20px !important;
right: 20px !important;
z-index: 100000 !important;
background: #007bff !important;
color: white !important;
border: none !important;
padding: 10px 15px !important;
border-radius: 5px !important;
cursor: pointer !important;
font-size: 14px !important;
box-shadow: 0 2px 5px rgba(0,0,0,0.2) !important;
`;
button.onclick = toggle;
document.body.appendChild(button);
}
// Initialize
function init() {
console.log('🚀 LeetCode Hints UserScript loaded!');
injectCSS();
addToggleButton();
// Wait for page to load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
setTimeout(scanPage, 1000);
});
} else {
setTimeout(scanPage, 1000);
}
}
// Start
init();
})();