page.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Initialize prettify for syntax highlighting
  2. if ( typeof prettyPrint === 'function' ) {
  3. prettyPrint();
  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[href^="#"]' );
  20. if ( ! target || ! target.hash ) return;
  21. const hash = target.hash.substring( 1 );
  22. const newHash = ( hash !== className ) ? `#${className}.${hash}` : `#${hash}`;
  23. const targetWindow = ( window.parent !== window ) ? window.parent : window;
  24. targetWindow.history.pushState( null, '', newHash );
  25. } );
  26. } )();
  27. // Add code copy buttons
  28. ( function addCopyButtons() {
  29. const elements = document.getElementsByTagName( 'pre' );
  30. for ( let i = 0; i < elements.length; i ++ ) {
  31. const element = elements[ i ];
  32. if ( element.classList.contains( 'linenums' ) === false ) {
  33. const copyButton = document.createElement( 'button' );
  34. copyButton.className = 'copy-btn';
  35. element.appendChild( copyButton );
  36. copyButton.addEventListener( 'click', function () {
  37. const codeContent = element.textContent;
  38. navigator.clipboard.writeText( codeContent ).then( () => {
  39. copyButton.classList.add( 'copied' );
  40. setTimeout( () => {
  41. copyButton.classList.remove( 'copied' );
  42. }, 1000 );
  43. } );
  44. } );
  45. }
  46. }
  47. } )();
粤ICP备19079148号