Mayx's Home Page
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

503 lines
15 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. var userAgent = window.navigator.userAgent.toLowerCase();
  2. console.log(userAgent);
  3. var norunAI = ["android", "iphone", "ipod", "ipad", "windows phone"];
  4. var norunFlag = false;
  5. for (var i = 0; i < norunAI.length; i++) {
  6. if (userAgent.indexOf(norunAI[i]) > -1) {
  7. norunFlag = true;
  8. break;
  9. }
  10. }
  11. if (!window.WebGLRenderingContext) {
  12. norunFlag = true;
  13. }
  14. if (!norunFlag) {
  15. var hitFlag = false;
  16. var AIFadeFlag = false;
  17. var liveTlakTimer = null;
  18. var sleepTimer_ = null;
  19. var AITalkFlag = false;
  20. var talkNum = 0;
  21. // 暴露到全局,供 pjax.js 在页面切换后重新调用
  22. window._live2d = { initTips: null, showMessage: null, showHitokoto: null };
  23. (function () {
  24. function renderTip(template, context) {
  25. var tokenReg = /(\\)?\{([^\{\}\\]+)(\\)?\}/g;
  26. return template.replace(tokenReg, function (word, slash1, token, slash2) {
  27. if (slash1 || slash2) {
  28. return word.replace(/\\/g, '');
  29. }
  30. var variables = token.replace(/\s/g, '').split('.');
  31. var currentObject = context;
  32. var i, length, variable;
  33. for (i = 0, length = variables.length; i < length; ++i) {
  34. variable = currentObject[variables[i]];
  35. if (variable === undefined || variable === null) return '';
  36. currentObject = variable;
  37. }
  38. return String(currentObject);
  39. });
  40. }
  41. String.prototype.renderTip = function (context) {
  42. return renderTip(this, context);
  43. };
  44. var re = /x/;
  45. console.log(re);
  46. re.toString = function () {
  47. showMessage('哈哈,你打开了控制台,是想要看看我的秘密吗?', 5000);
  48. return '';
  49. };
  50. $(document).on('copy', function () {
  51. showMessage('你都复制了些什么呀,转载要记得加上出处哦~~', 5000);
  52. });
  53. // 缓存 message.json 数据,供 PJAX 重绑定使用
  54. var tipsData = null;
  55. function initTips() {
  56. $.ajax({
  57. cache: true,
  58. url: message_Path + 'message.json',
  59. dataType: "json",
  60. success: function (result) {
  61. tipsData = result;
  62. // 解绑旧事件(用命名空间避免影响其他绑定)
  63. $.each(result.mouseover, function (index, tips) {
  64. $(tips.selector).off('mouseover._live2d_tips mouseout._live2d_tips');
  65. $(tips.selector).on('mouseover._live2d_tips', function () {
  66. var text = tips.text;
  67. if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length + 1) - 1];
  68. text = text.renderTip({ text: $(this).text() });
  69. showMessage(text, 3000);
  70. talkValTimer();
  71. clearInterval(liveTlakTimer);
  72. liveTlakTimer = null;
  73. });
  74. $(tips.selector).on('mouseout._live2d_tips', function () {
  75. showHitokoto();
  76. if (liveTlakTimer == null) {
  77. liveTlakTimer = window.setInterval(function () {
  78. showHitokoto();
  79. }, 15000);
  80. };
  81. });
  82. });
  83. $.each(result.click, function (index, tips) {
  84. $(tips.selector).off('click._live2d_tips');
  85. $(tips.selector).on('click._live2d_tips', function () {
  86. if (hitFlag) {
  87. return false
  88. }
  89. hitFlag = true;
  90. setTimeout(function () {
  91. hitFlag = false;
  92. }, 8000);
  93. var text = tips.text;
  94. if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length + 1) - 1];
  95. text = text.renderTip({ text: $(this).text() });
  96. showMessage(text, 3000);
  97. });
  98. clearInterval(liveTlakTimer);
  99. liveTlakTimer = null;
  100. if (liveTlakTimer == null) {
  101. liveTlakTimer = window.setInterval(function () {
  102. showHitokoto();
  103. }, 15000);
  104. };
  105. });
  106. }
  107. });
  108. }
  109. window._live2d.initTips = initTips;
  110. initTips();
  111. var text;
  112. if (document.referrer !== '' && document.referrer.split('/')[2] !== window.location.host) {
  113. var referrer = document.createElement('a');
  114. referrer.href = document.referrer;
  115. var domain = referrer.hostname.split('.')[1];
  116. if (domain == 'baidu' || domain == 'so' || domain == 'google') {
  117. var source = domain == 'baidu' ? '百度搜索' : domain == 'so' ? '360搜索' : '谷歌搜索';
  118. text = '嗨! 来自 ' + source + ' 的朋友!<br>欢迎访问<span style="color:#0099cc;">「 ' + document.title.split(' | ')[0] + ' 」</span>';
  119. } else {
  120. text = '嗨!来自 <span style="color:#0099cc;">' + referrer.hostname + '</span> 的朋友!';
  121. }
  122. } else {
  123. text = getWelcomeText();
  124. }
  125. showMessage(text, 12000);
  126. })();
  127. liveTlakTimer = setInterval(function () {
  128. showHitokoto();
  129. }, 15000);
  130. function showHitokoto() {
  131. if (sessionStorage.getItem("Sleepy") !== "1") {
  132. if (!AITalkFlag) {
  133. $.getJSON('https://hitokoto.mayx.eu.org/', function (result) {
  134. talkValTimer();
  135. showMessage(result.hitokoto, 0);
  136. });
  137. }
  138. } else {
  139. hideMessage(0);
  140. if (sleepTimer_ == null) {
  141. sleepTimer_ = setInterval(function () {
  142. checkSleep();
  143. }, 200);
  144. }
  145. console.log(sleepTimer_);
  146. }
  147. }
  148. window._live2d.showHitokoto = showHitokoto;
  149. function checkSleep() {
  150. var sleepStatu = sessionStorage.getItem("Sleepy");
  151. if (sleepStatu !== '1') {
  152. talkValTimer();
  153. showMessage('你回来啦~', 0);
  154. clearInterval(sleepTimer_);
  155. sleepTimer_ = null;
  156. }
  157. }
  158. function showMessage(text, timeout) {
  159. if (Array.isArray(text)) text = text[Math.floor(Math.random() * text.length + 1) - 1];
  160. //console.log('showMessage', text);
  161. $('.message').stop();
  162. if (typeof EventSource !== 'undefined' && text instanceof EventSource) {
  163. var outputContainer = $('.message')[0];
  164. var eventFlag = false;
  165. text.onmessage = function (event) {
  166. if (event.data == "[DONE]") {
  167. text.close();
  168. return;
  169. } else {
  170. if (!eventFlag) {
  171. talkValTimer();
  172. outputContainer.textContent = "";
  173. eventFlag = true;
  174. }
  175. var data = JSON.parse(event.data);
  176. if (data.response) {
  177. outputContainer.textContent += data.response;
  178. }
  179. }
  180. }
  181. } else {
  182. $('.message').html(text);
  183. }
  184. $('.message').fadeTo(200, 1);
  185. //if (timeout === null) timeout = 5000;
  186. //hideMessage(timeout);
  187. }
  188. window._live2d.showMessage = showMessage;
  189. function talkValTimer() {
  190. $('#live_talk').val('1');
  191. }
  192. function hideMessage(timeout) {
  193. //$('.message').stop().css('opacity',1);
  194. if (timeout === null) timeout = 5000;
  195. $('.message').delay(timeout).fadeTo(200, 0);
  196. }
  197. function initLive2d() {
  198. $("#landlord").mouseenter(function () {
  199. $(".live_ico_box").fadeIn();
  200. });
  201. $("#landlord").mouseleave(function () {
  202. $(".live_ico_box").fadeOut();
  203. });
  204. $('#hideButton').on('click', function () {
  205. if (AIFadeFlag) {
  206. return false;
  207. } else {
  208. AIFadeFlag = true;
  209. localStorage.setItem("live2dhidden", "0");
  210. $('#landlord').fadeOut(200);
  211. $('#open_live2d').delay(200).fadeIn(200);
  212. setTimeout(function () {
  213. AIFadeFlag = false;
  214. }, 300);
  215. }
  216. });
  217. $('#open_live2d').on('click', function () {
  218. if (AIFadeFlag) {
  219. return false;
  220. } else {
  221. AIFadeFlag = true;
  222. localStorage.setItem("live2dhidden", "1");
  223. $('#open_live2d').fadeOut(200);
  224. $('#landlord').delay(200).fadeIn(200);
  225. setTimeout(function () {
  226. AIFadeFlag = false;
  227. }, 300);
  228. }
  229. });
  230. $('#youduButton').on('click', function () {
  231. if ($('#youduButton').hasClass('doudong')) {
  232. var typeIs = $('#youduButton').attr('data-type');
  233. $('#youduButton').removeClass('doudong');
  234. $('body').removeClass(typeIs);
  235. $('#youduButton').attr('data-type', '');
  236. } else {
  237. var duType = $('#duType').val();
  238. var duArr = duType.split(",");
  239. var dataType = duArr[Math.floor(Math.random() * duArr.length)];
  240. $('#youduButton').addClass('doudong');
  241. $('#youduButton').attr('data-type', dataType);
  242. $('body').addClass(dataType);
  243. }
  244. });
  245. if (talkAPI !== "" && typeof EventSource !== 'undefined') {
  246. $('#showInfoBtn').on('click', function () {
  247. var live_statu = $('#live_statu_val').val();
  248. if (live_statu == "0") {
  249. return
  250. } else {
  251. $('#live_statu_val').val("0");
  252. $('.live_talk_input_body').fadeOut(500);
  253. AITalkFlag = false;
  254. showHitokoto();
  255. $('#showTalkBtn').show();
  256. $('#showInfoBtn').hide();
  257. }
  258. });
  259. $('#showTalkBtn').on('click', function () {
  260. var live_statu = $('#live_statu_val').val();
  261. if (live_statu == "1") {
  262. return
  263. } else {
  264. $('#live_statu_val').val("1");
  265. $('.live_talk_input_body').fadeIn(500);
  266. AITalkFlag = true;
  267. $('#showTalkBtn').hide();
  268. $('#showInfoBtn').show();
  269. }
  270. });
  271. $('#live_talk_input_form').on('submit', function (e) {
  272. e.preventDefault();
  273. var info_ = $('#AIuserText').val();
  274. // var userid_ = $('#AIuserName').val();
  275. let add_id = "";
  276. if ($('#load_this').prop("checked")) {
  277. add_id = "&id=" + encodeURIComponent($('#post_id').val());
  278. }
  279. if (info_ == "") {
  280. showMessage('写点什么吧!', 0);
  281. return;
  282. }
  283. showMessage('思考中~', 0);
  284. showMessage(new EventSource(talkAPI + "?info=" + encodeURIComponent(info_) + add_id));
  285. });
  286. } else {
  287. $('#showInfoBtn').hide();
  288. $('#showTalkBtn').hide();
  289. }
  290. // //获取用户名
  291. // var live2dUser = sessionStorage.getItem("live2duser");
  292. // if(live2dUser !== null){
  293. // $('#AIuserName').val(live2dUser);
  294. // }
  295. //获取位置
  296. var landL = sessionStorage.getItem("historywidth");
  297. var landB = sessionStorage.getItem("historyheight");
  298. if (landL == null || landB == null) {
  299. landL = '5px'
  300. landB = '0px'
  301. }
  302. $('#landlord').css('left', landL + 'px');
  303. $('#landlord').css('bottom', landB + 'px');
  304. //移动
  305. function getEvent() {
  306. return window.event || arguments.callee.caller.arguments[0];
  307. }
  308. var smcc = document.getElementById("landlord");
  309. var moveX = 0;
  310. var moveY = 0;
  311. var moveBottom = 0;
  312. var moveLeft = 0;
  313. var moveable = false;
  314. var docMouseMoveEvent = document.onmousemove;
  315. var docMouseUpEvent = document.onmouseup;
  316. smcc.onmousedown = function () {
  317. var ent = getEvent();
  318. moveable = true;
  319. moveX = ent.clientX;
  320. moveY = ent.clientY;
  321. var obj = smcc;
  322. moveBottom = parseInt(obj.style.bottom);
  323. moveLeft = parseInt(obj.style.left);
  324. if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
  325. window.getSelection().removeAllRanges();
  326. }
  327. document.onmousemove = function () {
  328. if (moveable) {
  329. var ent = getEvent();
  330. var x = moveLeft + ent.clientX - moveX;
  331. var y = moveBottom + (moveY - ent.clientY);
  332. obj.style.left = x + "px";
  333. obj.style.bottom = y + "px";
  334. }
  335. };
  336. document.onmouseup = function () {
  337. if (moveable) {
  338. var historywidth = obj.style.left;
  339. var historyheight = obj.style.bottom;
  340. historywidth = historywidth.replace('px', '');
  341. historyheight = historyheight.replace('px', '');
  342. sessionStorage.setItem("historywidth", historywidth);
  343. sessionStorage.setItem("historyheight", historyheight);
  344. document.onmousemove = docMouseMoveEvent;
  345. document.onmouseup = docMouseUpEvent;
  346. moveable = false;
  347. moveX = 0;
  348. moveY = 0;
  349. moveBottom = 0;
  350. moveLeft = 0;
  351. }
  352. };
  353. };
  354. //获取音乐信息初始化
  355. var $bgm = $('#live2d_bgm');
  356. // 音乐按钮点击事件(幂等,使用命名空间避免重复绑定)
  357. $('#musicButton').off('click._bgm').on('click._bgm', function () {
  358. if ($('#musicButton').hasClass('play')) {
  359. $bgm[0].pause();
  360. $('#musicButton').removeClass('play');
  361. sessionStorage.setItem("live2dBGM_IsPlay", '1');
  362. } else {
  363. $bgm[0].play();
  364. $('#musicButton').addClass('play');
  365. sessionStorage.setItem("live2dBGM_IsPlay", '0');
  366. }
  367. });
  368. // BGM 事件监听(仅绑定一次,使用标志位避免重复)
  369. if (!window._live2d._bgmEventsBound) {
  370. $bgm[0].addEventListener("timeupdate", function () {
  371. sessionStorage.setItem("live2dBGM_PlayTime", $bgm[0].currentTime);
  372. });
  373. $bgm[0].addEventListener("ended", function () {
  374. var listNow = parseInt($bgm.attr('data-bgm'));
  375. listNow++;
  376. var inputs = $('input[name=live2dBGM]');
  377. if (inputs.length === 0) return;
  378. if (listNow > inputs.length - 1) {
  379. listNow = 0;
  380. }
  381. var listNewSrc = inputs.eq(listNow).val();
  382. if (!listNewSrc) return;
  383. sessionStorage.setItem("live2dBGM_Num", listNow);
  384. $bgm.attr('src', listNewSrc);
  385. $bgm[0].play();
  386. $bgm.attr('data-bgm', listNow);
  387. });
  388. $bgm[0].addEventListener("error", function () {
  389. $bgm[0].pause();
  390. $('#musicButton').removeClass('play');
  391. showMessage('音乐似乎加载不出来了呢!', 0);
  392. });
  393. window.onbeforeunload = function () {
  394. sessionStorage.setItem("live2dBGM_WindowClose", '0');
  395. if ($('#musicButton').hasClass('play')) {
  396. sessionStorage.setItem("live2dBGM_IsPlay", '0');
  397. }
  398. };
  399. window._live2d._bgmEventsBound = true;
  400. }
  401. // 初始化 BGM(根据当前页面是否有 BGM 输入)
  402. if (typeof window._live2d.initBGM === 'function') {
  403. window._live2d.initBGM();
  404. }
  405. }
  406. // 暴露 BGM 初始化函数,供 PJAX 重初始化时调用
  407. window._live2d.initBGM = function() {
  408. var bgmListInfo = $('input[name=live2dBGM]');
  409. var $bgm = $('#live2d_bgm');
  410. if (bgmListInfo.length === 0) {
  411. $('#musicButton').hide();
  412. if ($bgm.length) $bgm[0].pause();
  413. return;
  414. }
  415. var bgmPlayNow = parseInt($bgm.attr('data-bgm')) || 0;
  416. var bgmPlayTime = 0;
  417. var live2dBGM_Num = sessionStorage.getItem("live2dBGM_Num");
  418. var live2dBGM_PlayTime = sessionStorage.getItem("live2dBGM_PlayTime");
  419. if (live2dBGM_Num) {
  420. if (parseInt(live2dBGM_Num) <= bgmListInfo.length - 1) {
  421. bgmPlayNow = parseInt(live2dBGM_Num);
  422. }
  423. }
  424. if (live2dBGM_PlayTime) {
  425. bgmPlayTime = parseFloat(live2dBGM_PlayTime);
  426. }
  427. var newSrc = bgmListInfo.eq(bgmPlayNow).val();
  428. $bgm.attr('data-bgm', bgmPlayNow);
  429. if ($bgm.attr('src') !== newSrc) {
  430. $bgm[0].pause();
  431. $bgm.attr('src', newSrc);
  432. $bgm[0].currentTime = bgmPlayTime;
  433. }
  434. $bgm[0].volume = 0.5;
  435. var live2dBGM_IsPlay = sessionStorage.getItem("live2dBGM_IsPlay");
  436. var live2dBGM_WindowClose = sessionStorage.getItem("live2dBGM_WindowClose");
  437. if (live2dBGM_IsPlay == '0' && live2dBGM_WindowClose == '0') {
  438. $bgm[0].play();
  439. $('#musicButton').addClass('play');
  440. }
  441. sessionStorage.setItem("live2dBGM_WindowClose", '1');
  442. $('#musicButton').show();
  443. };
  444. $(document).ready(function () {
  445. var AIimgSrc = [
  446. message_Path + "model/histoire/histoire.1024/texture_00.png",
  447. message_Path + "model/histoire/histoire.1024/texture_01.png",
  448. message_Path + "model/histoire/histoire.1024/texture_02.png",
  449. message_Path + "model/histoire/histoire.1024/texture_03.png"
  450. ]
  451. var images = [];
  452. var imgLength = AIimgSrc.length;
  453. var loadingNum = 0;
  454. for (var i = 0; i < imgLength; i++) {
  455. images[i] = new Image();
  456. images[i].src = AIimgSrc[i];
  457. images[i].onload = function () {
  458. loadingNum++;
  459. if (loadingNum === imgLength) {
  460. var live2dhidden = localStorage.getItem("live2dhidden");
  461. if (live2dhidden === "0") {
  462. setTimeout(function () {
  463. $('#open_live2d').fadeIn(200);
  464. }, 1300);
  465. } else {
  466. setTimeout(function () {
  467. $('#landlord').fadeIn(200);
  468. }, 1300);
  469. }
  470. setTimeout(function () {
  471. loadlive2d("live2d", message_Path + "model/histoire/model.json");
  472. }, 1000);
  473. initLive2d();
  474. images = null;
  475. }
  476. }
  477. }
  478. });
  479. }