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.

530 lines
16 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
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 if (typeof window._pjaxGetWelcomeText === 'function') {
  123. // 复用 pjax.js 中的欢迎语生成函数,避免重复逻辑
  124. text = window._pjaxGetWelcomeText();
  125. } else {
  126. // pjax.js 尚未加载时的兜底(首次访问且 pjax.js 在 message.js 之后加载)
  127. if (window.location.pathname == "/") {
  128. var now = (new Date()).getHours();
  129. if (now > 23 || now <= 5) {
  130. text = '你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?';
  131. } else if (now > 5 && now <= 7) {
  132. text = '早上好!一日之计在于晨,美好的一天就要开始了!';
  133. } else if (now > 7 && now <= 11) {
  134. text = '上午好!工作顺利嘛,不要久坐,多起来走动走动哦!';
  135. } else if (now > 11 && now <= 14) {
  136. text = '中午了,工作了一个上午,现在是午餐时间!';
  137. } else if (now > 14 && now <= 17) {
  138. text = '午后很容易犯困呢,今天的运动目标完成了吗?';
  139. } else if (now > 17 && now <= 19) {
  140. text = '傍晚了!窗外夕阳的景色很美丽呢,最美不过夕阳红~~';
  141. } else if (now > 19 && now <= 21) {
  142. text = '晚上好,今天过得怎么样?';
  143. } else if (now > 21 && now <= 23) {
  144. text = '已经这么晚了呀,早点休息吧,晚安~~';
  145. } else {
  146. text = '嗨~ 快来逗我玩吧!';
  147. }
  148. } else {
  149. text = '欢迎阅读<span style="color:#0099cc;">「 ' + document.title.split(' | ')[0] + ' 」</span>';
  150. }
  151. }
  152. showMessage(text, 12000);
  153. })();
  154. liveTlakTimer = setInterval(function () {
  155. showHitokoto();
  156. }, 15000);
  157. function showHitokoto() {
  158. if (sessionStorage.getItem("Sleepy") !== "1") {
  159. if (!AITalkFlag) {
  160. $.getJSON('https://hitokoto.mayx.eu.org/', function (result) {
  161. talkValTimer();
  162. showMessage(result.hitokoto, 0);
  163. });
  164. }
  165. } else {
  166. hideMessage(0);
  167. if (sleepTimer_ == null) {
  168. sleepTimer_ = setInterval(function () {
  169. checkSleep();
  170. }, 200);
  171. }
  172. console.log(sleepTimer_);
  173. }
  174. }
  175. window._live2d.showHitokoto = showHitokoto;
  176. function checkSleep() {
  177. var sleepStatu = sessionStorage.getItem("Sleepy");
  178. if (sleepStatu !== '1') {
  179. talkValTimer();
  180. showMessage('你回来啦~', 0);
  181. clearInterval(sleepTimer_);
  182. sleepTimer_ = null;
  183. }
  184. }
  185. function showMessage(text, timeout) {
  186. if (Array.isArray(text)) text = text[Math.floor(Math.random() * text.length + 1) - 1];
  187. //console.log('showMessage', text);
  188. $('.message').stop();
  189. if (typeof EventSource !== 'undefined' && text instanceof EventSource) {
  190. var outputContainer = $('.message')[0];
  191. var eventFlag = false;
  192. text.onmessage = function (event) {
  193. if (event.data == "[DONE]") {
  194. text.close();
  195. return;
  196. } else {
  197. if (!eventFlag) {
  198. talkValTimer();
  199. outputContainer.textContent = "";
  200. eventFlag = true;
  201. }
  202. var data = JSON.parse(event.data);
  203. if (data.response) {
  204. outputContainer.textContent += data.response;
  205. }
  206. }
  207. }
  208. } else {
  209. $('.message').html(text);
  210. }
  211. $('.message').fadeTo(200, 1);
  212. //if (timeout === null) timeout = 5000;
  213. //hideMessage(timeout);
  214. }
  215. window._live2d.showMessage = showMessage;
  216. function talkValTimer() {
  217. $('#live_talk').val('1');
  218. }
  219. function hideMessage(timeout) {
  220. //$('.message').stop().css('opacity',1);
  221. if (timeout === null) timeout = 5000;
  222. $('.message').delay(timeout).fadeTo(200, 0);
  223. }
  224. function initLive2d() {
  225. $("#landlord").mouseenter(function () {
  226. $(".live_ico_box").fadeIn();
  227. });
  228. $("#landlord").mouseleave(function () {
  229. $(".live_ico_box").fadeOut();
  230. });
  231. $('#hideButton').on('click', function () {
  232. if (AIFadeFlag) {
  233. return false;
  234. } else {
  235. AIFadeFlag = true;
  236. localStorage.setItem("live2dhidden", "0");
  237. $('#landlord').fadeOut(200);
  238. $('#open_live2d').delay(200).fadeIn(200);
  239. setTimeout(function () {
  240. AIFadeFlag = false;
  241. }, 300);
  242. }
  243. });
  244. $('#open_live2d').on('click', function () {
  245. if (AIFadeFlag) {
  246. return false;
  247. } else {
  248. AIFadeFlag = true;
  249. localStorage.setItem("live2dhidden", "1");
  250. $('#open_live2d').fadeOut(200);
  251. $('#landlord').delay(200).fadeIn(200);
  252. setTimeout(function () {
  253. AIFadeFlag = false;
  254. }, 300);
  255. }
  256. });
  257. $('#youduButton').on('click', function () {
  258. if ($('#youduButton').hasClass('doudong')) {
  259. var typeIs = $('#youduButton').attr('data-type');
  260. $('#youduButton').removeClass('doudong');
  261. $('body').removeClass(typeIs);
  262. $('#youduButton').attr('data-type', '');
  263. } else {
  264. var duType = $('#duType').val();
  265. var duArr = duType.split(",");
  266. var dataType = duArr[Math.floor(Math.random() * duArr.length)];
  267. $('#youduButton').addClass('doudong');
  268. $('#youduButton').attr('data-type', dataType);
  269. $('body').addClass(dataType);
  270. }
  271. });
  272. if (talkAPI !== "" && typeof EventSource !== 'undefined') {
  273. $('#showInfoBtn').on('click', function () {
  274. var live_statu = $('#live_statu_val').val();
  275. if (live_statu == "0") {
  276. return
  277. } else {
  278. $('#live_statu_val').val("0");
  279. $('.live_talk_input_body').fadeOut(500);
  280. AITalkFlag = false;
  281. showHitokoto();
  282. $('#showTalkBtn').show();
  283. $('#showInfoBtn').hide();
  284. }
  285. });
  286. $('#showTalkBtn').on('click', function () {
  287. var live_statu = $('#live_statu_val').val();
  288. if (live_statu == "1") {
  289. return
  290. } else {
  291. $('#live_statu_val').val("1");
  292. $('.live_talk_input_body').fadeIn(500);
  293. AITalkFlag = true;
  294. $('#showTalkBtn').hide();
  295. $('#showInfoBtn').show();
  296. }
  297. });
  298. $('#live_talk_input_form').on('submit', function (e) {
  299. e.preventDefault();
  300. var info_ = $('#AIuserText').val();
  301. // var userid_ = $('#AIuserName').val();
  302. let add_id = "";
  303. if ($('#load_this').prop("checked")) {
  304. add_id = "&id=" + encodeURIComponent($('#post_id').val());
  305. }
  306. if (info_ == "") {
  307. showMessage('写点什么吧!', 0);
  308. return;
  309. }
  310. showMessage('思考中~', 0);
  311. showMessage(new EventSource(talkAPI + "?info=" + encodeURIComponent(info_) + add_id));
  312. });
  313. } else {
  314. $('#showInfoBtn').hide();
  315. $('#showTalkBtn').hide();
  316. }
  317. // //获取用户名
  318. // var live2dUser = sessionStorage.getItem("live2duser");
  319. // if(live2dUser !== null){
  320. // $('#AIuserName').val(live2dUser);
  321. // }
  322. //获取位置
  323. var landL = sessionStorage.getItem("historywidth");
  324. var landB = sessionStorage.getItem("historyheight");
  325. if (landL == null || landB == null) {
  326. landL = '5px'
  327. landB = '0px'
  328. }
  329. $('#landlord').css('left', landL + 'px');
  330. $('#landlord').css('bottom', landB + 'px');
  331. //移动
  332. function getEvent() {
  333. return window.event || arguments.callee.caller.arguments[0];
  334. }
  335. var smcc = document.getElementById("landlord");
  336. var moveX = 0;
  337. var moveY = 0;
  338. var moveBottom = 0;
  339. var moveLeft = 0;
  340. var moveable = false;
  341. var docMouseMoveEvent = document.onmousemove;
  342. var docMouseUpEvent = document.onmouseup;
  343. smcc.onmousedown = function () {
  344. var ent = getEvent();
  345. moveable = true;
  346. moveX = ent.clientX;
  347. moveY = ent.clientY;
  348. var obj = smcc;
  349. moveBottom = parseInt(obj.style.bottom);
  350. moveLeft = parseInt(obj.style.left);
  351. if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
  352. window.getSelection().removeAllRanges();
  353. }
  354. document.onmousemove = function () {
  355. if (moveable) {
  356. var ent = getEvent();
  357. var x = moveLeft + ent.clientX - moveX;
  358. var y = moveBottom + (moveY - ent.clientY);
  359. obj.style.left = x + "px";
  360. obj.style.bottom = y + "px";
  361. }
  362. };
  363. document.onmouseup = function () {
  364. if (moveable) {
  365. var historywidth = obj.style.left;
  366. var historyheight = obj.style.bottom;
  367. historywidth = historywidth.replace('px', '');
  368. historyheight = historyheight.replace('px', '');
  369. sessionStorage.setItem("historywidth", historywidth);
  370. sessionStorage.setItem("historyheight", historyheight);
  371. document.onmousemove = docMouseMoveEvent;
  372. document.onmouseup = docMouseUpEvent;
  373. moveable = false;
  374. moveX = 0;
  375. moveY = 0;
  376. moveBottom = 0;
  377. moveLeft = 0;
  378. }
  379. };
  380. };
  381. //获取音乐信息初始化
  382. var $bgm = $('#live2d_bgm');
  383. // 音乐按钮点击事件(幂等,使用命名空间避免重复绑定)
  384. $('#musicButton').off('click._bgm').on('click._bgm', function () {
  385. if ($('#musicButton').hasClass('play')) {
  386. $bgm[0].pause();
  387. $('#musicButton').removeClass('play');
  388. sessionStorage.setItem("live2dBGM_IsPlay", '1');
  389. } else {
  390. $bgm[0].play();
  391. $('#musicButton').addClass('play');
  392. sessionStorage.setItem("live2dBGM_IsPlay", '0');
  393. }
  394. });
  395. // BGM 事件监听(仅绑定一次,使用标志位避免重复)
  396. if (!window._live2d._bgmEventsBound) {
  397. $bgm[0].addEventListener("timeupdate", function () {
  398. sessionStorage.setItem("live2dBGM_PlayTime", $bgm[0].currentTime);
  399. });
  400. $bgm[0].addEventListener("ended", function () {
  401. var listNow = parseInt($bgm.attr('data-bgm'));
  402. listNow++;
  403. var inputs = $('input[name=live2dBGM]');
  404. if (inputs.length === 0) return;
  405. if (listNow > inputs.length - 1) {
  406. listNow = 0;
  407. }
  408. var listNewSrc = inputs.eq(listNow).val();
  409. if (!listNewSrc) return;
  410. sessionStorage.setItem("live2dBGM_Num", listNow);
  411. $bgm.attr('src', listNewSrc);
  412. $bgm[0].play();
  413. $bgm.attr('data-bgm', listNow);
  414. });
  415. $bgm[0].addEventListener("error", function () {
  416. $bgm[0].pause();
  417. $('#musicButton').removeClass('play');
  418. showMessage('音乐似乎加载不出来了呢!', 0);
  419. });
  420. window.onbeforeunload = function () {
  421. sessionStorage.setItem("live2dBGM_WindowClose", '0');
  422. if ($('#musicButton').hasClass('play')) {
  423. sessionStorage.setItem("live2dBGM_IsPlay", '0');
  424. }
  425. };
  426. window._live2d._bgmEventsBound = true;
  427. }
  428. // 初始化 BGM(根据当前页面是否有 BGM 输入)
  429. if (typeof window._live2d.initBGM === 'function') {
  430. window._live2d.initBGM();
  431. }
  432. }
  433. // 暴露 BGM 初始化函数,供 PJAX 重初始化时调用
  434. window._live2d.initBGM = function() {
  435. var bgmListInfo = $('input[name=live2dBGM]');
  436. var $bgm = $('#live2d_bgm');
  437. if (bgmListInfo.length === 0) {
  438. $('#musicButton').hide();
  439. if ($bgm.length) $bgm[0].pause();
  440. return;
  441. }
  442. var bgmPlayNow = parseInt($bgm.attr('data-bgm')) || 0;
  443. var bgmPlayTime = 0;
  444. var live2dBGM_Num = sessionStorage.getItem("live2dBGM_Num");
  445. var live2dBGM_PlayTime = sessionStorage.getItem("live2dBGM_PlayTime");
  446. if (live2dBGM_Num) {
  447. if (parseInt(live2dBGM_Num) <= bgmListInfo.length - 1) {
  448. bgmPlayNow = parseInt(live2dBGM_Num);
  449. }
  450. }
  451. if (live2dBGM_PlayTime) {
  452. bgmPlayTime = parseFloat(live2dBGM_PlayTime);
  453. }
  454. var newSrc = bgmListInfo.eq(bgmPlayNow).val();
  455. $bgm.attr('data-bgm', bgmPlayNow);
  456. if ($bgm.attr('src') !== newSrc) {
  457. $bgm[0].pause();
  458. $bgm.attr('src', newSrc);
  459. $bgm[0].currentTime = bgmPlayTime;
  460. }
  461. $bgm[0].volume = 0.5;
  462. var live2dBGM_IsPlay = sessionStorage.getItem("live2dBGM_IsPlay");
  463. var live2dBGM_WindowClose = sessionStorage.getItem("live2dBGM_WindowClose");
  464. if (live2dBGM_IsPlay == '0' && live2dBGM_WindowClose == '0') {
  465. $bgm[0].play();
  466. $('#musicButton').addClass('play');
  467. }
  468. sessionStorage.setItem("live2dBGM_WindowClose", '1');
  469. $('#musicButton').show();
  470. };
  471. $(document).ready(function () {
  472. var AIimgSrc = [
  473. message_Path + "model/histoire/histoire.1024/texture_00.png",
  474. message_Path + "model/histoire/histoire.1024/texture_01.png",
  475. message_Path + "model/histoire/histoire.1024/texture_02.png",
  476. message_Path + "model/histoire/histoire.1024/texture_03.png"
  477. ]
  478. var images = [];
  479. var imgLength = AIimgSrc.length;
  480. var loadingNum = 0;
  481. for (var i = 0; i < imgLength; i++) {
  482. images[i] = new Image();
  483. images[i].src = AIimgSrc[i];
  484. images[i].onload = function () {
  485. loadingNum++;
  486. if (loadingNum === imgLength) {
  487. var live2dhidden = localStorage.getItem("live2dhidden");
  488. if (live2dhidden === "0") {
  489. setTimeout(function () {
  490. $('#open_live2d').fadeIn(200);
  491. }, 1300);
  492. } else {
  493. setTimeout(function () {
  494. $('#landlord').fadeIn(200);
  495. }, 1300);
  496. }
  497. setTimeout(function () {
  498. loadlive2d("live2d", message_Path + "model/histoire/model.json");
  499. }, 1000);
  500. initLive2d();
  501. images = null;
  502. }
  503. }
  504. }
  505. });
  506. }