1(function($) { 2 const constructUrlSettings = function(searchString) { 3 let type = ''; 4 let keyword = ''; 5 let url = ''; 6 7 if(searchString.indexOf("@") > -1) { 8 type = "user"; 9 keyword = searchString.substring(searchString.indexOf('@') + 1); 10 } else if(searchString.indexOf("#") > -1) { 11 type = "hashtag"; 12 keyword = searchString.substring(searchString.indexOf('#') + 1); 13 } else { 14 type = "hashtag"; 15 keyword = searchString; 16 } 17 18 if(type == "user") { 19 url = "https://www.instagram.com/" + keyword + "/?__a=1" 20 } else { 21 url = "https://www.instagram.com/explore/tags/" + keyword + "/?__a=1"; 22 } 23 24 return { 25 url: url, 26 type: type 27 }; 28 } 29 30 $.fn.instastory = function(userSettings) { 31 let $container = this; 32 let settings = $.extend({ 33 get : "", 34 type : "", 35 imageSize : 150, 36 limit : 6, 37 link : true, 38 template : "", 39 after : function(){} 40 }, userSettings); 41 42 if(!$container.length) { 43 console.group("Instastory.js log"); 44 console.warn("The DOM element you tried to initiate the plugin on, does not exist"); 45 console.log("For more info on how to use the plugin, please see: https://github.com/kasperlegarth/instastory.js"); 46 console.groupEnd(); 47 48 return false; 49 } 50 51 if(typeof userSettings == "string") { 52 settings.get = userSettings; 53 } 54 55 if(settings.get == "") { 56 console.group("Instastory.js log"); 57 console.warn("You failed to specify what you want"); 58 console.log("For more info on how to use the plugin, please see: https://github.com/kasperlegarth/instastory.js"); 59 console.groupEnd(); 60 61 return false; 62 } 63 64 let urlSettings = constructUrlSettings(settings.get); 65 settings.type = urlSettings.type; 66 67 const getImageSize = function(post) { 68 const wantedImageSize = settings.imageSize; 69 70 if(typeof wantedImageSize == "number") { 71 switch(wantedImageSize) { 72 case 150: 73 return post.thumbnail_resources[0].src; 74 case 240: 75 return post.thumbnail_resources[1].src; 76 case 320: 77 return post.thumbnail_resources[2].src; 78 case 480: 79 return post.thumbnail_resources[3].src; 80 case 640: 81 return post.thumbnail_resources[4].src; 82 default: 83 return post.thumbnail_resources[0].src; 84 } 85 } else if(wantedImageSize == "raw") { 86 return post.display_url; 87 } else { 88 return post.thumbnail_resources[0].src; 89 } 90 91 }; 92 93 const buildTemplate = function(post) { 94 String.prototype.allReplace = function(obj) { 95 let retStr = this; 96 for (let x in obj) { 97 retStr = retStr.replace(new RegExp(x, "g"), obj[x]); 98 } 99 return retStr; 100 }; 101 102 let templateCodes = { 103 "{{accessibility_caption}}" : post.accessibility_caption, 104 "{{caption}}": ((post.edge_media_to_caption.edges.length > 0) ? post.edge_media_to_caption.edges[0].node.text : ""), 105 "{{comments}}": post.edge_media_to_comment.count, 106 "{{image}}": getImageSize(post), 107 "{{likes}}": post.edge_liked_by.count, 108 "{{link}}": "https://www.instagram.com/p/"+ post.shortcode, 109 } 110 111 let template = settings.template.allReplace(templateCodes); 112 113 return template; 114 } 115 116 const generateHtml = function(ajaxResult) { 117 let html = ""; 118 let postsObject = {}; 119 120 switch (settings.type) { 121 case "user": 122 postsObject = ajaxResult.edge_owner_to_timeline_media; 123 break; 124 125 default: 126 postsObject = ajaxResult.edge_hashtag_to_media; 127 break; 128 } 129 130 let posts = postsObject.edges; 131 132 133 for(var i = 0; i < settings.limit; i++) { 134 135 if(posts[i] !== undefined) { 136 let post = posts[i].node; 137 let tempHtml = ""; 138 139 if(settings.template != "") { 140 tempHtml = buildTemplate(post); 141 } else { 142 tempHtml = "<img src='" + getImageSize(post) + "' alt='" + post.accessibility_caption + "'>" 143 144 if(settings.link) { 145 tempHtml = "<a href='https://www.instagram.com/p/"+ post.shortcode +"'>" + tempHtml + "</a>"; 146 } 147 } 148 149 html += tempHtml; 150 } 151 } 152 153 return html; 154 }; 155 156 $.ajax({ 157 url: urlSettings.url, 158 }).done(function(data) { 159 $container.html(generateHtml(data.graphql[settings.type])); 160 settings.after(); 161 }).fail(function(data) { 162 switch(data.status) { 163 case 404: 164 console.warn("The " + settings.type + " do not exsists, please try another one"); 165 break; 166 default: 167 console.warn('An unknow error happend'); 168 break; 169 } 170 }); 171 } 172 173 $.instastory = function(keyword) { 174 let urlSettings = constructUrlSettings(keyword); 175 let result = ''; 176 177 $.ajax({ 178 url: urlSettings.url, 179 async: false 180 }).done(function(data) { 181 result = data.graphql[urlSettings.type]; 182 }).fail(function(data) { 183 switch(data.status) { 184 case 404: 185 console.warn("The " + urlSettings.type + " do not exsists, please try another one"); 186 break; 187 default: 188 console.warn('An unknow error happend'); 189 break; 190 } 191 }); 192 193 return result; 194 } 195})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.