Skip to content
This repository was archived by the owner on Apr 7, 2021. It is now read-only.

Commit 076eb87

Browse files
Merge pull request #18 from kasperlegarth/feature/raw-data-mode
Added raw functionality
2 parents 726cc57 + cec0987 commit 076eb87

File tree

3 files changed

+77
-35
lines changed

3 files changed

+77
-35
lines changed

README.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
This is a jQuery plugin to make it easier for developers to implement instagram feeds on websites. There is no need for access tokens and stuff like that. Use good old jQuery.
33

44
**Table of contents**
5-
* [Getting startet](#getting-startet)
6-
* [Options](#options)
7-
* [Default options](#default-options)
8-
* [Templating](#templating)
9-
* [License](#license)
10-
* [Special thanks](#special-thanks)
5+
- [instastory.js](#instastoryjs)
6+
- [Getting startet](#getting-startet)
7+
- [Options](#options)
8+
- [Default options](#default-options)
9+
- [Templating](#templating)
10+
- [Data Mode](#data-mode)
11+
- [License](#license)
12+
- [Special thanks](#special-thanks)
1113

1214
## Getting startet
1315
It is really simple to use the plugin all you need to to is include jQuery and the instastory.js file where you include your other scrips in the project.
@@ -87,6 +89,15 @@ Here is a full list of tags to be used in the template:
8789
| {{likes}} | Returns the number of people that have liked the image. |
8890
| {{link}} | Returns the url for the post (not the same as the image url) |
8991

92+
## Data Mode
93+
This plugin only uses a fraction of all the information returned by the ajax call. You might want to do things differently than this plugin. Therefore if you call the plugin without a selector, you can get the raw data from the ajax call. Please note that this function may course the site to "hang" becouse the call is not async.
94+
95+
Simply do like this:
96+
```javascript
97+
const rawData = $.instastory('@instagram');
98+
console.log(rawData);
99+
```
100+
90101
## License
91102
This project is licensed under the MIT Liense - see the [LICENCE.md](LICENSE.md)
92103

demo/demo.js

+3
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,7 @@ $(document).ready(function() {
7777
});
7878
}, 2000);
7979
});
80+
81+
let rawData = $.instastory('@instagram');
82+
console.log('Data Mode', rawData);
8083
});

instastory.js

+57-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
(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+
230
$.fn.instastory = function(userSettings) {
331
let $container = this;
4-
let searchUrl = "";
532
let settings = $.extend({
633
get : "",
734
type : "",
@@ -34,26 +61,8 @@
3461
return false;
3562
}
3663

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;
5766

5867
const getImageSize = function(post) {
5968
const wantedImageSize = settings.imageSize;
@@ -144,15 +153,11 @@
144153
return html;
145154
};
146155

147-
determineType(settings.get);
148-
determineUrl(settings.type);
149-
150156
$.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();
156161
}).fail(function(data) {
157162
switch(data.status) {
158163
case 404:
@@ -164,4 +169,27 @@
164169
}
165170
});
166171
}
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+
}
167195
})(jQuery);

0 commit comments

Comments
 (0)