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.

72 lines
2.4 KiB

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

Powered by TurnKey Linux.