Skip to content

import survey bookmarklet

tbaggaley edited this page Jul 7, 2021 · 7 revisions

Import a survey bookmarklet

Context

Surveys can be imported between environments (e.g. local -> staging, staging -> local) using an API call to the /import endpoint of Author API, passing in the questionnaire JSON to import as request body and providing a valid Authorization header matching the currently logged in user.

To facilitate this process, an "import a survey" bookmarklet was created which provides a step-by-step instructions to grab foreign questionnaire JSON and import it into the active environment. It will automatically grab your current Authorization credentials and makes importing much faster than using an external tool such as Postman.

How to use

Create a bookmark in your browser and set the URL to be the following:

javascript:(function()%7Bdocument.write(%60%0A%3Ch1%3ESurvey%20import%20tool%3C%2Fh1%3E%0A%3Ch3%3E%201.%20Enter%20URL%20to%20the%20questionnaire%20you'd%20like%20to%20import%3A%20%3C%2Fh3%3E%0A%3Cinput%20type%3D%22text%22%20id%3D%22url%22%20style%3D%22width%3A%2033%25%22%3E%0A%3Ch3%3E%202.%20Open%20the%20following%20URL%20and%20copy%20the%20contents%20to%20your%20clipboard%3A%20%3C%2Fh3%3E%0A%3Cspan%20id%3D%22exportURL%22%3E%20Enter%20a%20questionnaire%20URL%20above%20to%20see%20the%20export%20URL%20%3C%2Fspan%3E%0A%3Ch3%3E%203.%20Paste%20the%20contents%20in%20the%20box%20below%20%3C%2Fh3%3E%0A%3Ctextarea%20id%3D'schema'%20style%3D'width%3A50%25%3B%20height%3A33%25'%3E%3C%2Ftextarea%3E%0A%3Ch3%3E%204.%20Hit%20the%20button%20to%20import%20the%20survey%20and%20open%20it%20for%20viewing%20%3C%2Fh3%3E%0A%3Cbutton%20id%3D%22importButton%22%3EImport%20survey%3C%2Fbutton%3E%0A%60)%3B%0A%0Adocument.body.style%20%3D%20%22text-align%3A%20center%3B%20font-family%3A%20sans-serif%3B%22%3B%0A%0Adocument.getElementById(%22url%22).oninput%20%3D%20e%20%3D%3E%20%7B%0A%20%20%20%20const%20exportURLelem%20%3D%20document.getElementById(%22exportURL%22)%3B%0A%20%20%20%20const%20matches%20%3D%20e.target.value.match(%2F%5E(.%2B)%5C%2F.%2Bq%5C%2F(.%2B%3F)%5C%2F%2F)%3B%0A%20%20%20%20if(matches.length%20%3C%203)%0A%20%20%20%20%20%20%20%20exportURLelem.innerText%20%3D%20%22Enter%20a%20questionnaire%20URL%20above%20to%20see%20the%20export%20URL%22%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20exportURLelem.innerHTML%20%3D%20%60%3Ca%20href%3D%22%24%7Bmatches%5B1%5D%7D%2Fexport%2F%24%7Bmatches%5B2%5D%7D%22%20target%3D%22_blank%22%3E%20Click%20here%20to%20view%20Author%20schema%20JSON%20%3C%2Fa%3E%60%3B%0A%7D%3B%0A%0Adocument.getElementById(%22importButton%22).onclick%20%3D%20async%20()%20%3D%3E%20%7B%0A%20%20%20%20const%20schema%20%3D%20document.getElementById(%22schema%22).value.trim()%3B%0A%20%20%20%20if(!schema)%20return%3B%0A%0A%20%20%20%20const%20host%20%3D%20window.location.host%3B%0A%20%20%20%20const%20addr%20%3D%20(host.startsWith(%22localhost%22)%20%3F%20%22http%3A%2F%2Flocalhost%3A4000%22%20%3A%20%60https%3A%2F%2F%24%7Bhost%7D%60)%20%2B%20%22%2Fimport%22%3B%0A%0A%20%20%20%20const%20result%20%3D%20await%20fetch(addr%2C%20%7B%0A%20%20%20%20%20%20%20%20method%3A%20%22POST%22%2C%0A%20%20%20%20%20%20%20%20headers%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Content-Type%22%3A%20%22application%2Fjson%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Authorization%22%3A%20%22Bearer%20%22%20%2B%20localStorage.accessToken%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20body%3A%20document.getElementById(%22schema%22).value.trim()%2C%0A%20%20%20%20%7D)%3B%0A%0A%20%20%20%20const%20json%20%3D%20await%20result.json()%3B%0A%0A%20%20%20%20if(json.id)%0A%20%20%20%20%20%20%20%20window.location%20%3D%20%60%2Fgo%23%2Fq%2F%24%7Bjson.id%7D%60%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20alert(%22Unable%20to%20add%20survey%20-%20check%20the%20JSON%20is%20valid%22)%3B%0A%7D%3B%0A%7D)()

To use the tool, go to the target environment (e.g. localhost:3000 if you're importing a prod questionnaire to your local dev machine). Once logged into eQ Author, activate the bookmarklet and follow the instructions.

The code that this was generated from is available at the following gist:

https://gist.github.com/tbaggaley/fb4d30c5fa26e0ff3e5aba98cf09111f

Clone this wiki locally