-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
195 lines (164 loc) · 6.01 KB
/
content.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
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
// hey this is my instagram follower tracker personal project!
// made by sulubsq
console.log('is this thing on?');
// this gets all the follower data
async function extractFollowerData() {
console.log('ok lets get the data...');
let followers = new Set();
let following = new Set();
try {
// first gotta get the user id
let userId = await getUserId();
if (!userId) {
throw new Error('couldnt find user id');
}
console.log('got the user id:', userId);
// now get followers
console.log('getting followers...');
let followerUsers = await fetchFollowers(userId);
followerUsers.forEach(user => followers.add(user));
console.log('got ' + followers.size + ' followers!');
// and following
console.log('getting following...');
let followingUsers = await fetchFollowing(userId);
followingUsers.forEach(user => following.add(user));
console.log('got ' + following.size + ' following!');
// send everything back
let data = {
type: 'FOLLOWER_DATA',
data: {
timestamp: new Date().toISOString(),
username: window.location.pathname.split('/')[1],
followers: Array.from(followers),
following: Array.from(following)
}
};
console.log('sending all the data back!');
chrome.runtime.sendMessage(data);
} catch (error) {
console.log('uh oh something went wrong:', error);
chrome.runtime.sendMessage({
type: 'ERROR',
error: error.message
});
}
}
// this gets the instagram user id
async function getUserId() {
// try to find it in the meta tags first
let idFromMeta = document.querySelector('meta[property="instapp:owner_user_id"]');
if (idFromMeta) {
return idFromMeta.content;
}
// maybe check the page data?
let pageData = window._sharedData || {};
let userData = pageData.entry_data?.ProfilePage?.[0]?.graphql?.user;
if (userData?.id) {
return userData.id;
}
// ok now lets try the api
let username = window.location.pathname.split('/')[1];
let response = await fetch(`https://www.instagram.com/api/v1/users/web_profile_info/?username=${username}`, {
headers: {
'x-ig-app-id': '936619743392459', // network tab thing
'x-requested-with': 'XMLHttpRequest'
}
});
if (!response.ok) {
throw new Error('couldnt get user id from api :(');
}
let data = await response.json();
return data.data?.user?.id;
}
// this gets all the followers
async function fetchFollowers(userId, after = null) {
let users = [];
let howMany = 50;
try {
let vars = {
id: userId,
first: howMany,
after: after
};
let response = await fetch(
`https://www.instagram.com/graphql/query/?query_hash=c76146de99bb02f6415203be841dd25a&variables=${encodeURIComponent(JSON.stringify(vars))}`,
{
headers: {
'x-requested-with': 'XMLHttpRequest'
}
}
);
if (!response.ok) {
throw new Error('couldnt get followers');
}
let data = await response.json();
let edges = data.data?.user?.edge_followed_by?.edges || [];
let pageInfo = data.data?.user?.edge_followed_by?.page_info;
// add all the usernames we got
users.push(...edges.map(edge => edge.node.username));
// if theres more, get those too
if (pageInfo?.has_next_page && pageInfo?.end_cursor) {
let moreUsers = await fetchFollowers(userId, pageInfo.end_cursor);
users.push(...moreUsers);
}
} catch (error) {
console.log('uh oh error getting followers:', error);
}
return users;
}
// this gets all the following
async function fetchFollowing(userId, after = null) {
let users = [];
let howMany = 50;
try {
let vars = {
id: userId,
first: howMany,
after: after
};
let response = await fetch(
`https://www.instagram.com/graphql/query/?query_hash=d04b0a864b4b54837c0d870b0e77e076&variables=${encodeURIComponent(JSON.stringify(vars))}`,
{
headers: {
'x-requested-with': 'XMLHttpRequest'
}
}
);
if (!response.ok) {
throw new Error('couldnt get following');
}
let data = await response.json();
let edges = data.data?.user?.edge_follow?.edges || [];
let pageInfo = data.data?.user?.edge_follow?.page_info;
users.push(...edges.map(edge => edge.node.username));
if (pageInfo?.has_next_page && pageInfo?.end_cursor) {
let moreUsers = await fetchFollowing(userId, pageInfo.end_cursor);
users.push(...moreUsers);
}
} catch (error) {
console.log('oops error getting following:', error);
}
return users;
}
// listen for messages from popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('got a message:', request);
if (request.action === 'EXTRACT_DATA') {
console.log('time to get the data!');
extractFollowerData();
sendResponse({status: 'started'});
} else if (request.action === 'CHECK_LOGIN') {
console.log('checking if logged in');
// check if we can see dm icon or profile stuff
let isLoggedIn = !!(
document.querySelector('a[href="/direct/inbox/"]') ||
document.querySelector('span[class*="_aaav"]') ||
document.querySelector('a[href*="/following"]') ||
document.querySelector('a[href*="/followers"]') ||
document.querySelector('svg[aria-label="Settings"]')
);
console.log('logged in?', isLoggedIn);
sendResponse({loggedIn: isLoggedIn});
}
return true;
});