page.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Initialize Highlight.js for syntax highlighting
  2. if ( typeof hljs !== 'undefined' ) {
  3. hljs.highlightAll();
  4. }
  5. // Scroll to hash on page load
  6. ( function () {
  7. const hash = window.location.hash.substring( 1 );
  8. if ( hash ) {
  9. const element = document.getElementById( hash );
  10. if ( element ) element.scrollIntoView();
  11. }
  12. } )();
  13. // Update URL hash when clicking on method/property links
  14. ( function () {
  15. const h1 = document.querySelector( 'h1' );
  16. const className = h1 ? h1.textContent.trim() : null;
  17. if ( ! className ) return;
  18. document.addEventListener( 'click', function ( event ) {
  19. const target = event.target.closest( 'a' );
  20. if ( ! target || ! target.hash ) return;
  21. // Check if it's a same-page link (either starting with # or pointing to current page)
  22. const href = target.getAttribute( 'href' );
  23. const isSamePageLink = href.startsWith( '#' ) || ( target.hostname === window.location.hostname && target.pathname === window.location.pathname );
  24. if ( ! isSamePageLink ) return;
  25. const hash = target.hash.substring( 1 );
  26. const newHash = ( hash !== className ) ? `#${className}.${hash}` : `#${hash}`;
  27. const targetWindow = ( window.parent !== window ) ? window.parent : window;
  28. targetWindow.history.pushState( null, '', newHash );
  29. } );
  30. } )();
  31. // Add code copy buttons
  32. ( function addCopyButtons() {
  33. const elements = document.getElementsByTagName( 'pre' );
  34. for ( let i = 0; i < elements.length; i ++ ) {
  35. const element = elements[ i ];
  36. if ( element.classList.contains( 'linenums' ) === false ) {
  37. const copyButton = document.createElement( 'button' );
  38. copyButton.className = 'copy-btn';
  39. element.appendChild( copyButton );
  40. copyButton.addEventListener( 'click', function () {
  41. const codeContent = element.textContent;
  42. navigator.clipboard.writeText( codeContent ).then( () => {
  43. copyButton.classList.add( 'copied' );
  44. setTimeout( () => {
  45. copyButton.classList.remove( 'copied' );
  46. }, 1000 );
  47. } );
  48. } );
  49. }
  50. }
  51. } )();
粤ICP备19079148号