// ==UserScript==
// @name Flight Rising: Predict Morphology - Next Option + Image Code Copy
// @description Adds a button to select the next option for each dropdown. Automatically predicts, removing excess clicks. Adds option to copy scry image code with settings for auto copy & adult on load.
// @namespace https://greasyfork.org/en/users/547396
// @author https://greasyfork.org/en/users/547396
// @match *://*.flightrising.com/scrying/predict*
// @grant none
// @version 0.3
// ==/UserScript==
(function() {
'use strict';
const container = document.getElementById('predict-morphology'),
optionsBlock = container.getElementsByClassName('scry-options')[0],
commonRows = optionsBlock.getElementsByClassName('common-row'),
predictBtn = document.getElementById('scry-button'),
loadBtn = document.getElementById('load-morphology'),
scryLoad = document.getElementsByClassName('scry-load')[0],
copyBtn = document.createElement('button'),
copyPasta = document.createElement('input');
// do it!
init();
function init() {
buildCopy();
loadScryUrl();
loadSettings();
for ( let row of commonRows ) {
let selector = row.getElementsByTagName('select')[0],
selectName = selector.name,
col = selector.parentNode;
selector.style.width = '80%';
appendDownSelect( col, selector, selectName );
}
predictBtn.addEventListener('click', loadScryUrl);
}
// append next button
function appendDownSelect( col, selector, name ) {
const downAction = document.createElement('button');
downAction.innerHTML = '↡';
downAction.name = name;
col.appendChild(downAction);
downAction.addEventListener('click', changeSelect.bind( event, selector, name ), false);
}
// select next option, ignoring all disabled options
function changeSelect( selector, name, e ){
const select = selector;
let currentIndex = select.selectedIndex,
collection = [...select.options],
collectionArr = [],
i = 0,
nextItemIndex;
for ( i; i < collection.length; i++ ) {
if ( collection[i].disabled == false ) {
collectionArr.push( collection[i].index );
}
}
let totalItems = parseInt(collectionArr.length) - 1,
lastItem = collectionArr.indexOf(currentIndex) == totalItems;
nextItemIndex = (lastItem) ? 0 : collectionArr.indexOf(currentIndex) + 1;
select.selectedIndex = collectionArr[nextItemIndex];
predict();
}
// append copy button and field to scry load box
function buildCopy() {
scryLoad.style.height = '145px';
copyBtn.classList.add('beigebutton');
copyBtn.classList.add('thingbutton');
copyBtn.style.width = '100%';
copyBtn.innerText = 'Copy Image BBC Code';
scryLoad.appendChild(copyPasta);
scryLoad.appendChild(copyBtn);
copyBtn.addEventListener('click', copyToClipboard);
}
function loadScryUrl() {
copyPasta.value = 'loading...';
setTimeout(function(){
copyPasta.value = '[img]' + getDragonImage() + '[/img]';
}, 500);
}
function getDragonImage() {
const dImg = document.getElementById('dragon-image'),
dImgSrc = dImg.getElementsByTagName('img')[0].src;
return dImgSrc;
}
// load settings
function loadSettings() {
const settingsContainer = document.createElement('div');
let autoCopy = createCheck('autoCopy', 'Automatically Copy');
let growUp = createCheck('growUp', 'Adult On Load');
container.appendChild(settingsContainer);
settingsContainer.style.position = 'absolute';
settingsContainer.style.top = '0';
settingsContainer.appendChild(autoCopy);
settingsContainer.appendChild(growUp);
}
function copyToClipboard() {
copyPasta.select();
copyPasta.setSelectionRange(0, 99999);
navigator.clipboard.writeText(copyPasta.value);
document.execCommand('copy');
appendMessage();
}
// post Message
function appendMessage() {
const message = document.createElement('div');
message.innerHTML = 'copied to clipboard';
scryLoad.appendChild(message);
message.style.font = 'italic normal 9px Times, serif';
message.style.margin = '3px 0';
setTimeout( function() {
scryLoad.removeChild(message);
}, 1000 );
}
// checkbox el
function createCheck( name, label ) {
let itemContainer = document.createElement('div'),
checkboxInput = document.createElement('input'),
checkboxLabel = document.createElement('label'),
getSavedVal = localStorage.getItem( name );
checkboxInput.type = 'checkbox';
checkboxInput.id = name;
checkboxInput.name = name;
checkboxLabel.innerText = label;
checkboxLabel.setAttribute('for', name);
itemContainer.appendChild(checkboxInput);
itemContainer.appendChild(checkboxLabel);
itemContainer.style.display = 'flex';
itemContainer.style.alignItems = 'center';
itemContainer.style.font = 'normal 10px/15px arial, sans-serif';
checkboxLabel.style.paddingLeft = '.5rem';
checkboxInput.checked = (getSavedVal == 'true' ) ? true : false;
checkboxInput.addEventListener('click', updateSetting);
applySetting( name, getSavedVal );
return itemContainer;
}
// apply setting logic if true
function applySetting( name, returnVal ) {
if ( name == 'growUp' && returnVal == 'true' ) {
let ageVal = document.getElementsByName('age')[0];
if ( ageVal.value == 0 ) {
changeSelect(ageVal);
}
}
if ( name == 'autoCopy' && returnVal == 'true' ) {
setTimeout(function(){
copyToClipboard();
}, 1000);
}
}
function updateSetting( e ) {
localStorage.setItem( e.target.name, e.target.checked );
}
// trigger predict
function predict() {
predictBtn.click();
}
})();