gt.js 8.0 KB

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