diff --git a/radioUpdater/assets/js/helperScripts.js b/radioUpdater/assets/js/helperScripts.js index 4f6171f..3e8ff96 100644 --- a/radioUpdater/assets/js/helperScripts.js +++ b/radioUpdater/assets/js/helperScripts.js @@ -34,3 +34,8 @@ function createHiddenDiv(id, child) return hiddenDiv } + +const kebabCase = string => string + .replace(/([a-z])([A-Z])/g, "$1-$2") + .replace(/[\s_]+/g, '-') + .toLowerCase(); \ No newline at end of file diff --git a/radioUpdater/assets/js/questions.js b/radioUpdater/assets/js/questions.js index 367f6ca..b93db19 100644 --- a/radioUpdater/assets/js/questions.js +++ b/radioUpdater/assets/js/questions.js @@ -1,7 +1,7 @@ const relatedArticlesSection = document.getElementById('mcq-related-articles') function updateAutoQuestion() { - + // Add the form. if (relatedArticlesSection) { const autoQuestionDiv = document.createElement('div') autoQuestionDiv.id = 'auto-questions' @@ -65,22 +65,37 @@ function watchForArticleUpdates() function getNewQuestion() { - let questionButton = document.getElementById('auto-question-button') - questionButton.disabled = true; - + // Get the question textarea. let questionTextarea = document.getElementById('for_multiple_choice_question_stem') let stemContent = questionTextarea.querySelectorAll('[contenteditable="true"]')[0] stemContent.id = 'question-content-textarea' + // Get the explanation textarea. let explanationContentEditable = document.getElementById('for_multiple_choice_question_explanation').querySelectorAll('[contenteditable="true"]')[0] - let article = document.getElementsByClassName('related-article-form-li')[0].getElementsByTagName('a')[0].getAttribute("href") + // Determine the type of question + let type = 'basic-factual' + let typeList = document.getElementsByClassName('question-category-titles')[0]; + let activeType = kebabCase(typeList.getElementsByClassName('active')[0].innerText) + + if (activeType === 'image-interpretation') { + alert("We can't auto-generate Image Interpretation questions yet. Change Type at the top of the page.") + return + } + // Disable the question generator button + let questionButton = document.getElementById('auto-question-button') + questionButton.disabled = true; + + // Get the first related article + let article = document.getElementsByClassName('related-article-form-li')[0].getElementsByTagName('a')[0].getAttribute("href") article = article.substring(article.lastIndexOf('/') + 1) + // Generate a prompt let prompt = document.getElementById('auto-question-input').value let payload = { + type: activeType, article: article, prompt: prompt, } @@ -88,6 +103,7 @@ function getNewQuestion() { chrome.runtime.sendMessage({question: payload}, function ({data}) { questionButton.disabled = false + if (data.error) { } else { @@ -125,7 +141,9 @@ function getNewQuestion() { let wrappedExplanation = document.createElement('p') wrappedExplanation.innerText = data.explanation explanationContentEditable.prepend(wrappedExplanation) - explanationContentEditable.querySelectorAll('[data-placeholder]')[0].remove() + + questionButton.innerText = 'Regenerate' + } }); diff --git a/radioUpdater/background.js b/radioUpdater/background.js index 9ecef48..3f50d4e 100644 --- a/radioUpdater/background.js +++ b/radioUpdater/background.js @@ -17,7 +17,7 @@ chrome.runtime.onMessage.addListener(function (message, sender, senderResponse) } else if(message.question) { - fetch('https://ai.radiopaedia.work/questions/api/' + message.question.article + '/' + message.question.prompt) + fetch('https://ai.radiopaedia.work/questions/' + message.question.type + '/' + message.question.article + '/' + message.question.prompt) .then(response => response.text()) .then(data => { let dataObj = JSON.parse(data) diff --git a/radioUpdater/manifest.json b/radioUpdater/manifest.json index fa21563..5b1e531 100644 --- a/radioUpdater/manifest.json +++ b/radioUpdater/manifest.json @@ -1,6 +1,6 @@ { "name": "RadioUpdater", - "version": "1.0", + "version": "2.0", "description": "Replace references on Radiopaedia.", "manifest_version": 3, "content_scripts": [