lesson.js 2.3 KB

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