Skip to content

Instantly share code, notes, and snippets.

@gongo
Created December 5, 2012 02:02
Show Gist options
  • Save gongo/4211439 to your computer and use it in GitHub Desktop.
Save gongo/4211439 to your computer and use it in GitHub Desktop.
OpenPNE ver2 用 cmd プラグイン
/*
* 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';
}
@tomohiro
Copy link

tomohiro commented Dec 5, 2012

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment