Created
June 29, 2020 22:00
-
-
Save KyleMit/287a240b8e2e7aa0c28da28f556d28d1 to your computer and use it in GitHub Desktop.
NBS Porting - Auto Mapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name NBS Porting - Auto Mapper | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Automatically map answers in the from and to answer fields when porting pages in NBS | |
// @author KyleMit | |
// @match https://ahs-nbs-dev.ahs.state.vt.us/nbs/PortPage.do | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
console.log("Mapping Load") | |
// check if mapping fields exist | |
let mappingIsLoaded = () => { | |
let fromIDValue = document.getElementById("fromQuestionID") | |
return !!fromIDValue.innerText | |
} | |
// open mapping fields for first row | |
let openMapping = () => { | |
console.log("Opening Mapping") | |
// get all map icons on current screen | |
let mapIcons = [...document.querySelectorAll("#parent [title=Map]")] | |
// get first icon | |
let mapIcon = mapIcons[0] | |
// start mapping current field | |
mapIcon.click() | |
} | |
// get possible values that we can map to | |
let getFromValues = () => { | |
return [...document.querySelectorAll("#toCodeList option")].map(o => { | |
let optionParts = o.innerText.split(":") | |
let optionCode = (optionParts[0] || "").trim() | |
let optionDesc = (optionParts[1] || "").trim() | |
let output = { | |
val: o.value, | |
text: o.innerText, | |
desc: optionDesc | |
} | |
return output | |
}) | |
} | |
// get 'to' value and assign to 'from' value | |
let mapValue = () => { | |
let fromCode = document.querySelector("#fromCode") | |
let fromDesc = document.querySelector("#fromCodeDesc") | |
let fromDescText = fromDesc.innerText | |
// get available values from to field | |
let possibleValues = getFromValues() | |
// get match *IF* it exists | |
let match = possibleValues.find(val => { | |
let isMatch = val.desc === fromDescText | |
return isMatch | |
}) | |
// stop if we don't get a match | |
if (!match) { | |
console.log("warning - we don't have a match") | |
return; | |
} | |
// assignment of value | |
console.log("got match", match.text) | |
// input get text part | |
let toInput = document.querySelector("[name=toCodeList_textbox]") | |
toInput.value = match.text | |
// select get val part | |
let toSelect = document.querySelector("#toCodeList") | |
toSelect.value = match.val | |
// save by clicking add button | |
let addBtn = document.getElementById("addButton") | |
addBtn.click() | |
} | |
// kick off mapping | |
let startMapping = () => { | |
console.log("Mapping Start") | |
// check if mapping is loaded | |
if (mappingIsLoaded()) { | |
mapValue() | |
} else { | |
// open mapping, wait, and map value | |
openMapping() | |
window.setTimeout(mapValue, 500) | |
} | |
} | |
// lazily wait for DOM to load | |
window.setTimeout(startMapping, 1500) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment