ã©ãã, 使ãã«ãã.
ã¾ã, ããã¨ãããå ã«è¨ã£ã¦ããã.
chrome.extension.sendRequestã®è¯ãã¨ãã, ããã¯ã³ã³ãã³ãã¹ã¯ãªããã¸ã®ãã«ããã£ã¹ããã§ãããã¨.
åãã£ã¦ã人ãªããã以ä¸ã®ãã¨ã¯èª¬æããªãã§ãåãã£ã¦ãããã¨æã.
ãªã«ãä¸æºãã¨ããã¨, ã¹ã¯ãªããå ¨ä½ã§ã¡ãã»ã¼ã¸ãåãåãã¨ãããã¨.
æåã«ãã®APIã使ãã¨ãã¯, 誰ã§ããããªæãã®ã³ã¼ãã«ãªãã¨æã.
chrome.extension.onRequest.addEventListener (function (e, sender, response) { switch (e.action) { 'foo': ... 'bar': ... 'zee': ... });
ããããaddEventListenerãã¹ã¯ãªããã®ã²ã¼ã¨ã¤.
ã§ãã§ã, ã ãã ããããããæºè¶³ã§ããªããªã£ã¦ãã. ã©ããããã¨ãã¨ããã¨,
function Foo () { } Foo.prototype.init = function () { chrome.extension.onRequest.addEventListener (function (e, sender, response) { switch (e.action) { 'foo': ... } }); }; var foo1 = new Foo; var foo2 = new Foo; var foo3 = new Foo;
ã¿ããã«, ã©ãã©ãã¤ã³ã¹ã¿ã³ã¹ãä½ã£ã¡ãã.
ããã¨, ã©ããªãã. ä¸ã¤ã®sendRequestã«å¯¾ãã¦foo1ãããfoo2ãããfoo3ãããã¯ã¼ã¯ã¼åãåºã.
ãªãã¨ããã, ããã¯ããã§ä½¿ãéãããæ°ããããã©, åã¯ä»ã®ã¿ãããfoo1ããã ãã«è©±ãããã®ã .
ãããããã¨ãèãã¦ãã¨, ãããªã£ã¦ãã.
function Foo () { this.id = createUnique (); } Foo.prototype.init = function () { var self = this; chrome.extension.onRequest.addEventListener (function (e, sender, response) { switch (e.action) { 'foo': if (e.id === self.id) {... } } }); };
åé¡ãªãã§ã¯ãªãã... ã¯ã£ã¯ã£ã¯.
ããã, ããããéã主ã¯ã¡ã¤ã³ã®ãã¼ã¸ã®ãªãã¸ã§ã¯ãã®idãã©ããã£ã¦ç¥ãã®ã ãã.
話ãæ½è±¡çããã¦åãããªããããããªã. ä»èãã¦ãå ·ä½çãªåé¡ã¯è³ã£ã¦ã·ã³ãã«ã§, 以ä¸ã®ãããªãã®ã .
Twitterã®ã¯ã©ã¤ã¢ã³ãã®èªè¨¼ãéãç»é¢, ãã®PINãèªåã§ã¢ããªã±ã¼ã·ã§ã³ã«éãæ¹æ³ã .
è²ã ãªå®è£ ã®ä»æ¹ãå¤åãã£ã¦, å人åJSã¨ã¯ãã®ãã¨ã ã¨æã.
æã¯ã©ã¤ã¢ã³ãã®ã³ã¼ããèªãã§ãã, 次ã®ããã«ãã£ã¦ã.
Contents scriptã§ã©ããªãã¼ã¸ã«ãã¹ã¯ãªãããåãè¾¼ã¿, URLãhttps://api.twitter.com/oauth/authorizeã§ããã°, PINã³ã¼ããæ¢ãã¦ã¢ããªã±ã¼ã·ã§ã³ã«é£ã°ã, ããã¦ã¿ããéãã.
åé¡ã¯, æããã¦ãã®ãããªã±ã¼ã¹ã«Contents scriptã使ãã®ã妥å½ã ãããï¼ã¨ãã話ãã§ãã.
å¿è«NOã . Contents scriptã£ã¦ã®ã¯å ¨ã¦ã®ãã¼ã¸ã§ã¹ã¯ãªãããã¼ãããåã.
Contents scriptã«jqueryãªããã使ã£ã¦ãæ¡å¼µããããã©, ããããªãã ã£ã¦è©±... ãããã³ã.
Twitterã®èªè¨¼ã®ããã ãã«Contents scriptã¯ç¸å¿ãããªãã§ãã.
ä»èãã¦ãå®è£ ã¯, ç°¡åã«è¨ãã¨æ¬¡ã®ãããªæã.
URLãä½ã â chrome.tabs.create ã§tabidãã²ãã â chrome.tabs.executeScript ã§PINã²ãã
ããã§ç¢ºãã«, PINã¯ã²ããã§ãã.
æåã«è¨ã£ã¦ãFooã£ã¦ãããªãã¸ã§ã¯ã, ããã¯, Twitter Consumerã .
ãï¼ ä¸ã¤ã®ã¢ããªã±ã¼ã·ã§ã³ã¯ä¸ã¤ã®Consumerã ã£ã¦ï¼
ãããã¡ãã£ã¨ä½è¨ã£ã¦ãã®ãåãããªã.
var consumer1 = new TwitterConsumer (consumerKey, consumerSecret); var consumer2 = new TwitterConsumer (consumerKey, consumerSecret); consumer1.requestToken ({ success: function (data) { chrome.tab.create ({ url: this.createPinUrl (data) }, function (obj) { var tabid = obj.id; this.getPinTimeout = setTimeout (function () { chrome.tabs.executeScript (tabid, { file: "getpin.js" }); }, 1000); }); } }); consumer2.requestToken ( ... );
ãªããããããã®. ã¡ãã£ã¡ãã£é©å½ãªã³ã¼ã(ããããthisã¨ã, ãã¼ã¼ã¼ã£ããã«åããªããã©... )ã§ç³ã訳ãªããã©, é çªã¯ãããªæã.
ã, ããããã¡ãã£ã¨ãã·ãªã³ã¼ãæ¸ãã¦ã¾ãã. ãã³ã.
ããã§, ãã£ãã®åé¡ãçãã. getpin.jsã«ã©ããã£ã¦consumer1ã¨ãconsumer2ã®æ å ±ãéãï¼
2ã¤ã®Consumerãéãã2ã¤ã®èªè¨¼ç»é¢ãã, ééããã«PINãè¿ãã«ã¯ï¼
ã¤ã¡ã¼ã¸ã¨ãã¦ã¯,
chrome.tabs.executeScript (tabid, { file: "getpin.js?id=" + this.id });
ã¿ãããªã.
ã£ã¦ããããã®ã¯ç¡çãªã®ã...
...
... ã³ã³ã¾ã§å¢ãã§æ¸ãã¦ãããã©, æç« ã«ãã¦ã¿ãã°ãã¾ãé£ãããªãã£ããªã£ã¦...
ä¸ã¤ã®æ±ãããæ¹ã¯, PINã®ã¿ãã§å®è¡ããã³ã¼ããåçã«å¤ããæ¹æ³.
chrome.tabs.executeScript (tabid, { code: createCode (self.id) });
ãããã, createCodeã¯æååçµåã§ã³ã¼ããä½ãã¿ãããª, ãã¾ããããããªãæ¹æ³.
äºã¤ç®, ããã¯ãã£ãä»æãã¤ããæ¹æ³.
getpin.jsã«, ã¢ããªå´ã®consumerã®çªå·ãæãããã£ã¦ããã®ããã¡ãªèãæ¹ãªãã ...
getpin.jsããtabã®idãpinã¨ä¸ç·ã«éã£ã¦ããã°ãã. ã¾ãã«é転ã®çºæ³.
consumerå´ã¯, tabãä½ã£ãæã«ãã®idã¯ç¥ããã¨ãã§ãã¦ããã.
ã ãã, 帰ã£ã¦ãã{ pin: ..., tabid: ... }ã®tabidã¨æ¯è¼ããããã.
ãã¼ãªããèªå·±è§£æ±ºãã¡ãã£ããã¼
JavaScriptãªã¢ã·ã¬ã§ã¼
Google Chromeãªã¢ã·ã¬ã¼ã, ãã£ã±
ãªããchrome.extension.sendRequestã§ãããã¾åé¡ãªãã
追è¨: OAuth2.0ã«ã¤ãã¦ã¯ãã¡ã
OAuth2.0ã®callbackã®urlã«, Google Chromeã®æ¡å¼µã®url(chrome-extension://...)ã使ããªã件. ç°¡åãªè§£æ±ºæ¹æ³ã¯, ãã®urlãTinyURLã§å§ç¸®ãã¦ç»é²ãããã¨. ãªãããã, ã£ã¦æãã ãã©, ãããè¶ ç°¡å.
ã§ãã¾ããã®ã¢ããªã±ã¼ã·ã§ã³ã®ãµã¤ããä½ã£ã¦, ã¢ããªå´ã«ãªãã¤ã¬ã¯ãããããã«ããã®ãããã®ãã.
èªè¨¼ã®ãããã¢ãããä½ãèªè¨¼tinyurlchrome-extension:// . /#oauth_access_token= çãªæãã« appå´ã¸sendRequestã§ãã¼ã¯ã³éã£ã¦window.close ã¿ãããªæããã
æ´ã«è¿½è¨: JavaScriptã®ã³ã¼ããåããã¾ãªãã¦ãOAuth2.0ãªãã¢ããªã®ãªãªã¸ã³ã®ã¢ãã¬ã¹ã«ãªãã¤ã¬ã¯ããããã®ã§, document.URLããaccess_tokenãèªã¿åã£ã¦, ãããæ¬ä½ã¢ããªã®æ¹ã«sendRequestãã¦ããwindow.closeããã°ãã. 詳ããã¯ä½æãæ¸ã.