-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdebug.js
113 lines (111 loc) · 3.76 KB
/
xdebug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(function (window) {
var option = {
host:null,
port:8081,
client:"anonymous",
debugModel:true
};
var scriptPath = "/target/target-script-min.js";
var debugXMLPath = "../xdebug.xml";
function Debugger(host, port, client) {
this.run = function(host, port, client){
_applyOption(host, port, client);
this.host = option.host;
this.port = option.port;
this.client = option.client;
_importDebugTag(this.host,this.port,this.client);
};
this.reload = function(){
location.reload();
};
this.log = function(msg){
if(!option.debugModel)return;
if(console.debug){
console.debug(msg);
}else{
console.log(msg);
}
};
if(host && typeof host == "string"){
this.run(host,port,client);
}
function _importDebugTag(host, port, client){
if(!host || !option.debugModel)return;
var url = host + ":" + port + scriptPath + "#" + client;
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
head.appendChild(script);
}
function _applyOption(host, port, client) {
if (host) {
if (/^http:\/\/|^https:\/\//.test(host)) {
option.host = host;
} else {
option.host = "http://" + host;
}
}
if (!isNaN(port)) {
option.port = port;
}
if (client && typeof client == "string") {
option.client = client;
}
}
}
function loadOption(complete,error){
var options = {};
var scripts = document.getElementsByTagName("script");
var s = scripts[scripts.length-1];
var host = s.getAttribute("host");
if(host){
var port = s.getAttribute("port");
var client = s.getAttribute("client");
if (/^http:\/\/|^https:\/\//.test(host)) {
options.host = host;
} else {
options.host = "http://" + host;
}
if(typeof port == "number"){
options.port = port;
}
if(client){
options.client = client;
}
options.debugModel = s.getAttribute("debug");
complete(options);
return;
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET",debugXMLPath,true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState == 4){
try{
var xml = xmlHttp.responseXML;
var root = xml.documentElement;
for(var i=0;i<root.childNodes.length;i++){
var node = root.childNodes[i];
if(node.nodeType == 1){
options[node.nodeName] = trimStr(node.textContent);
}
}
complete(options);
}catch (e){
error(e);
}
}
};
xmlHttp.send(null);
function trimStr(str){
return str.trim?str.trim():str.replace(/(^\s*)|(\s*$)/g, "");
}
}
window.xDebug = new Debugger();
loadOption(function(options){
option.debugModel = options.debugModel != "false";
window.xDebug.run(options.host,options.port,options.client);
},function(e){
console.error("load "+debugXMLPath+" fail. stack:"+ e.message);
});
})(this);