<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="http://code.jquery.com/jquery-2.2.0.min.js"></script >
<script type="text/javascript">
/* ---------------------------------------------
// 共通変数
--------------------------------------------- */
//連結用FLG
//0:停止 1:次へ進める -1:その関数のみ
var flgAuto = -1;
//リクエスト送信時ウェイト(単位:ミリ秒)
var msWaiteRequest = 1000;
//作業用
var newRows = [];
var count404Error = 0;
/* ---------------------------------------------
// 共通関数
--------------------------------------------- */
/* ---------------------------------------------
▼文字列抽出
--------------------------------------------- */
function sliceStr(str,search1,search2){
if(!str){return res = '';}
var a,b,len;
var res;
a=0;b=0;len=0;
if(search1!="" && search2!=""){
a = str.indexOf(search1);
if(a!=-1){
len = search1.length;
b = str.indexOf(search2,a+len);
return res = str.slice(a+len,b);
}else{
return res = "";
}
}else if(search1!="" && search2==""){
a = str.indexOf(search1);
if(a!=-1){
len = search1.length;
return res = str.slice(a+len);
}else{
return res = "";
}
}
}
function lastSliceStr(str,search1,search2){
var a,b,len;
var res;
a=0;b=0;len=0;
if(search1!="" && search2!=""){
a = str.lastIndexOf(search1);
if(a!=-1){
len = search1.length;
b = str.indexOf(search2,a+len);
return res = str.slice(a+len,b);
}else{
return res = "";
}
}else if(search1!="" && search2==""){
a = str.lastIndexOf(search1);
if(a!=-1){
len = search1.length;
return res = str.slice(a+len);
}else{
return res = "";
}
}
}
//ログ出力
function outputLog(argMsg) {
// return false;
var outMsg="";
var oDate = new Date();
var sDate = oDate.getHours()+":"+("00"+oDate.getMinutes()).substring((""+oDate.getMinutes()).length)+":"+ ("00"+oDate.getSeconds()).substring((""+oDate.getSeconds()).length);
var outMsg = document.getElementById("log").value;
//outMsg = outMsg + '<br />' + sDate + " " + argMsg;
//outMsg = outMsg + sDate + " " + argMsg + '\n'; //下に追記
outMsg = sDate + " " + argMsg + '\n' + outMsg ; //上に追記
document.getElementById("log").value = outMsg;
}
function getDate(){
var oDate = new Date();
var sDate = oDate.getHours()+":"+("00"+oDate.getMinutes()).substring((""+oDate.getMinutes()).length)+":"+ ("00"+oDate.getSeconds()).substring((""+oDate.getSeconds()).length);
return sDate;
}
// ローカルファイル保存(IEのみ動作)
function writeToLocal(filename, content) {
outputLog("-- function writeToLocal start");
var ua = navigator.userAgent.toLowerCase();
try {
if (ua.indexOf("msie")) { // MS IE
filename = 'D:\\Work\\' + filename;
filename = filename;
// インターネットオプションで「スクリプトを実行しても安全だとマークされていない
// ActiveX コントロールの初期化とスクリプトの実行(セキュリティで保護されていない)」
// を有効にする必要あり
var fso = new ActiveXObject("Scripting.FileSystemObject");
// ファイルを新規作成して書き込みモードで開く (文字コードはUTF-16)
// cf. http://msdn.microsoft.com/ja-jp/library/cc428044.aspx
// http://msdn.microsoft.com/ja-jp/library/cc428042.aspx
var file = fso.OpenTextFile(filename,
2, // 1: 読み取り専用, 2: 書き込み, 8: 追記
true, // ファイルが存在しなければ新規作成するかどうか
-1 // -2: OSのデフォルト文字コード, -1: UTF-16, 0: ASCII
);
file.Write(content);
file.Close();
alert("書き込みが完了しました!");
} else {
alert("エラー: ローカルファイルへの書き込み方がわかりません・・・");
}
} catch (e) {
alert("Error: " + e);
}
outputLog("-- function writeToLocal end");
}
/* ---------------------------------------------
// ファイル保存
--------------------------------------------- */
function saveFile(){
outputLog("-- function saveFile start");
var fileList = document.getElementById("btnLoadFile").files;
filename = "結果_"+ fileList[0].name;
outputLog("[filename]"+filename);
writeToLocal(filename, document.getElementById("resultText").value);
outputLog("-- function saveFile end");
}
/* ---------------------------------------------
// ファイル読み込み
--------------------------------------------- */
function loadFile(evt){
outputLog("-- function loadFile start");
var file = evt.target.files;
var reader = new FileReader(); //FileReaderの作成
reader.readAsText(file[0]); //テキスト形式で読み込む
reader.onload = function(ev){ //読込終了後の処理
document.getElementById("originalText").value = reader.result; //テキストエリアに表示する
}
outputLog("-- function loadFile end");
}
/* ---------------------------------------------
// ファイルの中身を一行ずつ読み取り、http://で始まるURLがあったら、抽出する
--------------------------------------------- */
function addURL(evt){
outputLog("-- function addURL start");
var text = document.getElementById("originalText").value;
var rows = new Array();
rows = text.split("\n"); //一行ずつ配列に格納
newRows = [];
var row = "";
var url = "";
var newText = ""
for (var i=0;i<rows.length;i++){
row = rows[i];
url = lastSliceStr(row,"http","");
if(url!=""){url = 'http'+url}
newRows.push({original:row,url:url,result:"",code:"",text:""});
//outputLog(row+"\t"+url);
newText = newText + row+"\t"+url +"\n";
}
document.getElementById("addURLText").value = newText;
outputLog("-- function addURL end");
}
/* ---------------------------------------------
// URLチェック開始
--------------------------------------------- */
function startCheck(){
if(flgAuto==0){return false;}
if(flgAuto==1){outputLog("既に実行中です");return false;}
flgAuto=1;
outputLog("-- function startCheck");
document.getElementById("startTime").innerHTML = getDate();
msWaiteRequest = parseInt(document.getElementById("waite")[document.getElementById("waite").selectedIndex].value);
count404Error =0;
indexNewRows = 0;
if(indexNewRows<newRows.length){
document.getElementById("resultText").value = "実行中 … 全"+newRows.length+"行中 "+ indexNewRows +"行目を確認中 (内404エラー: "+count404Error+"件)";
if(newRows[indexNewRows]["url"].length>0){
setTimeout(function(){check()},msWaiteRequest);
}else{
outputLog("["+(indexNewRows+1)+"行目][url]なし");
nextCheck();
}
}else{
endCheck();
}
}
function reStartCheck(){
if(flgAuto==1){outputLog("既に実行中です");return false;}
flgAuto=1;
outputLog("-- function reStartCheck");
document.getElementById("reStartTime").innerHTML = getDate();
msWaiteRequest = parseInt(document.getElementById("waite")[document.getElementById("waite").selectedIndex].value);
if(indexNewRows<newRows.length){
document.getElementById("resultText").value = "再開/実行中 … 全"+newRows.length+"行中 "+ indexNewRows +"行目を確認中 (内404エラー: "+count404Error+"件)";
if(newRows[indexNewRows]["url"].length>0){
setTimeout(function(){check()},msWaiteRequest);
}else{
outputLog("["+(indexNewRows+1)+"行目][url]なし");
nextCheck();
}
}else{
endCheck();
}
}
function nextCheck(){
if(flgAuto==0){document.getElementById("resultText").value = document.getElementById("resultText").value + " --- 停止しました" ;return false;}
outputLog("-- function nextCheck");
msWaiteRequest = parseInt(document.getElementById("waite")[document.getElementById("waite").selectedIndex].value);
indexNewRows++;
if(indexNewRows<newRows.length){
document.getElementById("resultText").value = "実行中 … 全"+newRows.length+"行中 "+ indexNewRows +"行目を確認中 (内404エラー: "+count404Error+"件)";
if(newRows[indexNewRows]["url"].length>0){
setTimeout(function(){check()},msWaiteRequest);
}else{
outputLog("["+(indexNewRows+1)+"行目][url]なし");
nextCheck();
}
}else{
endCheck();
}
}
function endCheck() {
if(flgAuto==0){document.getElementById("resultText").value = document.getElementById("resultText").value + " --- 停止しました" ;return false;}
var newText = ""
var row = "";
for (var i=0;i<newRows.length;i++){
row = newRows[i]["original"]+"\t"+newRows[i]["url"]+"\t"+newRows[i]["result"]+"\t"+newRows[i]["code"]+"\t"+newRows[i]["text"];
newText = newText + row+"\n";
}
document.getElementById("resultText").value = newText;
document.getElementById("endTime").innerHTML = getDate();
outputLog("-- function endCheck");
flgAuto=-1;
}
function check(){
if(flgAuto==0){document.getElementById("resultText").value = document.getElementById("resultText").value + " --- 停止しました" ;return false;}
outputLog("-- function check start");
var result = "";
var code = "";
var text = "";
// ajax HTTP通信
$.ajax({
url: newRows[indexNewRows]["url"],
method: "GET",
async : false,
dataType: "html",
complete: function(XMLHttpRequest, textStatus) {
result = textStatus;
code = XMLHttpRequest.status;
text = XMLHttpRequest.statusText;
newRows[indexNewRows]={original:newRows[indexNewRows]["original"],url:newRows[indexNewRows]["url"],result:result,code:code,text:text}
document.getElementById("log").value = "";
outputLog("["+(indexNewRows+1)+"行目][url]"+newRows[indexNewRows]["url"] + "[result]" +result+ " [code]"+code+" [text]"+text);
if(code == 404){count404Error++;}
outputLog("-- function check end");
nextCheck();
}
});
}
function stop(){
outputLog("-- stop");
flgAuto=0;
document.getElementById("stopTime").innerHTML = getDate();
}
/* ---------------------------------------------
// イベントリスナー
--------------------------------------------- */
$(document).ready(function(){
//クリア
document.getElementById("originalText").value = "";
document.getElementById("addURLText").value = "";
document.getElementById("resultText").value = "";
document.getElementById("log").value = "";
outputLog("-- ready function start");
//イベント追加
document.getElementById("btnLoadFile").addEventListener("change",loadFile,false);
document.getElementById("btnAddURL").addEventListener("click",addURL, false);
document.getElementById("btnRequestStart").addEventListener("click",startCheck,false);
document.getElementById("btnRequestReStart").addEventListener("click",reStartCheck,false);
document.getElementById("btnRequestStop").addEventListener("click",stop,false);
document.getElementById("btnSaveFile").addEventListener("click",saveFile,false);
outputLog("-- ready function end");
});
</script>
<style>
h1 a{color:#efefef;}
h2{font-size:100%;background-color: #efefef;border-bottom: 1px solid #CCC;}
h3{font-size:100%;background-color: #efefef;font-weight:normal;}
h1,h2,h3{ padding:5px 0 5px 1em; }
</style>
</head>
<body>
<form>
<h2>URLが書かれたファイルを読み込み <input type="file" id="btnLoadFile"></h2>
<p>チェックしたいURLが書かれたテキストファイルを上の「参照」ボタンから読み込む。</p>
<textarea id="originalText" rows="5" cols="150" readonly></textarea>
<h2>URL検索 <input type="button" value="検索" id="btnAddURL"></h2>
<p>上のテキストを一行ずつ読み込み、URLが書かれているか検索。URLが書かれていた場合、行の最後にタブ区切りで見つけたURLを追記。<br>
ファイル内の表記方法は問わないが、1行内に複数のhttpで始まるURLが書かれていた場合は、最期のURL1つのみが検索対象となる。</p>
<textarea id="addURLText" rows="5" cols="150" readonly></textarea>
<h2>URLチェック
<span style="font-weight:normal">ウェイト
<select id="waite">
<option value="500">0.5秒</option>
<option value="1000" selected>1.0秒</option>
<option value="1500" >1.5秒</option>
<option value="2000">2.0秒</option>
<option value="2500">2.5秒</option>
<option value="3000">3.0秒</option>
<option value="3500">3.5秒</option>
<option value="4000">4.0秒</option>
<option value="5000">5.0秒</option>
<option value="10000">10.0秒</option>
</select>
秒間隔
<input type="button" value=" 開始 " id="btnRequestStart"> <span id="startTime">--:--:--</span>
<input type="button" value=" 再開 " id="btnRequestReStart"> <span id="reStartTime">--:--:--</span>
<input type="button" value="停止" id="btnRequestStop"> <span id="stopTime">--:--:--</span>
(終了) <span id="endTime">--:--:--</span>
<input type="button" value=" 結果を保存 " id="btnSaveFile">
</span>
</h2>
<p>追記されたURLを対象に、GETリクエストを送信。結果(結果/HTTP状態コード/説明)を行の最後にタブ区切りで追記。<br>
※サーバへの負荷を考慮し、前回のリクエスト送信から「ウェイト」で指定した秒数を開けている。
</p>
<textarea id="resultText" rows="5" cols="150" readonly></textarea>
<h2>LOG</h2>
<p><textarea name="log" cols="150" rows="10" id="log"></textarea></p>
</form>
</body>
</html>