あわせて読みたいAPIでカスタマイズ(ファビコン付き)

ブログのテンプレートを変えたらなんだか『あわせて読みたい』が浮いてる。。。
せっかくなんであわせて読みたいAPIを使って見た目を変えてみたい。
やりかた分かんないんで、またインターネッツの力を借りよう。

ちなみにここではWordPressを使った例です。(大して関係ないですが。)

さて、APIっていうのが便利なモノだってのは何となくわかる。
そしてAPIとともに良く出てくるJSONP。ジェーソンピー?全く分からない。
少し調べてみたところ、JSONPを使うと外部ドメインにリクエストを送って、返されたデータをごにょごにょと使えるようになるらしい。(超アバウト。。。)

JSONPについては以下のエントリーが分かりやすかったです。

さて、早くも目的を見失いそうだけども(^_^;)
やりたいことは、あわせて読みたいの見た目を変えたいってこと。
これについては以下のエントリーを参考にさせていただきました。

まずは上記のエントリー中にある『awaseteyomitai.js』というファイルをダウンロード(またはコピペ)する。
そのファイルをサーバーにアップする。例えば、WordPress を使ってる場合だと、wordpress/wp-includes/js/ フォルダの中とか。

ただしここでは awaseteyomitai.js を次のように、一部変更している。

function awasete_yomitai(v){
var list_max=10;
if(list_max<1 || 10< max; i++){
var liElement = document.createElement( 'LI');
if (liElement){
var d = v[i];
elmUl.appendChild(liElement);
var imgElement = document.createElement('img');
imgElement.src = d.favicon;
liElement.appendChild(imgElement);
var aElement = document.createElement('A');
aElement.href=d.url;
aElement.appendChild(document.createTextNode(d.title));
liElement.appendChild(aElement);
}
}
}

変更点は、次の二つ。

  1. li タグに class を指定しない
  2. ファビコンをリストのあたまに表示する(imgElement の作成)

次に、この .js ファイルを読み込むために、ヘッダーに次のように記述する。

<script type="text/javascript" src="http://フォルダの場所/awaseteyomitai.js"></script>

さらに body の中に次のように記述。(これはヘッダーだとうまくいかないみたい。)

<script type="text/javascript" src="http://api.awasete.com/showjson.phtml?u=http%3a%2f%2f自分のブログのURL" charset="utf-8"></script>

ちなみにURLはエンコードする必要があるとのこと。このへんで↓

URL エンコード、デコードもできる UTF8/16 変換ライブラリ

これであわせて読みたいのデータを読み込むための準備ができたので、あとは自分のブログに表示するために、サイドバーなどに以下を記述。

<div id="awasete_yomitai"></div>

そうすると、不思議なことにブラウザ上では次のような構造でリストが表示される。(←まだよく理解してない。。。)

<div id="awasete_yomitai">
<ul>
<li><img src="ファビコンURL" /><a href="ブログURL">サイト1</a></li>
<li><img src="ファビコンURL" /><a href="ブログURL">サイト2</a></li>
<li><img src="ファビコンURL" /><a href="ブログURL">サイト3</a></li>
・・・
</ul>
</div>

これであわせて読みたいリストが表示されるようになるから、あとは CSS で背景を付けてみたり加工すればいいんじゃないかな。

« »

コメント / トラックバック 713 件

  1. rabbit vibrator より:

    I’ll put a link to this page on my website. I’m sure my visitors will find dis post really interesting

  2. hhmycpcxgaf より:

    ZVWUdh rqkpgpvkoywb, [url=http://vzjzxygcfojd.com/]vzjzxygcfojd[/url], [link=http://utpgqurxymel.com/]utpgqurxymel[/link], http://gcvjskfyixcd.com/

  3. zzikvnor より:

    Xvgnxv woyljvlsdggr, [url=http://tvjghksgvwcb.com/]tvjghksgvwcb[/url], [link=http://ezzwelfkublr.com/]ezzwelfkublr[/link], http://cgpjfgbzkpqz.com/

  4. ipad case より:

    When you get your own ipad time, maybe you also want to have your own iPad Case and iPad Accessories to better protect the iPad. When you come to our site, the first you need to know is that you can buy the best quality and most expensive iPad case, also your favorite iPad cases and iPad accessories. There are hundreds kinds of ipad case

  5. Silverfish Bug より:

    Beautiful blog with nice informational content. This is a really interesting and informative post. Good job! keep it up, hope to read your other updates. Thanks for this nice sharing.

  6. truth about abs より:

    Thanks so much for this great article; this is the kind of thing that continues me going through the day. I’ve been looking around for your blog after I heard about them from a friend and was thrilled when I was able to find it after searching for some time. Being a avid blogger, I’m pleased to see others taking initiative and contributing to the community. I just wanted to comment to show my appreciation for your work as it’s very encouraging, and many writers do not get the credit they deserve. I am sure I’ll be back and will send some of my friends.

  7. I usually don’t post in blogs but your weblog forced me to, awesome work… luvly

コメントをどうぞ