前提・実現したいこと
ウェブ上に画像、文字、テキストボックス、完了ボタンを表示させたく思っています。文字、テキストボックス、完了ボタンを実装してくれるAPIを使っているのですが、preambleというパラメータが文字列しかとらないらしく、画像ではなくパス名だけ表示されてしまいます。
preamble: jsPsych.timelineVariable('stimulus')を
preamble: "<img src=" + jsPsych.timelineVariable('stimulus') + ">"
にするとなぜか画像パスが'function()'に変換されてしまい、Failed to load resource: net::ERR_FILE_NOT_FOUNDになります。
jsPsych.timelineVariable('stimulus')は"パス名"だと思っていただければ良いです。パス名は合っています。
どなたかプラグインとの整合性を見ていただけないでしょうか?
該当のソースコード
javascript
1var survey_trial = { 2 type: 'survey-text', 3 questions: [{prompt: jsPsych.timelineVariable('instruction')}], 4 preamble: jsPsych.timelineVariable('stimulus')//ここに"パス名"が入ります。 5};
survey_trial plugin
https://www.jspsych.org/plugins/jspsych-survey-text/
javascript
1jsPsych.plugins['survey-text'] = (function() { 2 3 var plugin = {}; 4 5 plugin.info = { 6 name: 'survey-text', 7 description: '', 8 parameters: { 9 questions: { 10 type: jsPsych.plugins.parameterType.COMPLEX, 11 array: true, 12 pretty_name: 'Questions', 13 default: undefined, 14 nested: { 15 prompt: { 16 type: jsPsych.plugins.parameterType.STRING, 17 pretty_name: 'Prompt', 18 default: undefined, 19 description: 'Prompt for the subject to response' 20 }, 21 placeholder: { 22 type: jsPsych.plugins.parameterType.STRING, 23 pretty_name: 'Value', 24 default: "", 25 description: 'Placeholder text in the textfield.' 26 }, 27 rows: { 28 type: jsPsych.plugins.parameterType.INT, 29 pretty_name: 'Rows', 30 default: 1, 31 description: 'The number of rows for the response text box.' 32 }, 33 columns: { 34 type: jsPsych.plugins.parameterType.INT, 35 pretty_name: 'Columns', 36 default: 40, 37 description: 'The number of columns for the response text box.' 38 }, 39 required: { 40 type: jsPsych.plugins.parameterType.BOOL, 41 pretty_name: 'Required', 42 default: false, 43 description: 'Require a response' 44 }, 45 name: { 46 type: jsPsych.plugins.parameterType.STRING, 47 pretty_name: 'Question Name', 48 default: '', 49 description: 'Controls the name of data values associated with this question' 50 } 51 } 52 }, 53 preamble: { 54 type: jsPsych.plugins.parameterType.STRING, 55 pretty_name: 'Preamble', 56 default: null, 57 description: 'HTML formatted string to display at the top of the page above all the questions.' 58 }, 59 button_label: { 60 type: jsPsych.plugins.parameterType.STRING, 61 pretty_name: 'Button label', 62 default: 'Continue', 63 description: 'The text that appears on the button to finish the trial.' 64 } 65 } 66 } 67 68 plugin.trial = function(display_element, trial) { 69 70 for (var i = 0; i < trial.questions.length; i++) { 71 if (typeof trial.questions[i].rows == 'undefined') { 72 trial.questions[i].rows = 1; 73 } 74 } 75 for (var i = 0; i < trial.questions.length; i++) { 76 if (typeof trial.questions[i].columns == 'undefined') { 77 trial.questions[i].columns = 40; 78 } 79 } 80 for (var i = 0; i < trial.questions.length; i++) { 81 if (typeof trial.questions[i].value == 'undefined') { 82 trial.questions[i].value = ""; 83 } 84 } 85 86 var html = ''; 87 // show preamble text 88 if(trial.preamble !== null){ 89 html += '<div id="jspsych-survey-text-preamble" class="jspsych-survey-text-preamble">'+ trial.preamble +'</div>'; 90 } 91 // start form 92 html += '<form id="jspsych-survey-text-form">' 93 94 // generate question order 95 var question_order = []; 96 for(var i=0; i<trial.questions.length; i++){ 97 question_order.push(i); 98 } 99 if(trial.randomize_question_order){ 100 question_order = jsPsych.randomization.shuffle(question_order); 101 } 102 103 // add questions 104 for (var i = 0; i < trial.questions.length; i++) { 105 var question = trial.questions[question_order[i]]; 106 var question_index = question_order[i]; 107 html += '<div id="jspsych-survey-text-'+question_index+'" class="jspsych-survey-text-question" style="margin: 2em 0em;">'; 108 html += '<p class="jspsych-survey-text">' + question.prompt + '</p>'; 109 var autofocus = i == 0 ? "autofocus" : ""; 110 var req = question.required ? "required" : ""; 111 if(question.rows == 1){ 112 html += '<input type="text" id="input-'+question_index+'" name="#jspsych-survey-text-response-' + question_index + '" data-name="'+question.name+'" size="'+question.columns+'" '+autofocus+' '+req+' placeholder="'+question.placeholder+'"></input>'; 113 } else { 114 html += '<textarea id="input-'+question_index+'" name="#jspsych-survey-text-response-' + question_index + '" data-name="'+question.name+'" cols="' + question.columns + '" rows="' + question.rows + '" '+autofocus+' '+req+' placeholder="'+question.placeholder+'"></textarea>'; 115 } 116 html += '</div>'; 117 } 118 119 // add submit button 120 html += '<input type="submit" id="jspsych-survey-text-next" class="jspsych-btn jspsych-survey-text" value="'+trial.button_label+'"></input>'; 121 122 html += '</form>' 123 display_element.innerHTML = html; 124 125 // backup in case autofocus doesn't work 126 display_element.querySelector('#input-'+question_order[0]).focus(); 127 128 display_element.querySelector('#jspsych-survey-text-form').addEventListener('submit', function(e) { 129 e.preventDefault(); 130 // measure response time 131 var endTime = performance.now(); 132 var response_time = endTime - startTime; 133 134 // create object to hold responses 135 var question_data = {}; 136 137 for(var index=0; index < trial.questions.length; index++){ 138 var id = "Q" + index; 139 var q_element = document.querySelector('#jspsych-survey-text-'+index).querySelector('textarea, input'); 140 var val = q_element.value; 141 var name = q_element.attributes['data-name'].value; 142 if(name == ''){ 143 name = id; 144 } 145 var obje = {}; 146 obje[name] = val; 147 Object.assign(question_data, obje); 148 } 149 // save data 150 var trialdata = { 151 "rt": response_time, 152 "responses": JSON.stringify(question_data) 153 }; 154 155 display_element.innerHTML = ''; 156 157 // next trial 158 jsPsych.finishTrial(trialdata); 159 }); 160 161 var startTime = performance.now(); 162 }; 163 164 return plugin; 165})(); 166
あなたの回答
tips
プレビュー