@@ -14,46 +14,54 @@ class NumbasExamElement extends HTMLElement {
1414 }
1515
1616 async load_exam ( ) {
17- await Numbas . awaitScripts ( [ 'start-exam' , 'display' ] ) ;
17+ try {
18+ await Numbas . awaitScripts ( [ 'start-exam' , 'display' ] ) ;
1819
19- const options = {
20- exam_url : this . getAttribute ( 'source_url' ) ,
21- scorm : this . getAttribute ( 'scorm' ) ?. toLowerCase ( ) !== 'false' ,
22- element : this
23- } ;
24- const exam_source_element = this . querySelector ( 'script[type="application/numbas-exam"]' ) ;
25- if ( exam_source_element ) {
26- options . exam_source = exam_source_element . textContent ;
27- }
20+ const options = {
21+ exam_url : this . getAttribute ( 'source_url' ) ,
22+ storage : this . getAttribute ( 'storage' ) ?. toLowerCase ( ) ,
23+ element : this
24+ } ;
25+ const exam_source_element = this . querySelector ( 'script[type="application/numbas-exam"]' ) ;
26+ if ( exam_source_element ) {
27+ options . exam_source = exam_source_element . textContent ;
28+ }
29+
30+ if ( ! ( options . exam_url || options . exam_source ) ) {
31+ return ;
32+ }
2833
29- const { exam_data} = await Numbas . load_exam ( options ) ;
34+ const { exam_data} = await Numbas . load_exam ( options ) ;
3035
31- let extension_data_json = this . getAttribute ( 'extensions' ) ;
32- const extension_data_element = this . querySelector ( 'script[slot="extension-data"]' ) ;
33- if ( extension_data_element ) {
34- extension_data_json = extension_data_element . textContent . trim ( ) ;
35- }
36- const extension_data = JSON . parse ( extension_data_json ) ;
37-
38- for ( const extension of exam_data . extensions ) {
39- const data = extension_data [ extension ] ;
40- Numbas . extension_url_root [ extension ] = data . root ;
41- for ( const js of data . javascripts ) {
42- const src = `${ data . root } /${ js } ` ;
43- if ( ! document . head . querySelector ( `script[data-numbas-extension="${ extension } "][src="${ src } "]` ) ) {
44- const script = document . createElement ( 'script' ) ;
45- script . src = src ;
46- script . dataset . numbasExtension = extension ;
47- document . head . appendChild ( script ) ;
48- }
36+ let extension_data_json = this . getAttribute ( 'extensions' ) ;
37+ const extension_data_element = this . querySelector ( 'script[slot="extension-data"]' ) ;
38+ if ( extension_data_element ) {
39+ extension_data_json = extension_data_element . textContent . trim ( ) ;
4940 }
50- for ( const css of data . stylesheets ) {
51- const link = document . createElement ( 'link' ) ;
52- link . rel = 'stylesheet' ;
53- const src = `${ data . root } /${ css } ` ;
54- link . href = src ;
55- document . head . appendChild ( link ) ;
41+ const extension_data = JSON . parse ( extension_data_json ) ;
42+
43+ for ( const extension of exam_data . extensions ) {
44+ const data = extension_data [ extension ] ;
45+ Numbas . extension_url_root [ extension ] = data . root ;
46+ for ( const js of data . javascripts ) {
47+ const src = `${ data . root } /${ js } ` ;
48+ if ( ! document . head . querySelector ( `script[data-numbas-extension="${ extension } "][src="${ src } "]` ) ) {
49+ const script = document . createElement ( 'script' ) ;
50+ script . src = src ;
51+ script . dataset . numbasExtension = extension ;
52+ document . head . appendChild ( script ) ;
53+ }
54+ }
55+ for ( const css of data . stylesheets ) {
56+ const link = document . createElement ( 'link' ) ;
57+ link . rel = 'stylesheet' ;
58+ const src = `${ data . root } /${ css } ` ;
59+ link . href = src ;
60+ this . shadowRoot . appendChild ( link ) ;
61+ }
5662 }
63+ } catch ( err ) {
64+ Numbas . display . die ( err ) ;
5765 }
5866 }
5967
0 commit comments