gt.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. (function (window, document) {
  2. if (window.initGeetest) {
  3. return;
  4. }
  5. var head = document.getElementsByTagName("head")[0];
  6. var protocol = location.protocol + "//";
  7. var callbacks = [];
  8. var status = "init";
  9. var random = function () {
  10. return parseInt(Math.random() * 10000) + (new Date()).valueOf();
  11. };
  12. var run = function () {
  13. for (var i = 0, len = callbacks.length; i < len; i = i + 1) {
  14. callbacks[i]();
  15. }
  16. callbacks = [];
  17. };
  18. var detect = function () {
  19. return window.Geetest || document.getElementById("gt_lib");
  20. };
  21. var down = function (config) {
  22. var s = document.createElement("script");
  23. s.charset = "UTF-8";
  24. s.type = "text/javascript";
  25. s.onload = s.onreadystatechange = function () {
  26. if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
  27. if (detect()) {
  28. status = "loaded";
  29. run();
  30. } else {
  31. status = "fail";
  32. if (typeof config.onError === 'function') {
  33. config.onError();
  34. } else {
  35. throw new Error("网络错误");
  36. }
  37. }
  38. s.onload = s.onreadystatechange = null;
  39. }
  40. };
  41. s.onerror = function () {
  42. status = "fail";
  43. s.onerror = null;
  44. if (typeof config.onError === 'function') {
  45. config.onError();
  46. } else {
  47. throw new Error("网络错误");
  48. }
  49. };
  50. var staticServer = (config.staticservers && config.staticservers[0]) || "static.geetest.com/";
  51. s.src = protocol + staticServer + "static/js/geetest.0.0.0.js";
  52. head.appendChild(s);
  53. };
  54. var getLib = function (config) {
  55. status = "loading";
  56. var cb = "geetest_" + random();
  57. window[cb] = function (newConfig) {
  58. status = "loaded";
  59. if (newConfig) {
  60. config.type = newConfig.type;
  61. }
  62. run();
  63. window[cb] = undefined;
  64. try {
  65. delete window[cb];
  66. } catch (e) {
  67. }
  68. };
  69. var s = document.createElement("script");
  70. s.charset = "UTF-8";
  71. s.type = "text/javascript";
  72. s.onload = s.onreadystatechange = function () {
  73. if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
  74. if (!detect()) {
  75. down(config);
  76. }
  77. }
  78. };
  79. s.onerror = function () {
  80. down(config);
  81. };
  82. var apiServer = config.apiserver || "api.geetest.com/";
  83. s.src = protocol + apiServer + "getfrontlib.php?gt=" + config.gt + "&callback=" + cb;
  84. head.appendChild(s);
  85. };
  86. if (detect()) {
  87. status = "loaded";
  88. }
  89. window.initGeetest = function (config, callback) {
  90. if (typeof config.gt !== "string") {
  91. throw new Error("initGeetest接口配置参数gt错误")
  92. }
  93. var init = function () {
  94. callback(new window.Geetest(config));
  95. };
  96. if (status === "loaded") {
  97. init();
  98. } else if (status === "fail") {
  99. if (typeof config.onError === 'function') {
  100. config.onError();
  101. } else {
  102. throw new Error("网络错误");
  103. }
  104. } else if (status === "loading") {
  105. callbacks.push(function () {
  106. init();
  107. });
  108. } else if (status === "init") {
  109. callbacks.push(function () {
  110. init();
  111. });
  112. getLib(config);
  113. }
  114. };
  115. })(window, document);