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

Function postGetUrl

scripts/insta_injectDownloadBtn.js:455–535  ·  view source on GitHub ↗
(target, articleNode)

Source from the content-addressed store, hash-verified

453 }
454
455 async function postGetUrl(target, articleNode) {
456 // meta[property="og:video"]
457 let list = articleNode.querySelectorAll("li[style][class]");
458 let url = null;
459 let mediaIndex = 0;
460 if (list.length === 0) {
461 // single img or video
462 if (!disableNewUrlFetchMethod)
463 url = await getUrlFromInfoApi(articleNode);
464 if (url === null) {
465 let videoElem = articleNode.querySelector("video");
466 if (videoElem) {
467 // media type is video
468 url = videoElem.getAttribute("src");
469 if (videoElem.hasAttribute("videoURL")) {
470 url = videoElem.getAttribute("videoURL");
471 } else if (url === null || url.includes("blob")) {
472 url = await fetchVideoURL(articleNode, videoElem);
473 }
474 } else if (
475 articleNode.querySelector("article div[role] div > img")
476 ) {
477 // media type is image
478 url = articleNode
479 .querySelector("article div[role] div > img")
480 .getAttribute("src");
481 } else {
482 console.log("Err: not find media at handle post single");
483 }
484 }
485 } else {
486 // multiple imgs or videos
487 const postView = location.pathname.startsWith("/p/");
488 let dotsElements = [...articleNode.querySelectorAll(`div._acnb`)];
489 mediaIndex = [...dotsElements].reduce(
490 (result, element, index) =>
491 element.classList.length === 2 ? index : result,
492 null
493 );
494 if (mediaIndex === null) throw "Cannot find the media index";
495
496 if (!disableNewUrlFetchMethod)
497 url = await getUrlFromInfoApi(articleNode, mediaIndex);
498 if (url === null) {
499 const listElements = [
500 ...articleNode.querySelectorAll(
501 `:scope > div > div:nth-child(${
502 postView ? 1 : 2
503 }) > div > div:nth-child(1) ul li[style*="translateX"]`
504 ),
505 ];
506 const listElementWidth = Math.max(
507 ...listElements.map((element) => element.clientWidth)
508 );
509
510 const positionsMap = listElements.reduce((result, element) => {
511 const position = Math.round(
512 Number(element.style.transform.match(/-?(\d+)/)[1]) /

Callers 2

postOnMouseInFunction · 0.85
postOnClickedFunction · 0.85

Calls 2

getUrlFromInfoApiFunction · 0.85
fetchVideoURLFunction · 0.85

Tested by

no test coverage detected