gt.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. "v0.4.6 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 callbacks = {};
  79. var status = {};
  80. var random = function () {
  81. return parseInt(Math.random() * 10000) + (new Date()).valueOf();
  82. };
  83. var loadScript = function (url, cb) {
  84. var script = document.createElement("script");
  85. script.charset = "UTF-8";
  86. script.async = true;
  87. script.onerror = function () {
  88. cb(true);
  89. };
  90. var loaded = false;
  91. script.onload = script.onreadystatechange = function () {
  92. if (!loaded &&
  93. (!script.readyState ||
  94. "loaded" === script.readyState ||
  95. "complete" === script.readyState)) {
  96. loaded = true;
  97. setTimeout(function () {
  98. cb(false);
  99. }, 0);
  100. }
  101. };
  102. script.src = url;
  103. head.appendChild(script);
  104. };
  105. var normalizeDomain = function (domain) {
  106. // special domain: uems.sysu.edu.cn/jwxt/geetest/
  107. // return domain.replace(/^https?:\/\/|\/.*$/g, ''); uems.sysu.edu.cn
  108. return domain.replace(/^https?:\/\/|\/$/g, ''); // uems.sysu.edu.cn/jwxt/geetest
  109. };
  110. var normalizePath = function (path) {
  111. path = path.replace(/\/+/g, '/');
  112. if (path.indexOf('/') !== 0) {
  113. path = '/' + path;
  114. }
  115. return path;
  116. };
  117. var normalizeQuery = function (query) {
  118. if (!query) {
  119. return '';
  120. }
  121. var q = '?';
  122. new _Object(query)._each(function (key, value) {
  123. if (isString(value) || isNumber(value) || isBoolean(value)) {
  124. q = q + encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&';
  125. }
  126. });
  127. if (q === '?') {
  128. q = '';
  129. }
  130. return q.replace(/&$/, '');
  131. };
  132. var makeURL = function (protocol, domain, path, query) {
  133. domain = normalizeDomain(domain);
  134. var url = normalizePath(path) + normalizeQuery(query);
  135. if (domain) {
  136. url = protocol + domain + url;
  137. }
  138. return url;
  139. };
  140. var load = function (protocol, domains, path, query, cb) {
  141. var tryRequest = function (at) {
  142. var url = makeURL(protocol, domains[at], path, query);
  143. loadScript(url, function (err) {
  144. if (err) {
  145. if (at >= domains.length - 1) {
  146. cb(true);
  147. } else {
  148. tryRequest(at + 1);
  149. }
  150. } else {
  151. cb(false);
  152. }
  153. });
  154. };
  155. tryRequest(0);
  156. };
  157. var jsonp = function (domains, path, config, callback) {
  158. if (isObject(config.getLib)) {
  159. config._extend(config.getLib);
  160. callback(config);
  161. return;
  162. }
  163. if (config.offline) {
  164. callback(config._get_fallback_config());
  165. return;
  166. }
  167. var cb = "geetest_" + random();
  168. window[cb] = function (data) {
  169. if (data.status == 'success') {
  170. callback(data.data);
  171. } else if (!data.status) {
  172. callback(data);
  173. } else {
  174. callback(config._get_fallback_config());
  175. }
  176. window[cb] = undefined;
  177. try {
  178. delete window[cb];
  179. } catch (e) {
  180. }
  181. };
  182. load(config.protocol, domains, path, {
  183. gt: config.gt,
  184. callback: cb
  185. }, function (err) {
  186. if (err) {
  187. callback(config._get_fallback_config());
  188. }
  189. });
  190. };
  191. var throwError = function (errorType, config) {
  192. var errors = {
  193. networkError: '网络错误',
  194. gtTypeError: 'gt字段不是字符串类型'
  195. };
  196. if (typeof config.onError === 'function') {
  197. config.onError(errors[errorType]);
  198. } else {
  199. throw new Error(errors[errorType]);
  200. }
  201. };
  202. var detect = function () {
  203. return window.Geetest || document.getElementById("gt_lib");
  204. };
  205. if (detect()) {
  206. status.slide = "loaded";
  207. }
  208. window.initGeetest = function (userConfig, callback) {
  209. var config = new Config(userConfig);
  210. if (userConfig.https) {
  211. config.protocol = 'https://';
  212. } else if (!userConfig.protocol) {
  213. config.protocol = window.location.protocol + '//';
  214. }
  215. // for KFC
  216. if (userConfig.gt === '050cffef4ae57b5d5e529fea9540b0d1' ||
  217. userConfig.gt === '3bd38408ae4af923ed36e13819b14d42') {
  218. config.apiserver = 'yumchina.geetest.com/'; // for old js
  219. config.api_server = 'yumchina.geetest.com';
  220. }
  221. if (isObject(userConfig.getType)) {
  222. config._extend(userConfig.getType);
  223. }
  224. jsonp([config.api_server || config.apiserver], config.typePath, config, function (newConfig) {
  225. var type = newConfig.type;
  226. var init = function () {
  227. config._extend(newConfig);
  228. callback(new window.Geetest(config));
  229. };
  230. callbacks[type] = callbacks[type] || [];
  231. var s = status[type] || 'init';
  232. if (s === 'init') {
  233. status[type] = 'loading';
  234. callbacks[type].push(init);
  235. load(config.protocol, newConfig.static_servers || newConfig.domains, newConfig[type] || newConfig.path, null, function (err) {
  236. if (err) {
  237. status[type] = 'fail';
  238. throwError('networkError', config);
  239. } else {
  240. status[type] = 'loaded';
  241. var cbs = callbacks[type];
  242. for (var i = 0, len = cbs.length; i < len; i = i + 1) {
  243. var cb = cbs[i];
  244. if (isFunction(cb)) {
  245. cb();
  246. }
  247. }
  248. callbacks[type] = [];
  249. }
  250. });
  251. } else if (s === "loaded") {
  252. init();
  253. } else if (s === "fail") {
  254. throwError('networkError', config);
  255. } else if (s === "loading") {
  256. callbacks[type].push(init);
  257. }
  258. });
  259. };
  260. })(window);