lesson.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Licensed under a BSD license. See license.html for license
  2. 'use strict';
  3. ( function () {
  4. if ( ! window.frameElement && window.location.protocol !== 'file:' ) {
  5. // not in iframe: redirect to the framed manual
  6. const re = /^(.*?\/manual\/)(.*?)$/;
  7. const [ , baseURL, articlePath ] = re.exec( window.location.href );
  8. const href = `${baseURL}#${articlePath.replace( '.html', '' )}`;
  9. window.location.replace( href ); // lgtm[js/client-side-unvalidated-url-redirection]
  10. }
  11. const parts = window.location.href.split( '/' );
  12. const filename = parts[ parts.length - 1 ];
  13. if ( filename !== 'primitives.html' && filename !== 'prerequisites.html' ) {
  14. let text = document.body.innerHTML;
  15. text = text.replace( /\[link:([\w\:\/\.\-\_\(\)\?\#\=\!\~]+)\]/gi, '<a href="$1" target="_blank">$1</a>' ); // [link:url]
  16. text = text.replace( /\[link:([\w:/.\-_()?#=!~]+) ([\w\p{L}:/.\-_'\s]+)\]/giu, '<a href="$1" target="_blank">$2</a>' ); // [link:url title]
  17. text = text.replace( /\[example:([\w\_]+)\]/gi, '[example:$1 $1]' ); // [example:name] to [example:name title]
  18. text = text.replace( /\[example:([\w\_]+) ([\w\:\/\.\-\_ \s]+)\]/gi, '<a href="../../examples/#$1" target="_blank">$2</a>' ); // [example:name title]
  19. text = text.replace( /\`(.*?)\`/gs, '<code class="notranslate" translate="no">$1</code>' ); // `code`
  20. document.body.innerHTML = text;
  21. }
  22. if ( window.frameElement ) {
  23. // in iframe
  24. // Bind link handlers after the innerHTML above has been (re)written,
  25. // otherwise the reassignment discards the freshly attached listeners
  26. // and clicks fall back to native iframe navigation, leaving the parent
  27. // page's URL hash and sidebar out of sync.
  28. document.querySelectorAll( 'a' ).forEach( a => {
  29. // we have to send all links to the parent
  30. // otherwise we'll end up with 3rd party
  31. // sites under the frame.
  32. a.addEventListener( 'click', e => {
  33. // opening a new tab?
  34. if ( a.target === '_blank' ) {
  35. return;
  36. }
  37. // change changing hashes?
  38. if ( a.origin !== window.location.origin || a.pathname !== window.location.pathname ) {
  39. e.preventDefault();
  40. }
  41. window.parent.setUrl( a.href );
  42. } );
  43. } );
  44. window.parent.setTitle( document.title );
  45. }
  46. if ( window.prettyPrint ) {
  47. window.prettyPrint();
  48. }
  49. // help translation services translate comments.
  50. document.querySelectorAll( 'span[class=com]' ).forEach( elem => {
  51. elem.classList.add( 'translate', 'yestranslate' );
  52. elem.setAttribute( 'translate', 'yes' );
  53. } );
  54. if ( window.threejsLessonUtils ) {
  55. window.threejsLessonUtils.afterPrettify();
  56. }
  57. }() );
  58. // ios needs this to allow touch events in an iframe
  59. window.addEventListener( 'touchstart', {} );
粤ICP备19079148号