Repository navigation
Expand file tree
/
Copy pathsidebar.js
More file actions
192 lines (174 loc) · 6.42 KB
/
Copy pathsidebar.js
File metadata and controls
192 lines (174 loc) · 6.42 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
// =============================================================================
// sidebar.js — YouTube Studio Sidebar Panel
// =============================================================================
// This sidebar runs alongside YouTube and provides always-on SEO insights.
// Uses the same lib modules as the popup but is designed for persistent use.
// =============================================================================
import {
scoreSEO,
extractKeywords,
} from "./lib/seo.js";
import {
MODELS,
DEFAULT_MODEL,
loadModel,
generateSuggestions,
isWebGPUAvailable,
} from "./lib/engine.js";
const $ = (id) => document.getElementById(id);
let pageData = null;
// =============================================================================
// Close sidebar
// =============================================================================
$("btn-close")?.addEventListener("click", () => {
// Side panel can't close itself programmatically; just hide the content
window.close();
});
// =============================================================================
// GPU badge
// =============================================================================
const gpuBadge = $("sidebar-gpu");
if (isWebGPUAvailable()) {
gpuBadge.textContent = "WebGPU ✓";
gpuBadge.classList.add("ok");
} else {
gpuBadge.textContent = "No WebGPU";
gpuBadge.classList.add("no");
}
// =============================================================================
// Scrape and render
// =============================================================================
async function scrapeAndRender() {
try {
const response = await chrome.runtime.sendMessage({ type: "GET_PAGE_DATA" });
if (response?.ok) {
pageData = response;
renderAll(response);
} else {
$("not-watch").classList.remove("hidden");
$("watch-panel").classList.add("hidden");
}
} catch (e) {
// Content script may not be available
$("not-watch").classList.remove("hidden");
$("watch-panel").classList.add("hidden");
}
}
// =============================================================================
// Render all sections
// =============================================================================
function renderAll(data) {
if (!data || !data.isWatch) {
$("not-watch").classList.remove("hidden");
$("watch-panel").classList.add("hidden");
return;
}
$("not-watch").classList.add("hidden");
$("watch-panel").classList.remove("hidden");
renderScore(data);
renderKeywords(data);
renderChecklist(data);
}
// =============================================================================
// Score ring
// =============================================================================
function renderScore(data) {
const { score, grade, passed, total } = scoreSEO(data);
const ring = $("sidebar-score-ring");
const col =
score >= 75 ? "var(--good)" : score >= 55 ? "var(--warn)" : "var(--bad)";
ring.style.setProperty("--val", score);
ring.style.setProperty("--col", col);
$("sidebar-score-num").textContent = score;
$("sidebar-score-grade").textContent = `SEO: ${grade}`;
$("sidebar-score-sub").textContent = `${passed} of ${total} checks passed`;
}
// =============================================================================
// Keywords
// =============================================================================
function renderKeywords(data) {
const keywords = extractKeywords(data, 10);
const container = $("sidebar-keywords");
container.innerHTML = "";
for (const k of keywords) {
const chip = document.createElement("span");
chip.className = "chip";
chip.textContent = k.word;
chip.onclick = () => {
navigator.clipboard.writeText(k.word).catch(() => {});
chip.classList.add("copied");
setTimeout(() => chip.classList.remove("copied"), 800);
};
container.appendChild(chip);
}
}
// =============================================================================
// Checklist
// =============================================================================
function renderChecklist(data) {
const { checks } = scoreSEO(data);
const list = $("sidebar-checklist");
list.innerHTML = "";
for (const c of checks.slice(0, 8)) {
const li = document.createElement("li");
li.className = c.pass ? "pass" : "fail";
li.innerHTML = `<span class="icon">${c.pass ? "✓" : "!"}</span>
<span>${c.label}</span>`;
list.appendChild(li);
}
}
// =============================================================================
// AI optimization
// =============================================================================
$("sidebar-btn-optimize")?.addEventListener("click", async () => {
if (!isWebGPUAvailable() || !pageData) return;
const btn = $("sidebar-btn-optimize");
btn.disabled = true;
btn.textContent = "Generating…";
try {
await loadModel(DEFAULT_MODEL, () => {});
const keywords = extractKeywords(pageData, 10);
const result = await generateSuggestions({
title: pageData.title,
description: pageData.description,
keywords: keywords.map((k) => k.word),
});
const container = $("sidebar-ai-content");
container.innerHTML = "";
if (result.titles?.length) {
container.innerHTML +=
"<strong style='font-size:11px;color:#9aa3b2;text-transform:uppercase'>Titles</strong>";
result.titles.forEach((t) => {
const div = document.createElement("div");
div.className = "ai-sidebar-item";
div.textContent = t;
div.onclick = () => {
navigator.clipboard.writeText(t).catch(() => {});
div.classList.add("copied");
setTimeout(() => div.classList.remove("copied"), 800);
};
container.appendChild(div);
});
}
$("sidebar-ai-results").classList.remove("hidden");
} catch (e) {
console.log("[sidebar AI] Error:", e.message);
} finally {
btn.disabled = false;
btn.textContent = "Optimize with AI";
}
});
// =============================================================================
// Init
// =============================================================================
document.addEventListener("DOMContentLoaded", () => {
scrapeAndRender();
// Re-scrape every 30 seconds for live updates
setInterval(scrapeAndRender, 30000);
});
// Also respond to background requests
chrome.runtime.onMessage.addListener((msg) => {
if (msg?.type === "OPENTUBE_SIDEBAR_REFRESH") {
scrapeAndRender();
}
});