-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathtrendingTopicMonitor.js
More file actions
191 lines (166 loc) · 8.44 KB
/
Copy pathtrendingTopicMonitor.js
File metadata and controls
191 lines (166 loc) · 8.44 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
// Copyright (c) 2024-2026 nich (@nichxbt). Licensed under the Apache License, Version 2.0.
// scripts/trendingTopicMonitor.js
// Browser console script for tracking trending topics and detecting rising trends
// Paste in DevTools console on x.com/explore/tabs/trending
// by nichxbt
(() => {
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
// =============================================
// CONFIGURATION
// =============================================
const CONFIG = {
checkInterval: 300000, // Auto-refresh interval (ms) — 5 min
maxChecks: 50, // Max snapshots to keep
scrollRounds: 4, // Scroll rounds per scan
scrollDelay: 1500, // ms between scrolls
watchKeywords: [], // Alert when trends match
exportResults: true, // Auto-download JSON
};
// =============================================
const STORAGE_KEY = 'xactions_trend_history';
const download = (data, filename) => {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }));
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
console.log(`📥 Downloaded: ${filename}`);
};
const NICHES = {
'Tech': ['ai', 'chatgpt', 'openai', 'google', 'apple', 'microsoft', 'nvidia', 'coding', 'crypto', 'bitcoin', 'web3'],
'Politics': ['trump', 'biden', 'congress', 'election', 'democrat', 'republican', 'government'],
'Sports': ['nfl', 'nba', 'mlb', 'soccer', 'football', 'basketball', 'fifa', 'ufc', 'f1'],
'Entertainment': ['movie', 'music', 'album', 'netflix', 'disney', 'grammy', 'oscar'],
'Business': ['stock', 'market', 'earnings', 'ipo', 'startup', 'economy', 'fed'],
'Gaming': ['gaming', 'playstation', 'xbox', 'nintendo', 'steam', 'esports'],
};
const classifyNiche = (text) => {
if (!text) return 'Other';
const lower = text.toLowerCase();
for (const [niche, kws] of Object.entries(NICHES)) {
if (kws.some(kw => lower.includes(kw))) return niche;
}
return 'Other';
};
const loadHistory = () => { try { return JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]'); } catch { return []; } };
const saveSnapshot = (trends) => {
const h = loadHistory();
h.push({ timestamp: new Date().toISOString(), trends: trends.map(t => ({ topic: t.topic, rank: t.rank, postCount: t.postCount, niche: t.niche })) });
while (h.length > CONFIG.maxChecks) h.shift();
localStorage.setItem(STORAGE_KEY, JSON.stringify(h));
};
let autoTimer = null;
const scrapeTrends = async () => {
const trends = [];
const seen = new Set();
for (let round = 0; round < CONFIG.scrollRounds; round++) {
const cells = document.querySelectorAll('[data-testid="trend"]');
for (const cell of cells) {
const spans = cell.querySelectorAll('span');
let topic = '', tweetCount = '';
for (const el of spans) {
const text = el.textContent.trim();
if (/[\d,.]+[KM]?\s*(posts|tweets)/i.test(text)) tweetCount = text;
if (text.startsWith('#') || (text.length > 2 && text.length < 80 && !text.includes('·') && !text.includes('Trending') && !/posts|tweets/i.test(text) && !/^\d+$/.test(text))) {
if (text.length > topic.length) topic = text;
}
}
if (!topic || seen.has(topic.toLowerCase())) continue;
seen.add(topic.toLowerCase());
let postCount = 0;
const cm = tweetCount.match(/([\d,.]+)\s*([KM])?/i);
if (cm) { postCount = parseFloat(cm[1].replace(/,/g, '')); if (cm[2] === 'K') postCount *= 1000; if (cm[2] === 'M') postCount *= 1000000; }
trends.push({ rank: trends.length + 1, topic, niche: classifyNiche(topic), postCount, postCountRaw: tweetCount });
}
console.log(` 📜 Round ${round + 1}: ${trends.length} trends`);
window.scrollTo(0, document.body.scrollHeight);
await sleep(CONFIG.scrollDelay);
}
return trends;
};
const compareWithLast = (trends) => {
const h = loadHistory();
if (h.length < 2) { console.log(' 📊 Need more snapshots to compare.'); return; }
const prev = h[h.length - 2];
const prevMap = new Map(prev.trends.map(t => [t.topic.toLowerCase(), t]));
const newTrends = [];
const rising = [];
for (const t of trends) {
const p = prevMap.get(t.topic.toLowerCase());
if (!p) newTrends.push(t);
else if (t.rank < p.rank) rising.push({ ...t, prevRank: p.rank, change: p.rank - t.rank });
}
if (newTrends.length > 0) {
console.log(`\n 🆕 NEW (${newTrends.length}):`);
newTrends.slice(0, 8).forEach(t => console.log(` #${t.rank} ${t.topic} [${t.niche}]`));
}
if (rising.length > 0) {
console.log(`\n 📈 RISING (${rising.length}):`);
rising.sort((a, b) => b.change - a.change).slice(0, 5).forEach(t => console.log(` ↑${t.change} ${t.topic} (#${t.prevRank} → #${t.rank})`));
}
};
const run = async () => {
console.log('╔════════════════════════════════════════════════╗');
console.log('║ 📡 TRENDING TOPIC MONITOR ║');
console.log('║ by nichxbt — v1.0 ║');
console.log('╚════════════════════════════════════════════════╝');
console.log('\n📊 Scraping trends...\n');
const trends = await scrapeTrends();
if (trends.length === 0) { console.error('❌ No trends found. Go to x.com/explore/tabs/trending'); return; }
console.log(`\n━━━ 📡 TRENDING NOW (${trends.length}) ━━━`);
trends.forEach(t => {
const count = t.postCountRaw ? ` (${t.postCountRaw})` : '';
console.log(` ${String(t.rank).padStart(2)}. [${t.niche.padEnd(14)}] ${t.topic}${count}`);
});
// Niche distribution
const nicheCounts = {};
trends.forEach(t => nicheCounts[t.niche] = (nicheCounts[t.niche] || 0) + 1);
console.log('\n━━━ 📊 NICHE DISTRIBUTION ━━━');
Object.entries(nicheCounts).sort((a, b) => b[1] - a[1]).forEach(([n, c]) => {
console.log(` ${n.padEnd(16)} ${c} (${((c / trends.length) * 100).toFixed(0)}%) ${'█'.repeat(Math.round(c / trends.length * 20))}`);
});
// Keyword alerts
if (CONFIG.watchKeywords.length > 0) {
console.log('\n━━━ 🔔 KEYWORD ALERTS ━━━');
let found = 0;
trends.forEach(t => {
CONFIG.watchKeywords.forEach(kw => {
if (t.topic.toLowerCase().includes(kw.toLowerCase())) {
console.log(` 🚨 "${t.topic}" matches "${kw}" (rank #${t.rank})`);
found++;
}
});
});
if (found === 0) console.log(' No matches.');
}
// Content opportunities
const highVol = trends.filter(t => t.postCount > 10000).slice(0, 5);
if (highVol.length > 0) {
console.log('\n━━━ 💡 CONTENT OPPORTUNITIES ━━━');
highVol.forEach(t => console.log(` → ${t.topic} (${t.postCountRaw}) [${t.niche}]`));
}
saveSnapshot(trends);
compareWithLast(trends);
console.log('');
if (CONFIG.exportResults) {
download({ trends, analyzedAt: new Date().toISOString() }, `xactions-trends-${new Date().toISOString().slice(0, 10)}.json`);
}
};
window.XActions = window.XActions || {};
window.XActions.watch = (kws) => { CONFIG.watchKeywords = kws; console.log(`👁️ Watching: ${kws.join(', ')}`); };
window.XActions.history = () => {
const h = loadHistory();
if (h.length === 0) { console.log('📭 No history.'); return; }
h.slice(-10).forEach(s => console.log(` ${new Date(s.timestamp).toLocaleString()} — ${s.trends.length} trends: ${s.trends.slice(0, 3).map(t => t.topic).join(', ')}`));
};
window.XActions.compare = () => { const h = loadHistory(); if (h.length < 2) { console.log('Need 2+ snapshots.'); return; } compareWithLast(h[h.length - 1].trends); };
window.XActions.autoRefresh = (ms) => {
if (autoTimer) clearInterval(autoTimer);
const interval = ms || CONFIG.checkInterval;
console.log(`🔄 Auto-refresh every ${(interval / 60000).toFixed(1)} min.`);
autoTimer = setInterval(() => { console.log('\n🔄 Refreshing...'); run(); }, interval);
};
window.XActions.stop = () => { if (autoTimer) { clearInterval(autoTimer); autoTimer = null; } console.log('⏹️ Stopped.'); };
run();
})();