lesson.js 2.2 KB

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