This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
InstagramHelper.ts
604 lines (530 loc) · 23.2 KB
/
InstagramHelper.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
class InstagramHelper {
private UserId: string;
private UserCharParticipants: Array<ChatParticipant> = new Array<ChatParticipant>();
private AllMessagesItemsArray: Array<Item> = new Array<Item>();
private PurposePromptText: string = "What you want to do with the messages? \nA: Unsend \nB: Download \nC: Exit";
private ThreadIdPromptText: string = "Please enter your chat Thread ID.";
private SkipRecentXMessagesCountPromptText: string = "Please enter number of recent messages you want to skip. Default is 10.";
private DelayPromptText: string = "Please enter number of seconds to randomly wait between each message to delete. Default is 3 seconds";
private SkipTextMessagesPromptText: string = "Do you want to skip unsending text messages - yes/no? It means it will unsend media contents. Default is no.";
private LocalStorageKeys = {
instagramWebFBAppId: "instagramWebFBAppId",
instagramFBAppId: "instagramFBAppId",
instagramWebDesktopFBAppId: "instagramWebDesktopFBAppId",
igLiteAppId: "igLiteAppId"
};
private SessionStorageKeys = {
www_claim_v2: "www-claim-v2"
}
constructor() {
this.UserId = this.getCookie("ds_user_id");
this.storeAppIds();
console.info('Ready you can now continue');
this.askPrompts();
}
private async askPrompts() {
var purposePromptValue = prompt(this.PurposePromptText);
switch (purposePromptValue) {
case 'A' || 'a':
// Unsend
await this.startUnsending();
break;
case 'B' || 'b':
// Download
await this.downloadMessagesDetails();
break;
case 'C' || 'c':
return;
default:
alert("Please enter a valid Input. Bye");
throw new Error("Please enter a valid Input.");
}
}
private async waitTimeout(ms: number) {
console.warn("Waiting for " + ms.toString() + " milliseconds");
return new Promise((resolve, reject) => {
setTimeout(() => {
console.warn("Done waiting");
resolve(ms)
}, ms)
})
}
/**
* generates random integer between 1 and provided (maxNumber including)
* @param {number} maxNumber max number between which to generate random integer (default value is 10)
*/
private getRandomIntegerBetween(maxNumber: number = 10) {
if (maxNumber != undefined && maxNumber != null && maxNumber > 0) {
// its provided
} else {
maxNumber = 10;
}
return Math.floor((Math.random() * maxNumber) + 1);
}
/**
* provides cookie value if exists else provides empty string
* @param {string} c_name cookie key name
*/
private getCookie(c_name: string): string {
if (document.cookie.length > 0) {
var c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
var c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
var c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
/**
* App Ids Stored from this url : https://static.cdninstagram.com/rsrc.php/v3iyjp4/yO/l/en_US/DRdIhWKQvFs.js?
*/
private storeAppIds() {
// Source https://static.cdninstagram.com/rsrc.php/v3iyjp4/yO/l/en_US/DRdIhWKQvFs.js
localStorage.setItem(this.LocalStorageKeys.instagramFBAppId, "124024574287414");
localStorage.setItem(this.LocalStorageKeys.instagramWebFBAppId, "1217981644879628");
localStorage.setItem(this.LocalStorageKeys.instagramWebDesktopFBAppId, "389801252");
localStorage.setItem(this.LocalStorageKeys.igLiteAppId, "");
}
/**
* provides the ConsumerLibCommon Js File
* @param {string} jsFileName default is 4f7f1faf9a94.js
* @deprecated The method should not be used, use storeAppIds()
*/
private async getConsumerLibCommonsJs(jsFileName: string = "4f7f1faf9a94.js") {
let consumerLibCommonsJsRequestUrl = "https://www.instagram.com/static/bundles/es6/ConsumerLibCommons.js/" + jsFileName;
let consumerLibCommonsJsRequestInit: RequestInit = {
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"pragma": "no-cache",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1"
},
"referrerPolicy": "no-referrer-when-downgrade",
"body": null,
"method": "GET",
mode: "cors",
credentials: "include"
};
await fetch(
consumerLibCommonsJsRequestUrl,
consumerLibCommonsJsRequestInit
)
.then((response) => {
if (response.status != 200) {
throw new Error("Try again tomorrow");
} else {
return response.text();
}
})
.then((data) => {
if (data) {
let appIdVariableNames = [
this.LocalStorageKeys.instagramFBAppId,
this.LocalStorageKeys.instagramWebFBAppId,
this.LocalStorageKeys.instagramWebDesktopFBAppId,
this.LocalStorageKeys.igLiteAppId
];
appIdVariableNames.forEach(singleppIdVariableName => {
let searchString = singleppIdVariableName + "='";
let indexOfSearchedStringVariableName = data.indexOf(searchString);
let valueData = data.substring(indexOfSearchedStringVariableName + searchString.length);
let singleAppIdValue = valueData.substring(0, valueData.indexOf("'"));
// console.warn(singleppIdVariableName + " = " + singleAppIdValue);
localStorage.setItem(singleppIdVariableName, singleAppIdValue);
});
} else {
throw new Error("getConsumerLibCommonsJs not found");
}
});
}
/**
* Sends HTTP Request to Get Messages
*/
private async getMessagesHttpRequest(threadId: string, oldestCursor: string = ''): Promise<GetMessagesResponseModel> {
var threadLink = "https://www.instagram.com/direct/t/" + threadId.toString();
var getMessageAPIUrl = "https://i.instagram.com/api/v1/direct_v2/threads/" + threadId.toString() + "/";
if (oldestCursor != undefined && oldestCursor != null && oldestCursor.length > 0) {
getMessageAPIUrl = getMessageAPIUrl + "?cursor=" + oldestCursor + "";
}
var getMessagesRequestInit: RequestInit = {
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"pragma": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
"x-ig-app-id": localStorage.getItem(this.LocalStorageKeys.instagramWebFBAppId)?.toString() ?? '',
"x-ig-www-claim": sessionStorage.getItem(this.SessionStorageKeys.www_claim_v2)?.toString() ?? '',
},
"referrer": threadLink,
"referrerPolicy": "no-referrer-when-downgrade",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "include",
};
console.info("Getting Messages...");
return await fetch(
getMessageAPIUrl,
getMessagesRequestInit
).then(async (value: Response) => {
if (value.status != 200) {
throw new Error("Try again tomorrow");
} else {
return await value.json().then(response => {
if (response) {
return response as GetMessagesResponseModel;
} else {
throw new Error("Failed to get messages response");
}
});
}
});
}
/**
* Sends a delete message HTTP Request for a single message from a thread
* @param threadId string
* @param messageItemId string
* @returns void
*/
private async deleteMessageHttpRequest(threadId: string, messageItemId: string): Promise<Response> {
var threadLink = "https://www.instagram.com/direct/t/" + threadId.toString();
var deleteMessageAPIUrl = "https://i.instagram.com/api/v1/direct_v2/threads/" + threadId.toString() + "/items/" + messageItemId + "/delete/";
var deleteMessageRequestInit: RequestInit = {
"credentials": "include",
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded",
"pragma": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-csrftoken": this.getCookie("csrftoken"),
"x-ig-app-id": localStorage.getItem(this.LocalStorageKeys.instagramWebFBAppId)?.toString() ?? '',
"x-ig-www-claim": sessionStorage.getItem(this.SessionStorageKeys.www_claim_v2)?.toString() ?? '',
"x-requested-with": "XMLHttpRequest"
},
"referrer": threadLink,
"referrerPolicy": "no-referrer-when-downgrade",
"body": null,
"method": "POST",
"mode": "cors"
};
console.info("Unsending Message...");
return await fetch(
deleteMessageAPIUrl,
deleteMessageRequestInit
).then((response) => {
if (response.status >= 200 && response.status < 300) {
console.info("Deleted Message");
return response;
} else {
throw new Error("Try again tomorrow");
}
}).catch((error) => {
throw new Error(error);
});
}
/**
* Start Unsending messages
* @param startUnsendingRequest request model
*/
private async unsend(startUnsendingRequest: StartUnsendingRequest) {
if (startUnsendingRequest.thread_id == null || startUnsendingRequest.thread_id == undefined || startUnsendingRequest.thread_id.length < 1) {
startUnsendingRequest.thread_id = window.location.href.split('/')[5]; // Get the chat id automatically from the url, make sure a chat is currently active
console.warn("Starting deleting from thread Id : " + startUnsendingRequest.thread_id.toString());
}
if (startUnsendingRequest.skip_recent_x_messages_count < 2) {
throw new Error("skipRecentXMessagesCount must be greater than 2");
}
var skipItemTypesList = new Array<string>();
skipItemTypesList.push("video_call_event"); // Default Items which need to be skipped
if (startUnsendingRequest.skip_text_messages) {
skipItemTypesList.push("text");
}
var toSkippMessagesCount = 0;
console.warn("Inevitable");
var oldestCursor = "";
var prevCursor = "";
// if its first message ever sent then stop else continue
while (prevCursor != "MINCURSOR") {
var getMessagesResponse = await this.getMessagesHttpRequest(startUnsendingRequest.thread_id, oldestCursor);
let itemIdsToDelete: Array<string> = new Array<string>();
getMessagesResponse?.thread?.items.forEach(element => {
if (element?.user_id?.toString() == this.UserId.toString()) {
// Skipping those types of messages
if (element?.item_type && !skipItemTypesList.includes(element?.item_type?.toString())) {
if (element?.item_id?.toString()) {
if (!itemIdsToDelete.includes(element?.item_id?.toString())) {
itemIdsToDelete.push(element?.item_id?.toString());
}
}
}
}
});
//#region Deleting Messages
for (let itemIdIndex = 0; itemIdIndex < itemIdsToDelete.length; itemIdIndex++) {
const messageItemId = itemIdsToDelete[itemIdIndex];
if (toSkippMessagesCount < startUnsendingRequest.skip_recent_x_messages_count) {
toSkippMessagesCount = toSkippMessagesCount + 1;
continue;
}
await this.deleteMessageHttpRequest(startUnsendingRequest.thread_id, messageItemId);
await this.waitTimeout(this.getRandomIntegerBetween(startUnsendingRequest.delay));
}
//#endregion Deleting Messages
oldestCursor = getMessagesResponse?.thread?.oldest_cursor ?? '';
prevCursor = getMessagesResponse?.thread?.prev_cursor ?? '';
}
console.warn("All messages deleted.");
}
/**
* Start Unsending messages with prompt
*/
public async startUnsending() {
// Asking Prompts
var thread_id = '';
var threadIdPromptValue = prompt(this.ThreadIdPromptText);
if (threadIdPromptValue != null && threadIdPromptValue != undefined && threadIdPromptValue.length > 0) {
thread_id = threadIdPromptValue;
} else {
alert("Please enter a valid chat Thread ID. Bye");
throw new Error("Please enter a valid chat Thread ID");
}
var skip_recent_x_messages_count = 10;
var skipXMessagesPromptValue = prompt(this.SkipRecentXMessagesCountPromptText);
if (skipXMessagesPromptValue != null && skipXMessagesPromptValue != undefined && parseInt(skipXMessagesPromptValue)) {
skip_recent_x_messages_count = parseInt(skipXMessagesPromptValue);
}
var delay = 3;
var delayPromptValue = prompt(this.DelayPromptText);
if (delayPromptValue != null && delayPromptValue != undefined && parseInt(delayPromptValue)) {
delay = parseInt(delayPromptValue);
}
var skip_text_messages = false;
var skipTextMessagesPromptValue = prompt(this.SkipTextMessagesPromptText);
if (skipTextMessagesPromptValue != null && skipTextMessagesPromptValue != undefined && skipTextMessagesPromptValue.toLowerCase()[0] == "y") {
skip_text_messages = true;
}
// Building Request
var startUnsendingRequest: StartUnsendingRequest = {
thread_id: thread_id,
skip_recent_x_messages_count: skip_recent_x_messages_count,
skip_text_messages: skip_text_messages,
delay: delay * 1000,
}
// Starting Unsending
await this.unsend(startUnsendingRequest);
}
/**
* Gets all the messsages and stores into a global array.
*/
private async getAllMessagesData(threadId: string) {
if (threadId == null || threadId == undefined) {
threadId = window.location.href.split('/')[5]; // Get the chat id automatically from the url, make sure a chat is currently active
console.warn("Starting getting messages from thread Id : " + threadId.toString());
}
var oldestCursor = "";
var prevCursor = "";
// if its first message ever sent then stop else continue
while (prevCursor != "MINCURSOR") {
var getMessagesResponse = await this.getMessagesHttpRequest(threadId, oldestCursor);
//#region Own Inviter Adding to Participants
let isChatParticipantAlreadyExists = false;
for (let index = 0; index < this.UserCharParticipants.length; index++) {
const singleUserChatParticipant = this.UserCharParticipants[index];
if (singleUserChatParticipant.pk == getMessagesResponse?.thread?.inviter?.pk) {
isChatParticipantAlreadyExists = true;
break;
}
}
if (!isChatParticipantAlreadyExists) {
if (getMessagesResponse?.thread?.inviter?.pk
&&
getMessagesResponse?.thread?.inviter?.username
&&
getMessagesResponse?.thread?.inviter?.full_name
&&
getMessagesResponse?.thread?.inviter?.profile_pic_url
)
this.UserCharParticipants.push(
new ChatParticipant(
getMessagesResponse.thread.inviter.pk,
getMessagesResponse.thread.inviter.username,
getMessagesResponse.thread.inviter.profile_pic_url,
getMessagesResponse.thread.inviter.full_name
)
);
}
//#endregion
//#region Thread Participants adding
getMessagesResponse?.thread?.users?.forEach(element => {
let isChatParticipantAlreadyExists = false;
for (let index = 0; index < this.UserCharParticipants.length; index++) {
const singleUserChatParticipant = this.UserCharParticipants[index];
if (singleUserChatParticipant.pk == element?.pk) {
isChatParticipantAlreadyExists = true;
break;
}
}
if (!isChatParticipantAlreadyExists) {
this.UserCharParticipants.push(
new ChatParticipant(
element.pk,
element.username,
element.profile_pic_url,
element.full_name
)
);
}
});
//#endregion
//#region Adding Messages
getMessagesResponse?.thread?.items?.forEach(element => {
var isExists = false;
for (let index = 0; index < this.AllMessagesItemsArray.length; index++) {
const existingElement = this.AllMessagesItemsArray[index];
if (existingElement?.item_id?.toString() == element?.item_id?.toString()) {
isExists = true;
break;
}
}
if (!isExists) {
this.AllMessagesItemsArray.push(element);
}
});
//#endregion
oldestCursor = getMessagesResponse?.thread?.oldest_cursor ?? '';
prevCursor = getMessagesResponse?.thread?.prev_cursor ?? '';
}
console.warn("All Messages fetched, No More messages to fetch");
}
/**
* Download All Messages Details JSON
*/
public async downloadMessagesDetails() {
// Asking Prompts
var thread_id = '';
var threadIdPromptValue = prompt(this.ThreadIdPromptText);
if (threadIdPromptValue != null && threadIdPromptValue != undefined && threadIdPromptValue.length > 0) {
thread_id = threadIdPromptValue;
} else {
alert("Please enter a valid chat Thread ID. Bye");
throw new Error("Please enter a valid chat Thread ID");
}
await this.getAllMessagesData(thread_id);
await this.downloadJsonFile(
{
"myUserId": this.UserId,
"allMessagesItemsArray": this.AllMessagesItemsArray,
"usersChatParticipants": this.UserCharParticipants,
},
"InstaGramHelperChatMessagesData"
);
}
/**
* Downloads JSON file from Object in Browser
* @param {*} jsonDataObject Json Object
* @param {string} fileNameExcludingExtension
*/
private async downloadJsonFile(jsonDataObject: any, fileNameExcludingExtension: string = 'data') {
const blob = new Blob(['\ufeff' + JSON.stringify(jsonDataObject)], {
type: 'text/json;charset=utf-8;'
});
const dwldLink = document.createElement('a');
const url = URL.createObjectURL(blob);
const isSafariBrowser = navigator.userAgent.indexOf('Safari') !== -1 &&
navigator.userAgent.indexOf('Chrome') === -1;
if (isSafariBrowser) {
// for safari
dwldLink.setAttribute('target', '_blank');
}
dwldLink.setAttribute('href', url);
dwldLink.setAttribute('download', fileNameExcludingExtension + '.json');
dwldLink.style.visibility = 'hidden';
document.body.appendChild(dwldLink);
dwldLink.click();
document.body.removeChild(dwldLink);
}
/**
* Deletes all the follow requests for private account
* @param {string} deleteButtonCSSClassName | provide the css class name of Delete button by Inspect Element
*/
public async deleteAllFollowRequests(deleteButtonCSSClassName: string) {
let t = document.getElementsByClassName(deleteButtonCSSClassName);
for (let index = 0; index < t.length; index++) {
const element = t[index];
await this.waitTimeout(1000)
if (element instanceof HTMLElement) {
element.click();
}
}
}
}
class GetMessagesResponseModel {
thread?: Thread
status?: string
}
class Thread {
items: Item[] = [];
oldest_cursor?: string
newest_cursor?: string
next_cursor?: string
prev_cursor?: string
thread_title?: string
inviter?: {
full_name: string
pk: string;
username: string;
profile_pic_url: string;
}
users?: Array<{
full_name: string
pk: string;
username: string;
profile_pic_url: string;
}>
}
class Item {
item_id?: string
user_id?: string
item_type?: string
}
class StartUnsendingRequest {
thread_id: string;
skip_recent_x_messages_count: number = 10;
delay: number = 3500;
skip_text_messages: boolean = false;
constructor(thread_id: string, skip_recent_x_messages_count: number, delay: number, skip_text_messages: boolean) {
this.thread_id = thread_id;
this.skip_recent_x_messages_count = skip_recent_x_messages_count;
this.delay = delay;
this.skip_text_messages = skip_text_messages;
}
}
class ChatParticipant {
pk: string;
username: string;
profile_pic_url: string;
full_name: string;
constructor(pk: string, username: string, profile_pic_url: string, full_name: string) {
this.pk = pk;
this.username = username;
this.profile_pic_url = profile_pic_url;
this.full_name = full_name;
}
}