User:Santhosh.thottingal/wq.js

// This is an experimental script to provide natural language querying for wikipedia. // The server is not guranteed to be running all the time. // Source code for the server at github.com/santhoshtr/wq // LICENSE: MIT // AUTHOR: Santhosh Thottingal (function {   const WQSERVER = "https://wq.thottingal.in";   let fetchQuestionPromise = null;   function fetchQuestions(language, title) {      return fetch(`${WQSERVER}/api/qa/${language}/${title}`)         .then((response) => response.json);   }

function renderQA(qaObj) { const $editLink = $('').addClass('edit-qa'). text('edit') const $saveAns = $(' ').addClass('mw-ui-button').text('Save').hide; $question = $(' ') .append(           $(' ').addClass('mw-headline').text(qaObj.question),            $(' ').addClass('mw-editsection')               .append($editLink)         ); $answer = $(' ').text(qaObj.answer); $editLink.bind("click", { ansEl: $answer, saveAnsEl: $saveAns }, function (event) {        event.data.ansEl            .addClass('editable')            .attr('contenteditable', true)            .focus;         event.data.saveAnsEl.show;      }); $saveAns.bind("click", { ansEl: $answer, saveAnsEl: $saveAns }, function (event) {        event.data.ansEl            .removeClass('editable')            .attr('contenteditable', false);         event.data.saveAnsEl.hide;      });

return  $(' ') .prop('id', qaObj.id) .addClass('qa') .append($question, $answer, $saveAns)

}

function renderQAs(qas) { const $qaContainer = $(' '); const qaList = []; const $askQn = $(' ').addClass('mw-ui-button').text('Ask a question'); for (let i = 0; i < qas.length; i++) { qaList.push(renderQA(qas[i])); }     $qaContainer.append(qaList, $askQn); $askQn.click(function{        let q = prompt("Ask a question", "");         if (q != null) {            const $newQ = renderQA({ question: q,              anwser: "" })           $newQ.insertBefore($askQn);            $newQ.find('.edit-qa').click;         }      }); return $qaContainer; }

function getTrigger { $wqTrigger = $('#ca-talk').clone; $wqTrigger.find('a') .text("Questions") .attr('href', '#') .attr('title', 'Questions and answers about the content page [alt-shift-q]') .attr('accesskey', 'q');

// Remove the new link class if exists $wqTrigger.removeClass('new'); $wqTrigger.prop('id', 'ca-qa'); return $wqTrigger; }

$(document).ready(function {      // Add this only to main name space      if (mw.config.get('wgNamespaceNumber') !== 0) return;

const title = mw.config.get('wgTitle'); const language = mw.config.get('wgContentLanguage'); const $wqTrigger = getTrigger; $wqTrigger.insertAfter('#ca-talk'); fetchQuestionPromise = fetchQuestionPromise || fetchQuestions(language, title); $wqTrigger.click(function (e) {        // Avoid the browser going to '#'         e.preventDefault;         $wqTrigger.addClass('selected');         $('#mw-content-text').text("Fetching questions...");         // Initiate QA!         fetchQuestionPromise.then((qas) => { $('#mw-content-text').hide; renderQAs(qas).insertAfter('#mw-content-text'); });     });   })

});