-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
206 lines (187 loc) · 6.49 KB
/
Copy pathbackground.js
File metadata and controls
206 lines (187 loc) · 6.49 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
chrome.action.onClicked.addListener((tab) => {
if (tab.url.includes("x.com") || tab.url.includes("twitter.com")) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: captureAndBuildPDF,
world: "MAIN"
});
} else {
alert("Please navigate to an X (Twitter) thread first.");
}
});
async function captureAndBuildPDF() {
const statusDiv = document.createElement("div");
statusDiv.style.position = "fixed";
statusDiv.style.top = "20px";
statusDiv.style.right = "20px";
statusDiv.style.padding = "15px 20px";
statusDiv.style.backgroundColor = "#1d9bf0";
statusDiv.style.color = "#ffffff";
statusDiv.style.zIndex = "9999999";
statusDiv.style.borderRadius = "8px";
statusDiv.style.fontFamily = "sans-serif";
statusDiv.style.fontSize = "14px";
statusDiv.style.boxShadow = "0 4px 12px rgba(0,0,0,0.3)";
statusDiv.innerText = "Scrolling & capturing posts... Please wait.";
document.body.appendChild(statusDiv);
// Harvest visible posts directly from DOM during scrolling
function harvestDOM() {
window.__X_CAPTURED_POSTS = window.__X_CAPTURED_POSTS || new Map();
const articles = document.querySelectorAll('article[data-testid="tweet"]');
articles.forEach((art) => {
try {
const textEl = art.querySelector('[data-testid="tweetText"]');
const userEl = art.querySelector('[data-testid="User-Name"]');
const timeEl = art.querySelector('time');
const avatarImg = art.querySelector('img[src*="profile_images"]');
if (userEl && textEl) {
const userText = userEl.innerText.split('\n');
const name = userText[0] || 'Unknown';
const handle = (userText.find(t => t.startsWith('@')) || '').replace('@', '');
const text = textEl.innerText;
const time = timeEl ? timeEl.getAttribute('datetime') : new Date().toISOString();
const avatar = avatarImg ? avatarImg.src : '';
const key = handle + '_' + text.slice(0, 30);
if (!window.__X_CAPTURED_POSTS.has(key)) {
window.__X_CAPTURED_POSTS.set(key, {
id: key,
name: name,
handle: handle,
avatar: avatar,
text: text,
created_at: new Date(time).toLocaleString()
});
}
}
} catch (e) {}
});
}
let lastHeight = document.body.scrollHeight;
let sameHeightCount = 0;
// Auto-scroll loop
await new Promise((resolve) => {
const timer = setInterval(() => {
harvestDOM();
window.scrollTo(0, document.body.scrollHeight);
let newHeight = document.body.scrollHeight;
if (newHeight === lastHeight) {
sameHeightCount++;
} else {
sameHeightCount = 0;
lastHeight = newHeight;
}
if (sameHeightCount >= 4) {
harvestDOM();
clearInterval(timer);
resolve();
}
}, 1200);
});
statusDiv.style.backgroundColor = "#00ba7c";
statusDiv.innerText = "Opening PDF print dialog...";
const posts = window.__X_CAPTURED_POSTS ? Array.from(window.__X_CAPTURED_POSTS.values()) : [];
if (!posts || posts.length === 0) {
statusDiv.remove();
alert("No posts captured. Make sure you are on an open thread page and try again.");
return;
}
function escapeHtml(str) {
return (str || '').replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
}
// Inject print styles to isolate thread element during print
const styleEl = document.createElement('style');
styleEl.id = 'x-pdf-print-styles';
styleEl.textContent = `
@media print {
body > *:not(#x-pdf-print-container) {
display: none !important;
}
#x-pdf-print-container {
display: block !important;
position: absolute !important;
left: 0 !important;
top: 0 !important;
width: 100% !important;
background: #ffffff !important;
color: #0f1419 !important;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important;
padding: 20px !important;
box-sizing: border-box !important;
}
.x-pdf-post {
border: 1px solid #cfd9de !important;
border-radius: 12px !important;
padding: 16px !important;
margin-bottom: 12px !important;
page-break-inside: avoid !important;
}
.x-pdf-header {
display: flex !important;
align-items: center !important;
margin-bottom: 8px !important;
}
.x-pdf-avatar {
width: 40px !important;
height: 40px !important;
border-radius: 50% !important;
margin-right: 12px !important;
}
.x-pdf-name {
font-weight: bold !important;
font-size: 15px !important;
}
.x-pdf-handle {
color: #536471 !important;
font-size: 14px !important;
margin-left: 6px !important;
}
.x-pdf-text {
font-size: 15px !important;
line-height: 1.5 !important;
white-space: pre-wrap !important;
word-break: break-word !important;
}
.x-pdf-date {
color: #536471 !important;
font-size: 12px !important;
margin-top: 8px !important;
}
}
@media screen {
#x-pdf-print-container {
display: none !important;
}
}
`;
document.head.appendChild(styleEl);
// Build the print overlay container directly in the current document
const printContainer = document.createElement('div');
printContainer.id = 'x-pdf-print-container';
printContainer.innerHTML = `
<h1 style="font-size: 20px; border-bottom: 2px solid #eff3f4; padding-bottom: 10px; margin-bottom: 16px;">
Exported X Thread (${posts.length} posts captured)
</h1>
${posts.map(p => `
<div class="x-pdf-post">
<div class="x-pdf-header">
${p.avatar ? `<img class="x-pdf-avatar" src="${p.avatar}" />` : ''}
<div>
<span class="x-pdf-name">${escapeHtml(p.name)}</span>
<span class="x-pdf-handle">@${escapeHtml(p.handle)}</span>
</div>
</div>
<div class="x-pdf-text">${escapeHtml(p.text)}</div>
<div class="x-pdf-date">${p.created_at}</div>
</div>
`).join('')}
`;
document.body.appendChild(printContainer);
statusDiv.remove();
// Trigger system print dialog directly
window.print();
// Clean up print DOM elements after print dialog closes
setTimeout(() => {
printContainer.remove();
styleEl.remove();
}, 1000);
}