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.

37 lines
1.4 KiB

  1. function highlightKeyword() {
  2. var match = location.search.match(/[?&]kw=([^&]+)/);
  3. var kw = match ? $.trim(decodeURIComponent(match[1].replace(/\+/g, ' '))) : '';
  4. if (!kw) return;
  5. var reg = new RegExp('(' + kw.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')', 'gi');
  6. var escapeMap = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
  7. $('section, section *').not('script, style, textarea').contents().filter(function() {
  8. return this.nodeType === 3;
  9. }).each(function() {
  10. var escapedText = this.nodeValue.replace(/[&<>"']/g, function(m) { return escapeMap[m]; });
  11. var highlighted = escapedText.replace(reg, '<mark>$1</mark>');
  12. if (escapedText !== highlighted) {
  13. $(this).replaceWith(highlighted);
  14. }
  15. });
  16. }
  17. function initCopyButtons() {
  18. $('.copy').remove();
  19. $('div.highlight').each(function () {
  20. var $btn = $('<button>', { class: 'copy', type: 'button', text: '📋' });
  21. $(this).append($btn);
  22. $btn.on('click', function () {
  23. var code = $btn.siblings('pre').find('code').text().trim();
  24. navigator.clipboard.writeText(code)
  25. .then(function () { $btn.text('✅'); })
  26. .catch(function () { $btn.text('❌'); })
  27. .finally(function () { setTimeout(function () { $btn.text('📋'); }, 1500); });
  28. });
  29. });
  30. }
  31. $(function () {
  32. highlightKeyword();
  33. initCopyButtons();
  34. });