Created
December 5, 2012 02:02
-
-
Save gongo/4211439 to your computer and use it in GitHub Desktop.
OpenPNE ver2 用 cmd プラグイン
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* speakerdeck.com.js | |
* | |
* OpenPNE の日記に Speakerdeck の Embed を貼り付けて表示する | |
* | |
* @auther Kazuma | |
* @version 0.01 | |
* | |
*/ | |
function url2cmd(url) { | |
if (!url.match(/^https:\/\/speakerdeck\.com\/([a-zA-Z0-9_-]+)\/([a-zA-Z0-9_-]+)/)) { | |
pne_url2a(url); | |
return; | |
} | |
var user = RegExp.$1; | |
var title = RegExp.$2; | |
main(url, user, title) | |
} | |
/** | |
* Prototype の Ajax.Request だと、別ドメインのサイトを見るときに OPTIONS メソッドになって取得できず(Status はOK) | |
* XMLHttpRequest だと、http から https に遷移したら上手くいかなかったので | |
* 最終的には YQL 経由で | |
*/ | |
function main(realUrl, user, title) { | |
var width = 425; | |
var endpoint = 'http://speakerdeck.com/oembed.json?'; | |
var resourceUri = endpoint + 'url=/' + user + '/' + title + '&maxwidth=' + width; | |
var requestUri = getYQLUri(resourceUri); | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', requestUri, false); | |
xhr.send(null); | |
if (xhr.status === 200) { | |
var response = JSON.parse(xhr.responseText); | |
var results = response.query.results; | |
if (!results || results === undefined) { | |
pne_url2a(realUrl); | |
} | |
var embedHTML = results.json.html; | |
if (embedHTML == undefined) { | |
pne_url2a(realUrl); | |
return; | |
} | |
document.write(embedHTML); | |
} else { | |
pne_url2a(realUrl); | |
} | |
} | |
function getYQLUri(resourceUri) { | |
return 'http://query.yahooapis.com/v1/public/yql?q=' | |
+ encodeURIComponent('select * from json where url="' + resourceUri + '"') | |
+ '&format=json'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍