JSDeferred使ã£ã¦ã¿ã
// ==UserScript== // @name è¶ ãã¹ã // @namespace http://d.hatena.ne.jp/drgqst/ // @description è¶ ã¹ã¼ãã¼ã°ã¬ã¤ãã¦ã«ãã©ãã¹ã // @include http://www.google.co.jp/* // ==/UserScript== (function(){ /* ããã« JSDeferred ã®ã³ã¼ããè²¼ãä»ãã */ function parseJSON (d) { // unsafe return eval("("+d.responseText+")"); } next(function () { return xhttp.get("http://twitter.com/statuses/public_timeline.json").next(parseJSON); }). next(function (d) { var id = d[0].user.screen_name; GM_log(id); return xhttp.get("http://twitter.com/users/show/" + id + ".json").next(parseJSON); }). next(function (d) { GM_log(uneval(d)); }). error(function (e) { alert(e); }); });
ããï¼ããããããªã»ã»ã»
ãã»ã»ã»ããããã°ãã¯ããã«ä½ãé¢æ°ãå¼ã¹ã£ã¦æ¸ãã¦ããããªæ°ãããã
ããä¸åº¦é ããèªã¿ç´ãã¦ã¿ããããã
JSDeferred による非同期処理
ãã³ã¼ã³ï¼
ããã ï¼æãåºããã
Deferred.define();
ããã¡ã¤ã³ãå¼ã¶ãã ã£ããããã¡ã¤ã³ã1æ¥ä½¿ãæ¨ã¦ã³ã³ã¿ã¯ãã¬ã³ãºãã¯ã³ãã¼ ã¢ãã¥ãã¥ã¼ ããã¡ã¤ã³ã
åãã£ã¬ã³ã¸
// ==UserScript== // @name è¶ ãã¹ã // @namespace http://d.hatena.ne.jp/drgqst/ // @description è¶ ã¹ã¼ãã¼ã°ã¬ã¤ãã¦ã«ãã©ãã¹ã // @include http://www.google.co.jp/* // ==/UserScript== (function(){ /* ããã« JSDeferred ã®ã³ã¼ããè²¼ãä»ãã */ Deferred.define(); // â ããã«è¿½å ãã function parseJSON (d) { // unsafe return eval("("+d.responseText+")"); } next(function () { return xhttp.get("http://twitter.com/statuses/public_timeline.json").next(parseJSON); }). next(function (d) { var id = d[0].user.screen_name; GM_log(id); return xhttp.get("http://twitter.com/users/show/" + id + ".json").next(parseJSON); }). next(function (d) { GM_log(uneval(d)); }). error(function (e) { alert(e); }); });
ããã»ã»ã» ãããããªãããããxhttpãããªãã®ãã
ãã»ã»ã»ãå¾
ã¦ãã»ã»ã»
ä¸ã¤ç®ã®ãnextãã§çºçããä¾å¤ããä¸çªæå¾ã®ãerrorãã¾ã§ä¼æãã¦ããããªããã
ããã¼ããªãããããã
åã ãã£ã¬ã³ã¸
wikiãã¡ããã¨èªãã§ã¿ããã
JSDeferred - CodeRepos
ãããã»ã»ã»ãè±èªããã£ã±ãã ã»ã»ã»ãããã°ã俺
ããã§ãDownloadãã®æåãè¦ã¤ããç¬éã«ããæèåæ¢ãã¦ãããã«ãã¦ã³ãã¼ããã¦ãã¾ã£ãã®ã§ãã
ããã¡ãã£ã¨âã«ã¹ã¯ãã¼ã«ããã¨ããã
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
Greasemonkeyã§ä½¿ãããã®ã³ã¼ããç¨æããã¦ããããªããï¼
æåããããã使ããã¦ãããã°ããã£ããã ã俺ãä»ã¾ã§ãã£ã¦ãã®ã¯ãªãã ã£ããã ããããããï¼ï¼è¦ãã俺ãã´ãã®ããã ï¼ï¼
ãªãã§æå¾ã¾ã§èªã¾ãªããã ã俺ã®ãã«ï¼ã¢ã«ã¤ã©ã¤ï¼
åã ãã£ã¬ã³ã¸
// ==UserScript== // @name è¶ ãã¹ã // @namespace http://d.hatena.ne.jp/drgqst/ // @description è¶ ã¹ã¼ãã¼ã°ã¬ã¤ãã¦ã«ãã©ãã¹ã // @include http://www.google.co.jp/* // ==/UserScript== with (D()) { function parseJSON (d) { // unsafe return eval("("+d.responseText+")"); } next(function () { return xhttp.get("http://twitter.com/statuses/public_timeline.json").next(parseJSON); }). next(function (d) { var id = d[0].user.screen_name; return xhttp.get("http://twitter.com/users/show/" + id + ".json").next(parseJSON); }). next(function (d) { var img = document.createElement( "img" ); img.src = d.profile_image_url; document.body.appendChild( img ); }). error(function (e) { alert(e); }); } /* ããã« http://svn.coderepos.org/share/lang/javascript/jsdeferred/trunk/jsdeferred.userscript.js ã è²¼ãä»ãã */
ããããã»ã»ã»
ããã£ã»ãï¼
Twitterããç»åãæã£ã¦ãããã¨ã«æåãã¾ãããï¾ï¾ï¾ï½»ï¾ï½°ï½²
ã¾ã¨ã
- JSDeferred ã¯éåæå¦çãåªãããã¦ãããã©ã¤ãã©ãªã
- éåæå¦çã®try{}catch{}ãã¾ã¨ãã¦ãããã
- ããXMLHttpRequestã ã£ã¦æããªãã
- ãããããããã§ãï¼âè¶ éè¦ï¼
ã¿ããªã使ã£ã¦ã¿ãã¨ãããï¼