未加星标

Instafetch.js A 10KB JS plugin for the Instagram API

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Instafetch.js   A 10KB JS plugin for the Instagram API

I have nothing against jQuery. It does exactly what it’s supposed to do. jQuery simplifies a lot of the complicated things in javascript, so it is easier to use (and we do use it here at Urban Influence).

But why use an 87KB file only to write more JavaScript that is only being used for AJAX to return some JSONP? Every project doesn’t need it. So let’s use fetch instead to hit the endpoints of the Instagram API.

See the Pen Instafetch.js Example by Thomas Vaeth ( @thomasvaeth ) on CodePen .

The Fetch API

The Fetch API provides a fetch() method defined on the window object, which can be used to perform requests. fetch() returns a Promise that can be used to retrieve the response of the request. It is an easier way to make web requests and handle responses than using a XMLHttpRequest.

fetch(url, options).then(function(response) {
// handle HTTP response
}.catch(error) {
// handle network error
});

However, the Fetch API only supports JSON, but what about JSONP? The Instagram API requires JSONP because it is a cross-domain request. Fetch-JSONP is being used as a dependency to allow JSONP data-types while using the Fetch API.

Now we’re good to go on most browsers except for Internet Explorer. It’s pretty common for something not to work on IE and having to find a workaround, so this is no different from other things we build. The Fetch API returns a Promise, but IE doesn’t know what a Promise is. You will see “Promise is undefined” in the console and the Instagram feed will be empty. We’re using the ES6-Promise Polyfill to support IE until fetch() is supported. So that’s where the plugin goes from 3KB to 10KB (but it’s still less than 87KB).

Download

Instafetch.js is currently available on NPM, Bower, and GitHub.

NPM npm install --save instafetch.js es6-promise require('es6-promise').polyfill();
require('instafetch.js'); Bower bower install instafetch.js <script type="text/javascript" src="path/to/bower_components/instafetch.js/dist/instafetch.min.js"></script> GitHub

Download the script or the minified version in the dist folder.

<script type="text/javascript" src="path/to/instafetch.min.js"></script> How to Use

The Instagram API uses the OAuth 2.0 protocol, so you're going to need an access token. The easiest way to get your access token is login to Instagram on your browser and generate one on Pixel Union .

<div id="instafetch"></div> <script type="text/javascript">
instafetch.init({
accessToken: 'ACCESS TOKEN',
target: 'instafetch',
numOfPics: 20,
caption: false
});
</script>

Instafetch.js will look for an element with the ID of instafetch by default. The target element can be changed when initializing the plugin.

The plugin also allows you to set the number of items to return from your feed and if you want to include the captions.

Demo & Documentation

Demo

Documentation

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JSONPGitJavaScriptGitHubJavajQueryXML
分页:12
转载请注明
本文标题:Instafetch.js A 10KB JS plugin for the Instagram API
本站链接:http://www.codesec.net/view/483052.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(34)