Mayx's Home Page https://mabbs.github.io
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.

77 lines
2.5 KiB

  1. $(function () {
  2. $("div#landlord").mouseenter(function () {
  3. $("div.live_ico_box").fadeIn();
  4. });
  5. $("div#landlord").mouseleave(function () {
  6. $("div.live_ico_box").fadeOut();
  7. });
  8. const urlParams = new URLSearchParams(window.location.search);
  9. const keyword = urlParams.get('kw')?.trim();
  10. if (!keyword) return;
  11. // 转义正则表达式特殊字符,避免安全问题
  12. const escapedKeyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  13. // 创建不区分大小写的正则表达式(全局匹配)
  14. const regex = new RegExp(`(${escapedKeyword})`, 'gi');
  15. // 递归遍历并高亮文本节点
  16. const escapeHTML = str => str.replace(/[&<>"']/g,
  17. tag => ({
  18. '&': '&amp;',
  19. '<': '&lt;',
  20. '>': '&gt;',
  21. '"': '&quot;',
  22. "'": '&#39;'
  23. }[tag] || tag));
  24. function highlightTextNodes(element) {
  25. $(element).contents().each(function () {
  26. if (this.nodeType === Node.TEXT_NODE) {
  27. const $this = $(this);
  28. const text = escapeHTML($this.text());
  29. // 使用正则替换并保留原始大小写
  30. if (regex.test(text)) {
  31. const replaced = text.replace(regex, '<mark>$1</mark>');
  32. $this.replaceWith(replaced);
  33. }
  34. } else if (
  35. this.nodeType === Node.ELEMENT_NODE &&
  36. !$(this).is('script, style, noscript, textarea')
  37. ) {
  38. highlightTextNodes(this);
  39. }
  40. });
  41. }
  42. $('section').each(function () {
  43. highlightTextNodes(this);
  44. });
  45. var codeBlocks = document.querySelectorAll('div.highlight');
  46. codeBlocks.forEach(function (codeBlock) {
  47. var copyButton = document.createElement('button');
  48. copyButton.className = 'copy';
  49. copyButton.type = 'button';
  50. copyButton.innerText = '📋';
  51. codeBlock.append(copyButton);
  52. copyButton.addEventListener('click', function () {
  53. var code = codeBlock.querySelector('pre code').innerText.trim();
  54. window.navigator.clipboard.writeText(code)
  55. .then(() => {
  56. copyButton.innerText = '✅';
  57. })
  58. .catch(err => {
  59. copyButton.innerText = '❌';
  60. console.error('Failed to copy:', err);
  61. });
  62. setTimeout(function () {
  63. copyButton.innerText = '📋';
  64. }, 1500);
  65. });
  66. });
  67. });

Powered by TurnKey Linux.