Created
September 6, 2016 10:19
-
-
Save kawa-/e34671ee5e65e9dae880fba79d92bf75 to your computer and use it in GitHub Desktop.
URLからTitleを取ってくる(jQuery3依存)
This file contains 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
// URLを入力するとTitleを自動補完する | |
$("#url").change(function () { | |
var url = $("#url").val(); | |
if (is_url(url)) { | |
$("#title").val("自動補完中...しばらくお待ち下さい。"); | |
// URLのようであれば問い合わせしに行く | |
$.ajax({ | |
type: "GET", | |
url: "https://crossorigin.me/" + url, | |
timeout: 10000, | |
cache: false, | |
dataType: 'html' | |
}).done(function (response, textStatus, jqXHR) { | |
var doc = new DOMParser().parseFromString(response, "text/html"); | |
var title = doc.querySelectorAll('title')[0]; | |
$("#title").val(title.innerText); | |
}).fail(function (jqXHR, textStatus, errorThrown) { | |
console.log('問い合わせ失敗'); | |
if(jqXHR.status === 404){ | |
$("#title").val("そのURLは404のようです。有効なURLを入力してください。"); | |
} else { | |
$("#title").val("自動補完に失敗しました。そのURLが有効であれば手動でタイトルを入力してください。"); | |
} | |
})} | |
}); | |
function is_url(str) | |
{ | |
regexp = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/; | |
if (regexp.test(str)) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment