-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
133 lines (125 loc) · 5.19 KB
/
script.js
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
document.addEventListener("DOMNodeInserted", checkForStoriesPage, false);
let hasFired = false;
let hasCompletedFetching = false;
function checkForStoriesPage() {
if (!window._sharedData) {
return;
}
const pathname = window.location.pathname;
const username = window._sharedData.config.viewer.username;
const mediaId = pathname.split('/')[3];
if (pathname.includes('/stories/' + username) && !hasFired) {
hasFired = true;
removeBox();
getAllViewers(mediaId);
} else if (!pathname.includes('/stories/' + username)) {
removeBox();
} else if (pathname.includes('/stories/' + username) && hasFired && mediaId != window.currentMediaId) {
removeBox();
getAllViewers(mediaId);
}
}
function removeBox() {
const box = document.querySelector('.story-box');
if (!box)
return;
box.remove();
hasFired = false;
}
function getAllViewers(mediaId, maxId = null) {
if (!window._storyViewers) {
window._storyViewers = [];
}
if (window.currentMediaId != mediaId) {
hasCompletedFetching = false;
window.currentMediaId = mediaId;
}
const storyIndexInWindow = window._storyViewers.findIndex(media => media.mediaId == mediaId);
if (storyIndexInWindow > -1 && hasCompletedFetching) {
addBox();
return;
}
const csrfToken = window._sharedData.config.csrf_token;
fetch(`https://i.instagram.com/api/v1/media/${mediaId}/list_reel_media_viewer/`, {
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9,fa;q=0.8",
"content-type": "application/x-www-form-urlencoded",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"x-asbd-id": "198387",
"x-csrftoken": csrfToken,
"x-ig-app-id": "1217981644879628",
},
"referrer": "https://www.instagram.com/",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": `include_blacklist_sample=true&${maxId ? 'max_id=' + maxId : ''}`,
"method": "POST",
"mode": "cors",
"credentials": "include"
}).then(res => res.json()).then(res => {
if (storyIndexInWindow > -1) {
window._storyViewers[storyIndexInWindow].users = [...window._storyViewers[storyIndexInWindow].users, ...res.users];
} else {
window._storyViewers.push({ users: [...res.users], mediaId: mediaId });
}
if (res.next_max_id) {
setTimeout(() => getAllViewers(mediaId, res.next_max_id), 50);
} else {
hasCompletedFetching = true;
addBox();
}
});
}
function addBox() {
const currentStoryInfo = window._storyViewers.find(story => story.mediaId == window.currentMediaId);
const box = document.createElement('div');
box.classList.add('story-box');
const input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('placeholder', 'Search');
input.setAttribute('id', 'story-search-input');
input.addEventListener('keyup', searchInViewers);
input.classList.add('story-search-input');
box.appendChild(input);
const count = document.createElement('span');
count.classList.add('story-viewers-count');
count.setAttribute('id', 'story-viewers-count');
count.innerHTML = currentStoryInfo.users.length + ' viewers';
box.appendChild(count);
const list = document.createElement('ul');
list.setAttribute('id', 'story-viewers-list');
box.appendChild(list);
currentStoryInfo.users.slice(0, 100).forEach(user => {
const li = createUserElement(user);
list.appendChild(li);
});
document.body.appendChild(box);
};
function createUserElement(user) {
const li = document.createElement('li');
li.setAttribute('id', 'story-viewer-' + user.id);
li.innerHTML = `<div class="story-user"><a href="https://instagram.com/${user.username}" target="_blank" class="story-user-avatar"><img src="${user.profile_pic_url}"/></a><div class="story-user-body"><span>${user.full_name}</span><strong class="story-viewer-username">${user.username}</strong></div></div>`;
return li;
}
function searchInViewers() {
const currentStoryInfo = window._storyViewers.find(story => story.mediaId == window.currentMediaId);
const input = document.getElementById('story-search-input');
const text = input.value.toLowerCase();
const list = document.getElementById('story-viewers-list');
const count = document.getElementById('story-viewers-count');
list.innerHTML = '';
let users;
if (text.length > 1) {
users = currentStoryInfo.users.filter(user => user.username.toLowerCase().includes(text) || user.full_name.toLowerCase().includes(text));
count.innerHTML = users.length + ' viewers';
} else {
users = currentStoryInfo.users.slice(0, 100);
count.innerHTML = currentStoryInfo.users.length + ' viewers';
}
users.forEach(user => {
const li = createUserElement(user);
list.appendChild(li);
});
}