Skip to content

Commit bb274ee

Browse files
committed
Click "Show More Posts from" button
1 parent b002180 commit bb274ee

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/api/api.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,11 @@ export function createApi(
176176
*/
177177
export class Hashtag<T> extends Instagram<T> {
178178
constructor(id: string, options: IOptions = {}) {
179-
const endpoint = "https://instagram.com/explore/tags/[id]";
180-
const pageQuery = "data.hashtag.edge_hashtag_to_media.page_info";
181-
const edgeQuery = "data.hashtag.edge_hashtag_to_media.edges";
182179
super(
183-
endpoint,
180+
"https://instagram.com/explore/tags/[id]",
184181
id,
185-
pageQuery,
186-
edgeQuery,
182+
"data.hashtag.edge_hashtag_to_media.page_info",
183+
"data.hashtag.edge_hashtag_to_media.edges",
187184
options,
188185
getPageValidator(options),
189186
);
@@ -194,15 +191,34 @@ export class Hashtag<T> extends Instagram<T> {
194191
* An Instagram user API wrapper
195192
*/
196193
export class User<T> extends Instagram<T> {
194+
defaultPageFunctions = [
195+
() => {
196+
let morePostsIntervalCounter = 0;
197+
const morePostsInterval = setInterval(() => {
198+
const searchDiv = Array.from(
199+
document.getElementsByTagName("div"),
200+
).filter((d) =>
201+
d.innerHTML.startsWith("Show More Posts from"),
202+
)[0];
203+
204+
morePostsIntervalCounter++;
205+
206+
if (searchDiv !== undefined) {
207+
searchDiv.parentElement.parentElement.click();
208+
clearInterval(morePostsInterval);
209+
} else if (morePostsIntervalCounter > 10) {
210+
clearInterval(morePostsInterval);
211+
}
212+
}, 1000);
213+
},
214+
];
215+
197216
constructor(id: string, options: IOptions = {}) {
198-
const endpoint = "https://instagram.com/[id]";
199-
const pageQuery = "data.user.edge_owner_to_timeline_media.page_info";
200-
const edgeQuery = "data.user.edge_owner_to_timeline_media.edges";
201217
super(
202-
endpoint,
218+
"https://instagram.com/[id]",
203219
id,
204-
pageQuery,
205-
edgeQuery,
220+
"data.user.edge_owner_to_timeline_media.page_info",
221+
"data.user.edge_owner_to_timeline_media.edges",
206222
options,
207223
getPageValidator(options),
208224
);

src/api/instagram.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ export class Instagram<PostType> {
100100
// Logging object
101101
public logger: winston.Logger;
102102

103+
// Implementation-specific page functions
104+
public defaultPageFunctions: (() => void)[] = [];
105+
103106
// Validations
104107
private readonly strict: boolean = false;
105108
private readonly validator: Type<unknown>;
@@ -738,6 +741,11 @@ export class Instagram<PostType> {
738741
try {
739742
await this.page.goto(this.url);
740743

744+
// Run defaultPagePlugins
745+
for (const f of this.defaultPageFunctions) {
746+
await this.page.evaluate(f);
747+
}
748+
741749
// Fix issue with disabled scrolling
742750
/* istanbul ignore next */
743751
await this.page.evaluate(() => {

0 commit comments

Comments
 (0)