gt.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. "v0.4.8 Geetest Inc.";
  2. (function (window) {
  3. "use strict";
  4. if (typeof window === 'undefined') {
  5. throw new Error('Geetest requires browser environment');
  6. }
  7. var document = window.document;
  8. var Math = window.Math;
  9. var head = document.getElementsByTagName("head")[0];
  10. function _Object(obj) {
  11. this._obj = obj;
  12. }
  13. _Object.prototype = {
  14. _each: function (process) {
  15. var _obj = this._obj;
  16. for (var k in _obj) {
  17. if (_obj.hasOwnProperty(k)) {
  18. process(k, _obj[k]);
  19. }
  20. }
  21. return this;
  22. }
  23. };
  24. function Config(config) {
  25. var self = this;
  26. new _Object(config)._each(function (key, value) {
  27. self[key] = value;
  28. });
  29. }
  30. Config.prototype = {
  31. api_server: 'api.geetest.com',
  32. protocol: 'http://',
  33. typePath: '/gettype.php',
  34. fallback_config: {
  35. slide: {
  36. static_servers: ["static.geetest.com", "dn-staticdown.qbox.me"],
  37. type: 'slide',
  38. slide: '/static/js/geetest.0.0.0.js'
  39. },
  40. fullpage: {
  41. static_servers: ["static.geetest.com", "dn-staticdown.qbox.me"],
  42. type: 'fullpage',
  43. fullpage: '/static/js/fullpage.0.0.0.js'
  44. }
  45. },
  46. _get_fallback_config: function () {
  47. var self = this;
  48. if (isString(self.type)) {
  49. return self.fallback_config[self.type];
  50. } else if (self.new_captcha) {
  51. return self.fallback_config.fullpage;
  52. } else {
  53. return self.fallback_config.slide;
  54. }
  55. },
  56. _extend: function (obj) {
  57. var self = this;
  58. new _Object(obj)._each(function (key, value) {
  59. self[key] = value;
  60. })
  61. }
  62. };
  63. var isNumber = function (value) {
  64. return (typeof value === 'number');
  65. };
  66. var isString = function (value) {
  67. return (typeof value === 'string');
  68. };
  69. var isBoolean = function (value) {
  70. return (typeof value === 'boolean');
  71. };
  72. var isObject = function (value) {
  73. return (typeof value === 'object' && value !== null);
  74. };
  75. var isFunction = function (value) {
  76. return (typeof value === 'function');
  77. };
  78. var MOBILE = /Mobi/i.test(navigator.userAgent);
  79. var pt = MOBILE ? 3 : 0;
  80. var callbacks = {};
  81. var status = {};
  82. var nowDate = function () {
  83. var date = new Date();
  84. var year = date.getFullYear();
  85. var month = date.getMonth() + 1;
  86. var day = date.getDate();
  87. var hours = date.getHours();
  88. var minutes = date.getMinutes();
  89. var seconds = date.getSeconds();
  90. if (month >= 1 && month <= 9) {
  91. month = '0' + month;
  92. }
  93. if (day >= 0 && day <= 9) {
  94. day = '0' + day;
  95. }
  96. if (hours >= 0 && hours <= 9) {
  97. hours = '0' + hours;
  98. }
  99. if (minutes >= 0 && minutes <= 9) {
  100. minutes = '0' + minutes;
  101. }
  102. if (seconds >= 0 && seconds <= 9) {
  103. seconds = '0' + seconds;
  104. }
  105. var currentdate = year + '-' + month + '-' + day + " " + hours + ":" + minutes + ":" + seconds;
  106. return currentdate;
  107. }
  108. var random = function () {
  109. return parseInt(Math.random() * 10000) + (new Date()).valueOf();
  110. };
  111. var loadScript = function (url, cb) {
  112. var script = document.createElement("script");
  113. script.charset = "UTF-8";
  114. script.async = true;
  115. // 对geetest的静态资源添加 crossOrigin
  116. if ( /static\.geetest\.com/g.test(url)) {
  117. script.crossOrigin = "anonymous";
  118. }
  119. script.onerror = function () {
  120. cb(true);
  121. };
  122. var loaded = false;
  123. script.onload = script.onreadystatechange = function () {
  124. if (!loaded &&
  125. (!script.readyState ||
  126. "loaded" === script.readyState ||
  127. "complete" === script.readyState)) {
  128. loaded = true;
  129. setTimeout(function () {
  130. cb(false);
  131. }, 0);
  132. }
  133. };
  134. script.src = url;
  135. head.appendChild(script);
  136. };
  137. var normalizeDomain = function (domain) {
  138. // special domain: uems.sysu.edu.cn/jwxt/geetest/
  139. // return domain.replace(/^https?:\/\/|\/.*$/g, ''); uems.sysu.edu.cn
  140. return domain.replace(/^https?:\/\/|\/$/g, ''); // uems.sysu.edu.cn/jwxt/geetest
  141. };
  142. var normalizePath = function (path) {
  143. path = path.replace(/\/+/g, '/');
  144. if (path.indexOf('/') !== 0) {
  145. path = '/' + path;
  146. }
  147. return path;
  148. };
  149. var normalizeQuery = function (query) {
  150. if (!query) {
  151. return '';
  152. }
  153. var q = '?';
  154. new _Object(query)._each(function (key, value) {
  155. if (isString(value) || isNumber(value) || isBoolean(value)) {
  156. q = q + encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&';
  157. }
  158. });
  159. if (q === '?') {
  160. q = '';
  161. }
  162. return q.replace(/&$/, '');
  163. };
  164. var makeURL = function (protocol, domain, path, query) {
  165. domain = normalizeDomain(domain);
  166. var url = normalizePath(path) + normalizeQuery(query);
  167. if (domain) {
  168. url = protocol + domain + url;
  169. }
  170. return url;
  171. };
  172. var load = function (config, send, protocol, domains, path, query, cb) {
  173. var tryRequest = function (at) {
  174. var url = makeURL(protocol, domains[at], path, query);
  175. loadScript(url, function (err) {
  176. if (err) {
  177. if (at >= domains.length - 1) {
  178. cb(true);
  179. // report gettype error
  180. if (send) {
  181. config.error_code = 508;
  182. var url = protocol + domains[at] + path;
  183. reportError(config, url);
  184. }
  185. } else {
  186. tryRequest(at + 1);
  187. }
  188. } else {
  189. cb(false);
  190. }
  191. });
  192. };
  193. tryRequest(0);
  194. };
  195. var jsonp = function (domains, path, config, callback) {
  196. if (isObject(config.getLib)) {
  197. config._extend(config.getLib);
  198. callback(config);
  199. return;
  200. }
  201. if (config.offline) {
  202. callback(config._get_fallback_config());
  203. return;
  204. }
  205. var cb = "geetest_" + random();
  206. window[cb] = function (data) {
  207. if (data.status == 'success') {
  208. callback(data.data);
  209. } else if (!data.status) {
  210. callback(data);
  211. } else {
  212. callback(config._get_fallback_config());
  213. }
  214. window[cb] = undefined;
  215. try {
  216. delete window[cb];
  217. } catch (e) {
  218. }
  219. };
  220. load(config, true, config.protocol, domains, path, {
  221. gt: config.gt,
  222. callback: cb
  223. }, function (err) {
  224. if (err) {
  225. callback(config._get_fallback_config());
  226. }
  227. });
  228. };
  229. var reportError = function (config, url) {
  230. load(config, false, config.protocol, ['monitor.geetest.com'], '/monitor/send', {
  231. time: nowDate(),
  232. captcha_id: config.gt,
  233. challenge: config.challenge,
  234. pt: pt,
  235. exception_url: url,
  236. error_code: config.error_code
  237. }, function (err) {})
  238. }
  239. var throwError = function (errorType, config) {
  240. var errors = {
  241. networkError: '网络错误',
  242. gtTypeError: 'gt字段不是字符串类型'
  243. };
  244. if (typeof config.onError === 'function') {
  245. config.onError(errors[errorType]);
  246. } else {
  247. throw new Error(errors[errorType]);
  248. }
  249. };
  250. var detect = function () {
  251. return window.Geetest || document.getElementById("gt_lib");
  252. };
  253. if (detect()) {
  254. status.slide = "loaded";
  255. }
  256. window.initGeetest = function (userConfig, callback) {
  257. var config = new Config(userConfig);
  258. if (userConfig.https) {
  259. config.protocol = 'https://';
  260. } else if (!userConfig.protocol) {
  261. config.protocol = window.location.protocol + '//';
  262. }
  263. // for KFC
  264. if (userConfig.gt === '050cffef4ae57b5d5e529fea9540b0d1' ||
  265. userConfig.gt === '3bd38408ae4af923ed36e13819b14d42') {
  266. config.apiserver = 'yumchina.geetest.com/'; // for old js
  267. config.api_server = 'yumchina.geetest.com';
  268. }
  269. if(userConfig.gt){
  270. window.GeeGT = userConfig.gt
  271. }
  272. if(userConfig.challenge){
  273. window.GeeChallenge = userConfig.challenge
  274. }
  275. if (isObject(userConfig.getType)) {
  276. config._extend(userConfig.getType);
  277. }
  278. jsonp([config.api_server || config.apiserver], config.typePath, config, function (newConfig) {
  279. var type = newConfig.type;
  280. var init = function () {
  281. config._extend(newConfig);
  282. callback(new window.Geetest(config));
  283. };
  284. callbacks[type] = callbacks[type] || [];
  285. var s = status[type] || 'init';
  286. if (s === 'init') {
  287. status[type] = 'loading';
  288. callbacks[type].push(init);
  289. load(config, true, config.protocol, newConfig.static_servers || newConfig.domains, newConfig[type] || newConfig.path, null, function (err) {
  290. if (err) {
  291. status[type] = 'fail';
  292. throwError('networkError', config);
  293. } else {
  294. status[type] = 'loaded';
  295. var cbs = callbacks[type];
  296. for (var i = 0, len = cbs.length; i < len; i = i + 1) {
  297. var cb = cbs[i];
  298. if (isFunction(cb)) {
  299. cb();
  300. }
  301. }
  302. callbacks[type] = [];
  303. }
  304. });
  305. } else if (s === "loaded") {
  306. init();
  307. } else if (s === "fail") {
  308. throwError('networkError', config);
  309. } else if (s === "loading") {
  310. callbacks[type].push(init);
  311. }
  312. });
  313. };
  314. })(window);