Last active
August 29, 2015 14:03
-
-
Save spiegel-im-spiegel/8075979c314a417b96c2 to your computer and use it in GitHub Desktop.
jlib-importtext.js -- JavaScript Library for SyntaxHighlighter form
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
// ================================================================ | |
// jlib-importtext.js -- JavaScript Library for SyntaxHighlighter form | |
// ================================================================ | |
// Written and revised by Yasuhiro (Spiegel) Arakawa, 2014 | |
// http://www.baldanders.info/spiegel/log2/000717.shtml | |
// https://gist.github.com/spiegel-im-spiegel/8075979c314a417b96c2 | |
// This document code refer to JKL.ParseXML library. | |
// http://www.kawa.net/works/js/jkl/parsexml.html | |
// This document code is licensed under cc-license "CC-BY" | |
// http://creativecommons.org/licenses/by/4.0/ | |
// ================================================================ | |
if ( typeof(JLIB) == 'undefined' ) JLIB = function() {}; | |
// ================================================================ | |
// class: JLIB.ImportText | |
// テキストファイルを読み込み、SyntaxHighlighter に準拠した | |
// フォーマットで出力するだけの簡単なお仕事 | |
// | |
// param: url : URL (required) | |
// id : node identify (required) | |
JLIB.ImportText = function (url, id) { | |
this.url = url; | |
this.id = id; | |
this.classAttr = "brush:javascript html-script:true"; | |
this.title = "" | |
this.http = new JLIB.HTTP(url, null, "GET", true); | |
return this; | |
}; | |
// コールバック関数をセットしてローダを呼ぶ | |
JLIB.ImportText.prototype.load = function() { | |
var copy = this; | |
this.http.callback_func = function() { | |
var text = copy.http.responseText(); | |
var pre = document.createElement('pre'); | |
pre.className = copy.classAttr; | |
pre.setAttribute('title', copy.title); | |
pre.appendChild(document.createTextNode(text)); | |
var div = document.getElementById(copy.id); | |
div.appendChild(pre); | |
}; | |
this.http.load(); | |
} | |
// ================================================================ | |
// class: JLIB.HTTP : XMLHttpRequest 制御 | |
// | |
// param: url : URL (required) | |
// query : string or null (default: null) | |
// method : GET / POST (default: GET) | |
// textmode : true or false (default: true) | |
JLIB.HTTP = function (url, query, method, textmode) { | |
this.url = url; | |
this.query = null; | |
this.method = "GET"; | |
this.textmode = true; | |
this.req = null; | |
this.already_done = false; | |
this.callback_func = null; | |
if (arguments.length > 1) { | |
this.query = query; | |
} | |
if (arguments.length > 2) { | |
this.method = method; | |
} | |
if (arguments.length > 3) { | |
this.textmode = textmode; | |
} | |
if (typeof(this.query) == "string") { | |
this.method = "POST"; | |
} | |
return this; | |
}; | |
// ローダ本体 | |
// XMLHttpRequest の onreadystatechange イベントでコールバック関数を起動する | |
JLIB.HTTP.prototype.load = function() { | |
if (window.XMLHttpRequest) { | |
this.req = new XMLHttpRequest(); | |
} else if (window.ActiveXObject) { | |
var activex = "Microsoft.XMLHTTP"; | |
if (this.method == "GET" && this.textmode == false) { | |
activex = "Microsoft.XMLDOM"; | |
} | |
this.req = new ActiveXObject(activex); | |
} else { | |
return; | |
} | |
if (typeof(this.req.send) != "undefined") { | |
this.req.open(this.method, this.url, true); //async mode | |
} | |
if (typeof(this.req.setRequestHeader) != "undefined") { | |
this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | |
} | |
if (typeof(this.req.overrideMimeType) != "undefined" && this.textmode == false) { | |
this.req.overrideMimeType("text/xml"); | |
} | |
var copy = this; | |
copy.already_done = false; | |
var check_func = function () { | |
if (copy.req.readyState == 4 && copy.req.status == 200){ | |
if (copy.already_done ==true) return; | |
copy.already_done = true; | |
if ( copy.callback_func != null ) { | |
copy.callback_func(); | |
} | |
} | |
}; | |
this.req.onreadystatechange = check_func; | |
if ( typeof(this.req.send) != "undefined" ) { | |
this.req.send( this.query ); | |
} else if ( typeof(this.req.load) != "undefined" ) { | |
this.req.async = true; | |
this.req.load( this.url ); | |
} | |
} | |
//ロードしたテキスト情報を取得する | |
JLIB.HTTP.prototype.responseText = function() { | |
if (this.req == null) return ""; | |
if (navigator.appVersion.match("KHTML")) { | |
var esc = escape( this.req.responseText ); | |
if ( ! esc.match("%u") && esc.match("%") ) { | |
return decodeURIComponent(esc); | |
} | |
} | |
return this.req.responseText; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment