gofly-front.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var launchButtonFlag=false;
  2. var titleTimer,titleNum=0;
  3. var originTitle = document.title;
  4. if (typeof GOFLY_URL=="undefined"){
  5. var GOFLY_URL="https://gofly.sopans.com";
  6. }
  7. if (typeof GOFLY_KEFU_ID=="undefined"){
  8. var GOFLY_KEFU_ID="";
  9. }
  10. if (typeof GOFLY_BTN_TEXT=="undefined"){
  11. var GOFLY_BTN_TEXT="Chat with me";
  12. }
  13. dynamicLoadCss(GOFLY_URL+"/static/css/gofly-front.css");
  14. if (typeof $!="function"){
  15. dynamicLoadJs("https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js",function () {
  16. dynamicLoadJs("https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js",function () {
  17. clickBtn();
  18. });
  19. });
  20. }else{
  21. dynamicLoadJs("https://cdn.bootcdn.net/ajax/libs/layer/3.1.1/layer.min.js",function () {
  22. clickBtn();
  23. });
  24. }
  25. function clickBtn(){
  26. $('body').append('<div id="launchButton" class="launchButton animateUpDown"><div class="launchButtonText">'+GOFLY_BTN_TEXT+'</div></div>');
  27. $("#launchButton").on("click",function() {
  28. if (launchButtonFlag) return;
  29. var width=$(window).width();
  30. if(width<768){
  31. window.open(GOFLY_URL+'/chatIndex?kefu_id='+GOFLY_KEFU_ID+'&refer='+window.document.title);
  32. return;
  33. }
  34. layer.open({
  35. type: 2,
  36. title: GOFLY_BTN_TEXT,
  37. closeBtn: 1, //不显示关闭按钮
  38. shade: [0],
  39. area: ['520px', '530px'],
  40. offset: 'rb', //右下角弹出
  41. anim: 2,
  42. content: [GOFLY_URL+'/chatIndex?kefu_id='+GOFLY_KEFU_ID+'&refer='+window.document.title, 'yes'], //iframe的url,no代表不显示滚动条
  43. end: function(){
  44. launchButtonFlag=false;
  45. $(".launchButton").show();
  46. }
  47. });
  48. launchButtonFlag=true;
  49. $(".launchButton").hide();
  50. });
  51. $("body").click(function () {
  52. clearTimeout(titleTimer);
  53. document.title = originTitle;
  54. });
  55. }
  56. function showKefu(){
  57. if (launchButtonFlag) return;
  58. var width=$(window).width();
  59. if(width<768){
  60. window.open(GOFLY_URL+'/chatIndex?kefu_id='+GOFLY_KEFU_ID+'&refer='+window.document.title);
  61. return;
  62. }
  63. layer.open({
  64. type: 2,
  65. title: GOFLY_BTN_TEXT,
  66. closeBtn: 1, //不显示关闭按钮
  67. shade: [0],
  68. area: ['520px', '530px'],
  69. offset: 'rb', //右下角弹出
  70. anim: 2,
  71. content: [GOFLY_URL+'/chatIndex?kefu_id='+GOFLY_KEFU_ID+'&refer='+window.document.title, 'yes'], //iframe的url,no代表不显示滚动条
  72. end: function(){
  73. launchButtonFlag=false;
  74. $(".launchButton").show();
  75. }
  76. });
  77. launchButtonFlag=true;
  78. $(".launchButton").hide();
  79. $("body").click(function () {
  80. clearTimeout(titleTimer);
  81. document.title = originTitle;
  82. });
  83. }
  84. function dynamicLoadCss(url) {
  85. var head = document.getElementsByTagName('head')[0];
  86. var link = document.createElement('link');
  87. link.type='text/css';
  88. link.rel = 'stylesheet';
  89. link.href = url;
  90. head.appendChild(link);
  91. }
  92. function dynamicLoadJs(url, callback) {
  93. var head = document.getElementsByTagName('head')[0];
  94. var script = document.createElement('script');
  95. script.type = 'text/javascript';
  96. script.src = url;
  97. if(typeof(callback)=='function'){
  98. script.onload = script.onreadystatechange = function () {
  99. if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete"){
  100. callback();
  101. script.onload = script.onreadystatechange = null;
  102. }
  103. };
  104. }
  105. head.appendChild(script);
  106. }
  107. function flashTitle() {
  108. titleNum++;
  109. if (titleNum >=3) {
  110. titleNum = 1;
  111. }
  112. if (titleNum == 1) {
  113. document.title = '【】' + originTitle;
  114. }
  115. if (titleNum == 2) {
  116. document.title = '【你有一条消息】' + originTitle;
  117. }
  118. titleTimer = setTimeout("flashTitle()", 500);
  119. }
  120. window.addEventListener('message',function(e){
  121. var msg=e.data;
  122. if(msg.type=="message"){
  123. flashTitle();//标题闪烁
  124. }
  125. });
  126. window.onfocus = function () {
  127. clearTimeout(titleTimer);
  128. document.title = originTitle;
  129. };