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

Function storyOnClicked

scripts/insta_injectDownloadBtn.js:765–809  ·  view source on GitHub ↗
(target)

Source from the content-addressed store, hash-verified

763 }
764
765 async function storyOnClicked(target) {
766 // extract url from target story and download or open it
767 let sectionNode = storyGetSectionNode(target);
768 let url = await storyGetUrl(target, sectionNode);
769 const posterUrlPat = /\/stories\/(.*)\/.*\//;
770 // download or open media url
771 if (target.getAttribute("class").includes("download-btn")) {
772 let mediaName = url
773 .split("?")[0]
774 .split("\\")
775 .pop()
776 .split("/")
777 .pop();
778 mediaName = mediaName.substring(0, mediaName.lastIndexOf("."));
779 let datetime = new Date(
780 sectionNode.querySelector("time").getAttribute("datetime")
781 );
782 let posterName = "unkown";
783 // method 1
784 const posterNameHeader = sectionNode.querySelector("header a");
785 if (posterNameHeader) {
786 posterName = posterNameHeader
787 .getAttribute("href")
788 .replace(/\//g, "");
789 }
790
791 // method 2
792 if (posterName === "unkown") {
793 const match = window.location.pathname.match(posterUrlPat);
794 if (match) {
795 posterName = match[1];
796 }
797 }
798 let filename = filenameFormat(
799 storyFilenameTemplate,
800 posterName,
801 datetime,
802 mediaName
803 );
804 downloadResource(url, filename);
805 } else {
806 // open url in new tab
807 openResource(url);
808 }
809 }
810
811 function storyGetSectionNode(target) {
812 let sectionNode = target;

Callers 1

onClickHandlerFunction · 0.85

Calls 6

storyGetSectionNodeFunction · 0.85
storyGetUrlFunction · 0.85
filenameFormatFunction · 0.85
downloadResourceFunction · 0.85
openResourceFunction · 0.85
replaceMethod · 0.80

Tested by

no test coverage detected