-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtwitter.ts
285 lines (221 loc) · 7.51 KB
/
twitter.ts
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const Q = {
rootPath: "//h1[@role='heading' and @aria-level='1']/following-sibling::div[@aria-label]//div[@style]",
anchor: ".//article",
childMatchSelect: "string(.//article//a[starts-with(@href, '/') and @aria-label]/@href)",
childMatch: "child::div[.//a[@href='$1']]",
expand: ".//div[@role='button' and not(@aria-haspopup) and not(@data-testid)]",
quote: ".//div[@role='blockquote' and @aria-haspopup='false']",
image: ".//a[@role='link' and starts-with(@href, '/') and contains(@href, '/photo/')]",
imageFirstNext: "//div[@aria-roledescription='carousel']/div[2]/div[1]//div[@role='button']",
imageNext: "//div[@aria-roledescription='carousel']/div[2]/div[2]//div[@role='button']",
imageClose: "//div[@role='presentation']/div[@role='button' and @aria-label]",
backButton: "//div[@data-testid='titleContainer']//div[@role='button']",
viewSensitive: ".//a[@href='/settings/content_you_see']/parent::div/parent::div/parent::div//div[@role='button']",
progress: ".//*[@role='progressbar']",
promoted: ".//div[data-testid='placementTracking']",
};
export class TwitterTimelineBehavior {
seenTweets: Set<any>;
seenMediaTweets: Set<any>;
static id = "Twitter"
static isMatch() {
return !!window.location.href.match(/https:\/\/(www\.)?(x|twitter)\.com\//);
}
static init() {
return {
state: {
tweets: 0,
images: 0,
videos: 0
},
opts: {
maxDepth: 0
}
};
}
constructor() {
this.seenTweets = new Set();
this.seenMediaTweets = new Set();
}
showingProgressBar(ctx, root) {
const { xpathNode } = ctx.Lib;
const node = xpathNode(Q.progress, root);
if (!node) {
return false;
}
// return false is hidden / no-height
return node.clientHeight > 10;
}
async waitForNext(ctx, child) {
const { sleep, waitUnit } = ctx.Lib;
if (!child) {
return null;
}
await sleep(waitUnit * 2);
if (!child.nextElementSibling) {
return null;
}
while (this.showingProgressBar(ctx, child.nextElementSibling)) {
await sleep(waitUnit);
}
return child.nextElementSibling;
}
async expandMore(ctx, child) {
const { sleep, waitUnit, xpathNode } = ctx.Lib;
const expandElem = xpathNode(Q.expand, child);
if (!expandElem) {
return child;
}
const prev = child.previousElementSibling;
expandElem.click();
await sleep(waitUnit);
while (this.showingProgressBar(ctx, prev.nextElementSibling)) {
await sleep(waitUnit);
}
child = prev.nextElementSibling;
return child;
}
async* infScroll(ctx) {
const { scrollIntoView, RestoreState, sleep, waitUnit, xpathNode } = ctx.Lib;
let root = xpathNode(Q.rootPath);
if (!root) {
return;
}
let child = root.firstElementChild;
if (!child) {
return;
}
while (child) {
let anchorElem = xpathNode(Q.anchor, child);
if (!anchorElem && Q.expand) {
child = await this.expandMore(ctx, child);
anchorElem = xpathNode(Q.anchor, child);
}
if (child && child.innerText) {
scrollIntoView(child);
}
if (child && anchorElem) {
await sleep(waitUnit);
const restorer = new RestoreState(Q.childMatchSelect, child);
yield anchorElem;
if (restorer.matchValue) {
child = await restorer.restore(Q.rootPath, Q.childMatch);
}
}
child = await this.waitForNext(ctx, child);
}
}
async* mediaPlaying(ctx, tweet) {
const { getState, sleep, xpathNode, xpathString } = ctx.Lib;
const media = xpathNode("(.//video | .//audio)", tweet);
if (!media || media.paused) {
return;
}
let mediaTweetUrl = null;
try {
mediaTweetUrl = new URL(xpathString(Q.childMatchSelect, tweet.parentElement), window.location.origin).href;
} catch (e) {
console.warn(e);
}
// no need to wait for mp4s, should be loaded fully automatically
if (media.src.startsWith("https://") && media.src.indexOf(".mp4") > 0) {
yield getState(ctx, `Loading video for ${mediaTweetUrl || "unknown"}`, "videos");
return;
}
let msg;
if (mediaTweetUrl) {
if (this.seenMediaTweets.has(mediaTweetUrl)) {
return;
}
msg = `Waiting for media playback for ${mediaTweetUrl} to finish`;
this.seenMediaTweets.add(mediaTweetUrl);
} else {
msg = "Loading video";
}
yield getState(ctx, msg, "videos");
const p = new Promise((resolve) => {
media.addEventListener("ended", () => resolve(null));
media.addEventListener("abort", () => resolve(null));
media.addEventListener("error", () => resolve(null));
media.addEventListener("pause", () => resolve(null));
});
await Promise.race([p, sleep(60000)]);
}
async* clickImages(ctx, tweet) {
const { getState, HistoryState, sleep, waitUnit, xpathNode } = ctx.Lib;
const imagePopup = xpathNode(Q.image, tweet);
if (imagePopup) {
const imageState = new HistoryState(() => imagePopup.click());
yield getState(ctx, "Loading Image: " + window.location.href, "images");
await sleep(waitUnit * 5);
let nextImage = xpathNode(Q.imageFirstNext);
let prevLocation = window.location.href;
while (nextImage) {
nextImage.click();
await sleep(waitUnit * 2);
if (window.location.href === prevLocation) {
await sleep(waitUnit * 5);
break;
}
prevLocation = window.location.href;
yield getState(ctx, "Loading Image: " + window.location.href, "images");
await sleep(waitUnit * 5);
nextImage = xpathNode(Q.imageNext);
}
await imageState.goBack(Q.imageClose);
}
}
async* clickTweet(ctx, tweet, depth) {
const { getState, HistoryState, sleep, waitUnit } = ctx.Lib;
const tweetState = new HistoryState(() => tweet.click());
await sleep(waitUnit);
if (tweetState.changed) {
yield getState(ctx, "Capturing Tweet: " + window.location.href, "tweets");
const maxDepth = ctx.opts.maxDepth;
if (depth < maxDepth && !this.seenTweets.has(window.location.href)) {
yield* this.iterTimeline(ctx, depth + 1);
}
this.seenTweets.add(window.location.href);
// wait
await sleep(waitUnit * 2);
await tweetState.goBack(Q.backButton);
await sleep(waitUnit);
}
}
async* iterTimeline(ctx, depth = 0) {
const { getState, sleep, waitUnit, xpathNode } = ctx.Lib;
if (this.seenTweets.has(window.location.href)) {
return;
}
yield getState(ctx, "Capturing thread: " + window.location.href, "threads");
// iterate over infinite scroll of tweets
for await (const tweet of this.infScroll(ctx)) {
// skip promoted tweets
if (xpathNode(Q.promoted, tweet)) {
continue;
}
await sleep(waitUnit * 2.5);
const viewButton = xpathNode(Q.viewSensitive, tweet);
if (viewButton) {
viewButton.click();
await sleep(waitUnit * 2.5);
}
// process images
yield* this.clickImages(ctx, tweet);
// process quoted tweet
const quoteTweet = xpathNode(Q.quote, tweet);
if (quoteTweet) {
yield* this.clickTweet(ctx, quoteTweet, 1000);
}
// await any video or audio
yield* this.mediaPlaying(ctx, tweet);
// track location to see if click goes to new url
yield* this.clickTweet(ctx, tweet, depth);
// wait before continuing
await sleep(waitUnit * 5);
}
}
async* run(ctx) {
yield* this.iterTimeline(ctx, 0);
}
}