指定されたURLのタイトルとコメントを取得する処理をつけています。
テスト環境にてとりあえずの動作はできているっぽいです。
内容
テキストフィールド(id=edit_url)に記述されたURLのページを取得し、その情報からタイトル(id=edit_name)とコメント(id=edit_comment)のテキストフィールドの値を変更する。タイトルは<title>タグにて囲まれた文字を対象とし、コメントは<meta name=”description”>の属性contentとして指定された文字列とする。
ちなみにAjaxを使用するので、jquery.jsを使用しています。(prototype.jsではないです)
処理の流れ
当初はJavaScript(Ajax)にてURLのhtmlを取得して、それを解析しようと思っていたのですが、セキュリティ上同じドメインからでないとJavaScriptではデータを取れないということが判明しました。なのでrailsにて他サーバーからのhtml取得とかを行う事にしました。
- JavaScriptにてURLをパラメータとしてrailsに送信
- railsにてパラメータのURLをhtmlで取得
- railsにて取得したhtmlを解析してタイトルとコメントを取得する
- railsにてタイトルを<title>タグで囲み、またコメントを<comment>タグで囲んで出力する
- JavaScriptにてrailsのレスポンスを解析し、タイトルとコメントのテキストフィールドにセットする
参考にしたサンプルソースです。取得ボタンのonclickイベントにてtest()は実行するみたいです。
function test(){
$.post(’edit/test’, {}, function(result){
alert(result);
});
def test
render_text “aiueo”
end
なんか$.postが動かなくて$(this).postって記述を変えたような気もする。正直どうだったか忘れました。
取得ボタンのonclickイベントで実行されるのはget_info()で、内部で使用している文字コードはutf8です。
//—————————————————————————————
// タイトル/コメントの取得
//—————————————————————————————
var get_info_flg = false;
function get_info(){
var url;
var msg;
//———————————————————————————–
// 二重処理防止チェック
//———————————————————————————–
if(get_info_flg != false){
return;
}
get_info_flg = true;
url = $(“#edit_url”).val(); // URLの取得
//———————————————————————————–
// URLチェック
//———————————————————————————–
if(url.length == 0){
return;
}
//———————————————————————————–
// 結果出力先の初期化
//———————————————————————————–
$(“#edit_name”).val(MSG_NOW_GET_INFO);
$(“#edit_comment”).val(MSG_NOW_GET_INFO);
//———————————————————————————–
// タイトル、コメントの取得
//———————————————————————————–
$.post(“edit/bookmark_get_info?url=” + url, {}, function(html){
$(“#edit_name”).val(get_substring(html, “title”)); // タイトルの取得
$(“#edit_comment”).val(get_substring(html, “comment”)); // コメントの取得
// エラー表示
msg = get_substring(html, “message”);
if(msg != “”){
alert(msg)
}
get_info_flg = false;
});
}
//—————————————————————————————
// 部分取得
// [引数] src 検索対象
// key キーワード
// [戻り値]結果
//—————————————————————————————
function get_substring(src, key)
{
var x1, x2;
x1 = src.indexOf(‘<‘ + key + ‘>’);
x1 += key.length + 2;
x2 = src.indexOf(‘</’ + key + ‘>’, x1);
return (src.substring(x1, x2));
}
#
# 指定URLの情報表示
#
require ‘hpricot’
require ‘open-uri’
def bookmark_get_info
title = “”
comment = “”
msg = “”
begin
doc = Hpricot open(URI.encode(params[:url])) # 指定されたURLのファイル取得
# タイトル取得
title = doc.search(“title”).first.inner_html
# コメント取得
doc.search(“meta”) do |t|
comment = t[:content] if t[:name] && t[:content] && t[:name].casecmp(“description”) == 0
end
# 文字コード変換
title = bookmark_get_info_conv title
comment = bookmark_get_info_conv comment
unless title != “”
msg += MES_WARN_GETINFO_TITLE
end
unless comment != “”
msg += MES_WARN_GETINFO_COMMENT
end
rescue
msg += MES_WARN_GETINFO
end
render_text “<title>#{title}</title><comment>#{comment}</comment><message>#{msg}</message>”
end
#
# 文字列のエンコード変換&整形
#
def bookmark_get_info_conv str
str = Kconv.toutf8 str
str = str.strip
str = str.gsub “\n”, “”
str = str.gsub “\t”, ” ”
str = str.gsub “ ”, ” ”
str = str.gsub ” “, ” ”
while str.include?(” “)
str = str.gsub ” “, ” ”
end
return str
end