-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathbackupAccount.js
More file actions
161 lines (137 loc) · 5.62 KB
/
Copy pathbackupAccount.js
File metadata and controls
161 lines (137 loc) · 5.62 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
// Copyright (c) 2024-2026 nich (@nichxbt). Licensed under the Apache License, Version 2.0.
// scripts/backupAccount.js
// Browser console script for backing up your X/Twitter profile and tweets as JSON
// Paste in DevTools console on x.com/YOUR_USERNAME
// by nichxbt
(() => {
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
// =============================================
// CONFIGURATION
// =============================================
const CONFIG = {
maxTweets: 100, // Max tweets to scrape
scrollDelay: 1500, // Delay between scrolls (ms)
autoDownload: true, // Download backup JSON automatically
};
// =============================================
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();
};
const parseCount = (text) => {
if (!text) return 0;
const clean = text.replace(/,/g, '').trim();
const num = parseFloat(clean);
if (clean.includes('K')) return Math.round(num * 1000);
if (clean.includes('M')) return Math.round(num * 1000000);
return isNaN(num) ? 0 : num;
};
const scrapeProfile = () => {
const getText = (sel) => document.querySelector(sel)?.textContent?.trim() || null;
return {
displayName: document.querySelector('[data-testid="UserName"]')?.querySelector('span')?.textContent?.trim() || null,
bio: getText('[data-testid="UserDescription"]'),
location: getText('[data-testid="UserLocation"]'),
website: getText('[data-testid="UserUrl"]'),
joinDate: getText('[data-testid="UserJoinDate"]'),
isVerified: !!document.querySelector('[data-testid="icon-verified"]'),
followers: parseCount(document.querySelector('a[href$="/followers"] span')?.textContent),
following: parseCount(document.querySelector('a[href$="/following"] span')?.textContent),
avatarUrl: document.querySelector('[data-testid="UserAvatar-Container"] img')?.src || null,
headerUrl: document.querySelector('a[href$="/header_photo"] img')?.src || null,
};
};
const extractTweet = (el) => {
const textEl = el.querySelector('[data-testid="tweetText"]');
const linkEl = el.querySelector('a[href*="/status/"]');
const timeEl = el.querySelector('time');
const images = [...el.querySelectorAll('[data-testid="tweetPhoto"] img')].map(img => img.src);
const videoEl = el.querySelector('video');
return {
text: textEl?.textContent || '',
url: linkEl?.href || '',
tweetId: linkEl?.href?.match(/status\/(\d+)/)?.[1] || '',
timestamp: timeEl?.dateTime || '',
likes: el.querySelector('[data-testid="like"] span')?.textContent || '0',
reposts: el.querySelector('[data-testid="retweet"] span')?.textContent || '0',
replies: el.querySelector('[data-testid="reply"] span')?.textContent || '0',
media: {
images,
hasVideo: !!videoEl,
},
};
};
const run = async () => {
console.log('💾 BACKUP ACCOUNT — XActions by nichxbt');
console.log('━'.repeat(45));
const pathMatch = window.location.pathname.match(/^\/([A-Za-z0-9_]+)/);
const username = pathMatch ? pathMatch[1] : null;
if (!username || ['home', 'explore', 'notifications', 'messages', 'i', 'settings', 'search'].includes(username)) {
console.error('❌ Navigate to a profile page first! (x.com/USERNAME)');
return;
}
console.log(`\n👤 Backing up @${username}...\n`);
// Scrape profile info
console.log('📋 Scraping profile...');
const profile = scrapeProfile();
console.log(' ✅ Profile scraped');
// Scroll and collect tweets
console.log(`📥 Collecting tweets (max ${CONFIG.maxTweets})...`);
const tweets = new Map();
let noNewCount = 0;
while (tweets.size < CONFIG.maxTweets && noNewCount < 5) {
const els = document.querySelectorAll('article[data-testid="tweet"]');
const prevSize = tweets.size;
els.forEach(el => {
try {
const tweet = extractTweet(el);
if (tweet.tweetId && !tweets.has(tweet.tweetId)) {
tweets.set(tweet.tweetId, tweet);
}
} catch {}
});
if (tweets.size === prevSize) noNewCount++;
else noNewCount = 0;
if (tweets.size < CONFIG.maxTweets) {
window.scrollTo(0, document.body.scrollHeight);
await sleep(CONFIG.scrollDelay);
}
if (tweets.size % 20 === 0 && tweets.size > 0) {
console.log(` 📊 ${tweets.size} tweets collected...`);
}
}
console.log(` ✅ ${tweets.size} tweets collected`);
// Build backup object
const backup = {
meta: {
tool: 'XActions Backup',
version: '1.0.0',
createdAt: new Date().toISOString(),
url: window.location.href,
},
username,
profile,
tweets: [...tweets.values()],
stats: {
tweetCount: tweets.size,
hasProfile: !!profile.displayName,
},
};
console.log('\n' + '━'.repeat(45));
console.log('✅ Backup complete!');
console.log(` 📊 Profile: ${backup.stats.hasProfile ? 'Yes' : 'No'}`);
console.log(` 📊 Tweets: ${backup.stats.tweetCount}`);
if (CONFIG.autoDownload) {
download(backup, `xactions-backup-${username}-${new Date().toISOString().slice(0, 10)}.json`);
console.log('📥 Backup downloaded as JSON');
}
window.__xactions_backup = backup;
console.log('💡 Access data: window.__xactions_backup');
console.log('');
};
run();
})();