-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (168 loc) · 6.03 KB
/
script.js
File metadata and controls
191 lines (168 loc) · 6.03 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
/* ═══════════════════════════════════════════
ScrapeGoat Docs — Interactivity
═══════════════════════════════════════════ */
document.addEventListener('DOMContentLoaded', () => {
initThemeToggle();
initMobileNav();
initCopyButtons();
initFAQ();
initScrollAnimations();
initActiveNavigation();
initBackToTop();
initCommandTabs();
});
/* ── Theme Toggle ── */
function initThemeToggle() {
const btn = document.getElementById('theme-toggle');
const saved = localStorage.getItem('scrapegoat-theme');
if (saved === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark');
btn.textContent = '☀️';
}
btn.addEventListener('click', () => {
const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
if (isDark) {
document.documentElement.removeAttribute('data-theme');
btn.textContent = '🌙';
localStorage.setItem('scrapegoat-theme', 'light');
} else {
document.documentElement.setAttribute('data-theme', 'dark');
btn.textContent = '☀️';
localStorage.setItem('scrapegoat-theme', 'dark');
}
});
}
/* ── Mobile Navigation ── */
function initMobileNav() {
const hamburger = document.getElementById('hamburger');
const mobileNav = document.getElementById('mobile-nav');
hamburger.addEventListener('click', () => {
mobileNav.classList.toggle('open');
const spans = hamburger.querySelectorAll('span');
if (mobileNav.classList.contains('open')) {
spans[0].style.transform = 'rotate(45deg) translate(5px, 5px)';
spans[1].style.opacity = '0';
spans[2].style.transform = 'rotate(-45deg) translate(6px, -6px)';
} else {
spans[0].style.transform = '';
spans[1].style.opacity = '1';
spans[2].style.transform = '';
}
});
// Close on link click
mobileNav.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
mobileNav.classList.remove('open');
const spans = hamburger.querySelectorAll('span');
spans[0].style.transform = '';
spans[1].style.opacity = '1';
spans[2].style.transform = '';
});
});
}
/* ── Copy to Clipboard ── */
function initCopyButtons() {
document.querySelectorAll('.code-block__copy').forEach(btn => {
btn.addEventListener('click', () => {
const codeBlock = btn.closest('.code-block');
const code = codeBlock.querySelector('code').textContent;
navigator.clipboard.writeText(code).then(() => {
btn.textContent = '✓ Copied!';
btn.classList.add('code-block__copy--copied');
setTimeout(() => {
btn.textContent = 'Copy';
btn.classList.remove('code-block__copy--copied');
}, 2000);
}).catch(() => {
// Fallback
const textarea = document.createElement('textarea');
textarea.value = code;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
btn.textContent = '✓ Copied!';
btn.classList.add('code-block__copy--copied');
setTimeout(() => {
btn.textContent = 'Copy';
btn.classList.remove('code-block__copy--copied');
}, 2000);
});
});
});
}
/* ── FAQ Accordion ── */
function initFAQ() {
document.querySelectorAll('.faq-question').forEach(btn => {
btn.addEventListener('click', () => {
const item = btn.closest('.faq-item');
const answer = item.querySelector('.faq-answer');
const isOpen = item.classList.contains('open');
// Close all others
document.querySelectorAll('.faq-item.open').forEach(openItem => {
if (openItem !== item) {
openItem.classList.remove('open');
openItem.querySelector('.faq-answer').style.maxHeight = '0';
}
});
if (isOpen) {
item.classList.remove('open');
answer.style.maxHeight = '0';
} else {
item.classList.add('open');
answer.style.maxHeight = answer.scrollHeight + 'px';
}
});
});
}
/* ── Scroll Animations ── */
function initScrollAnimations() {
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
}
});
}, { threshold: 0.1, rootMargin: '0px 0px -40px 0px' });
document.querySelectorAll('.fade-in').forEach(el => observer.observe(el));
}
/* ── Active Navigation Highlighting ── */
function initActiveNavigation() {
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('.navbar__links a');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const id = entry.target.id;
navLinks.forEach(link => {
link.classList.toggle('active', link.getAttribute('href') === `#${id}`);
});
}
});
}, { rootMargin: '-30% 0px -60% 0px' });
sections.forEach(section => observer.observe(section));
}
/* ── Back to Top ── */
function initBackToTop() {
const btn = document.getElementById('back-to-top');
window.addEventListener('scroll', () => {
btn.classList.toggle('visible', window.scrollY > 500);
});
btn.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
/* ── Command Tabs ── */
function initCommandTabs() {
const tabs = document.querySelectorAll('.commands-tab');
const panels = document.querySelectorAll('.command-panel');
tabs.forEach(tab => {
tab.addEventListener('click', () => {
const target = tab.dataset.tab;
tabs.forEach(t => t.classList.remove('active'));
panels.forEach(p => p.classList.remove('active'));
tab.classList.add('active');
document.getElementById(`panel-${target}`).classList.add('active');
});
});
}