|
1 | 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 | + |
2 | 30 | $.fn.instastory = function(userSettings) {
|
3 | 31 | let $container = this;
|
4 |
| - let searchUrl = ""; |
5 | 32 | let settings = $.extend({
|
6 | 33 | get : "",
|
7 | 34 | type : "",
|
|
34 | 61 | return false;
|
35 | 62 | }
|
36 | 63 |
|
37 |
| - const determineType = function(searchString) { |
38 |
| - if(searchString.indexOf("@") > -1) { |
39 |
| - settings.type = "user"; |
40 |
| - settings.get = searchString.substring(searchString.indexOf('@') + 1); |
41 |
| - } else if(searchString.indexOf("#") > -1) { |
42 |
| - settings.type = "hashtag"; |
43 |
| - settings.get = searchString.substring(searchString.indexOf('#') + 1); |
44 |
| - } else { |
45 |
| - settings.type = "hashtag"; |
46 |
| - settings.get = searchString; |
47 |
| - } |
48 |
| - } |
49 |
| - |
50 |
| - const determineUrl = function(searchType) { |
51 |
| - if(searchType == "user") { |
52 |
| - searchUrl = "https://www.instagram.com/" + settings.get + "/?__a=1" |
53 |
| - } else { |
54 |
| - searchUrl = "https://www.instagram.com/explore/tags/" + settings.get + "/?__a=1"; |
55 |
| - } |
56 |
| - } |
| 64 | + let urlSettings = constructUrlSettings(settings.get); |
| 65 | + settings.type = urlSettings.type; |
57 | 66 |
|
58 | 67 | const getImageSize = function(post) {
|
59 | 68 | const wantedImageSize = settings.imageSize;
|
|
144 | 153 | return html;
|
145 | 154 | };
|
146 | 155 |
|
147 |
| - determineType(settings.get); |
148 |
| - determineUrl(settings.type); |
149 |
| - |
150 | 156 | $.ajax({
|
151 |
| - url: searchUrl, |
152 |
| - success: function(data) { |
153 |
| - $container.html(generateHtml(data.graphql[settings.type])); |
154 |
| - settings.after(); |
155 |
| - } |
| 157 | + url: urlSettings.url, |
| 158 | + }).done(function(data) { |
| 159 | + $container.html(generateHtml(data.graphql[settings.type])); |
| 160 | + settings.after(); |
156 | 161 | }).fail(function(data) {
|
157 | 162 | switch(data.status) {
|
158 | 163 | case 404:
|
|
164 | 169 | }
|
165 | 170 | });
|
166 | 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 | + } |
167 | 195 | })(jQuery);
|
0 commit comments