MCPcopy Create free account
hub / github.com/Useful-Scripts-Extension/useful-script / findMediaId

Function findMediaId

scripts/insta_injectDownloadBtn.js:571–630  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

569 }
570
571 async function findMediaId() {
572 // method 1: extract from url.
573 function method1() {
574 let href = window.location.href;
575 let match = href.match(
576 /www.instagram.com\/stories\/[^\/]+\/(\d+)/
577 );
578 if (!href.includes("highlights") && match) return match[1];
579 }
580
581 // method 3
582 async function method3() {
583 let postId = await findPostId(articleNode);
584 if (!postId) {
585 return null;
586 }
587
588 if (!(postId in mediaIdCache)) {
589 let postUrl = `https://www.instagram.com/p/${postId}/`;
590 let resp = await fetch(postUrl);
591 let text = await resp.text();
592 let idMatch = text ? text.match(mediaIdPattern) : [];
593 let mediaId = null;
594 for (let i = 0; i < idMatch.length; ++i) {
595 if (idMatch[i]) mediaId = idMatch[i];
596 }
597 if (!mediaId) return null;
598 mediaIdCache[postId] = mediaId;
599 }
600 return mediaIdCache[postId];
601 }
602
603 function method2() {
604 let scriptJson = document.querySelectorAll(
605 'script[type="application/json"]'
606 );
607 for (let i = 0; i < scriptJson.length; i++) {
608 let match = scriptJson[i].text.match(
609 /"pk":"(\d+)","id":"[\d_]+"/
610 );
611 if (match) {
612 if (!window.location.href.includes("highlights")) {
613 return match[1];
614 }
615 let matchs = Array.from(
616 scriptJson[i].text.matchAll(
617 /"pk":"(\d+)","id":"[\d_]+"/g
618 ),
619 (match) => match[1]
620 );
621 const matchIndex = findHighlightsIndex();
622 if (matchs.length > matchIndex) {
623 return matchs[matchIndex];
624 }
625 }
626 }
627 }
628

Callers 1

getUrlFromInfoApiFunction · 0.85

Calls 3

method1Function · 0.85
method3Function · 0.85
method2Function · 0.85

Tested by

no test coverage detected