Skip to content

Instantly share code, notes, and snippets.

@djcyphers
Last active March 31, 2016 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djcyphers/a941450036dd148f7c14 to your computer and use it in GitHub Desktop.
Save djcyphers/a941450036dd148f7c14 to your computer and use it in GitHub Desktop.
Tool used for MJ Freeway and the EK-1200i weight system
// ==UserScript==
// @name MJ Freeway: EZ Weight System
// @namespace mjfezb1
// @description Automagically weigh and process orders even faster
// @include about:addons
// @include https://i.gomjfreeway.com/*
// @version v0.9b
// @grant none
// @grant GM_addStyle
// @grant GM_log
/* Scan your product, weigh it, and then use the print() function on your EK-1200i scale.
If you have to weigh exact weights, click the EZWeigh menu button. It changes to red (deactivated) and is persistant.
This way you can keep the auto print and add to order functions. Button state is also persistant.
Needs a better way to detect when to print the label as currently it's set on a timer; lag can affect it.
*/
// ==/UserScript==
// Created by DJ @ djcyphers.com
window.addEventListener('load', Greasemonkey_main, false);
window.addEventListener('load', processData, false);
// Listen for DOMNodeInserted
function Greasemonkey_main() {
window.addEventListener('DOMNodeInserted', findElement, false);
//Get website latency
getLatency();
//Begin Toggle Button
mjfMenu = document.getElementById('nice-menu-0');
bState = sessionStorage.getItem("bState");
if (mjfMenu != '') {
var eNode = document.createElement('li');
if ((bState === "1") || (bState = null) || (bState = '')){
sessionStorage.setItem('bState', 1);
eNode.innerHTML = '<div id="EZWEIGH"><a href="#">EZWeigh</a></div>';
mjfMenu.appendChild(eNode);
document.getElementById('EZWEIGH').addEventListener('click', buttonclickAction, false);
document.getElementById('EZWEIGH').setAttribute("style","background: none;");
optionsMenu();
attachHistory();
}
else {
sessionStorage.setItem('bState', 0);
eNode.innerHTML = '<div id="EZWEIGH"><a href="#">EZWeigh</a></div>';
mjfMenu.appendChild(eNode);
document.getElementById('EZWEIGH').addEventListener('click', buttonclickAction, false);
document.getElementById('EZWEIGH').setAttribute("style","background: #780000;");
optionsMenu();
attachHistory();
}
}
}
// Need to get latency to apply the correct setTimeout for elements to exist
function processData(){
patientList = document.getElementById('block-mj_patient_profile-0');
if (window.location.pathname.split("/")[2] === "patients" && patientList !== 'undefined'){
//wait for patient to appear on list then get their pic
patientList.addEventListener('DOMNodeInserted', processData, false);
}
patientCheck = document.getElementsByClassName('first last')[1].getElementsByTagName('a')[0];
patientLast = document.getElementById('block-mj_patient_profile-0').getElementsByClassName('last')[0].getElementsByTagName('a')[0];
patientHref = document.getElementById('block-mj_patient_profile-0').getElementsByClassName('last')[0].getElementsByTagName('a')[0].href;
patientHid = document.querySelector(".last .wait_threshhold");
splitList = patientHref.split("/");
patientId = splitList[ splitList.length - 2 ];
storeId = window.location.pathname.split("/")[1];
patientNodeUrl = "https://i.gomjfreeway.com/" + storeId + "/node/" + patientId;
dNode = document.createElement('span');
pNode = document.getElementById('patientImage');
verifyUrl = "https://i.gomjfreeway.com/" + storeId + "/patients";
if (!pNode) {
$.get(patientNodeUrl, function(data) {
var imgs = $(data).find('img');
imgs.each(function(i, img) {
if (this.src.indexOf("profile_photo") != -1) {
var imgUrl = this.src;
dNode.innerHTML = '<img src="' + imgUrl + '"></img>';
dNode.id = "patientImage";
dNode.className = 'wait_time_minutes trigger-processed';
patientHid.appendChild(dNode);
return false;
}
});
});
}
else {return;}
}
// Need to get latency to apply the correct setTimeout for elements to exist
function getLatency(){
currentUrl = window.location.href;
$.ajax({
beforeSend: function(){
// right before firing off the request, create a global object that includes the request send time
window.startTime = new Date();
},
// send the request to the root URI of your host
url: currentUrl,
success: function(){
// once the request comes back, record the end time
window.endTime = new Date();
// take the difference, which will yield a number in milliseconds, and print it out
sessionStorage.setItem('latency', (window.endTime - window.startTime));
}
});
}
//Add order history to Patient Data tab for easier access
function attachHistory(){
var patientTab = document.getElementsByClassName('tabs')[0].getElementsByTagName('li')[1];
patientTab.addEventListener('click', getHistory, false);
}
function getHistory(){
var patientURL = document.getElementsByClassName('customer')[0].getElementsByTagName('a')[0].href;
var patientTab = document.getElementById('pdsPurchaseHistory');
var eNode = document.createElement('div');
var oNode = document.getElementById('orderhistory');
eNode.id = 'orderhistory';
if (!oNode) {
eNode.innerHTML = '<a href="' + patientURL + '/order-history">Order History</a>';
patientTab.appendChild(eNode);
}
else {return;}
}
// Find weight form box, select it, and add event listener for weight added
function findElement() {
latency = sessionStorage.getItem("latency");
weight = document.getElementById('edit-nonstandard-weight');
//If elem exists, add listener
if (weight) {
window.setTimeout(boxSelect, latency*0.1);
document.getElementById('edit-nonstandard-weight').addEventListener('change', doTransaction, false);
}
else {return;}
}
// Get price value then wait for value input
function doTransaction() {
latency = sessionStorage.getItem("latency");
weight = document.getElementById('edit-nonstandard-weight');
if (weight) {
//needs updating ie mutation observer, but latency check will work
window.setTimeout(changeValue, latency);
window.setTimeout(forcePrice, latency);
}
else {console.log('Problem on doTransaction! -> ' + weight); return;}
}
//Select the weight form box if it exists, needs updating... maybe for while loop
function boxSelect() {
weight = document.getElementById('edit-nonstandard-weight');
if (weight) {
weight.select();
}
else {return;}
}
//Apply math if elem exists
function changeValue() {
bState = sessionStorage.getItem("bState");
weight = document.getElementById('edit-nonstandard-weight');
// Depends on button state
if ((weight) && (bState === "1")) {
weight.value = roundToHalf(weight.value);
}
else {
return;
}
}
// Add menu options
function optionsMenu(){
var button = document.getElementById('EZWEIGH');
var eNode = document.createElement('ul');
var bState = sessionStorage.getItem("bState");
var tNode;
if ((bState === "0") || (bState = null) || (bState = '')) {
tNode = "Exact Weight";
eNode.innerHTML = '<li><a id="ezstatus" href="#">' + tNode + '</a></li>';
button.appendChild(eNode);
}
else if (bState = 1) {
tNode = "EZ Weight";
eNode.innerHTML = '<li><a id="ezstatus" href="#">' + tNode + '</a></li>';
button.appendChild(eNode);
}
}
// Button function toggles on/off mode
function buttonclickAction() {
bState = sessionStorage.getItem("bState");
if ((bState === "0") || (bState = null) || (bState = '')) {
sessionStorage.setItem("bState", 1);
document.getElementById('EZWEIGH').setAttribute("style","background: none;");
document.getElementById('ezstatus').innerHTML = "EZ Weight";
}
else if (bState = 1) {
sessionStorage.setItem("bState", 0);
document.getElementById('EZWEIGH').setAttribute("style","background: #780000;");
document.getElementById('ezstatus').innerHTML = "Exact Weight";
}
}
// Find form and submit
function submitOrder() {
totalPrice = document.getElementById('edit-item-price');
submit = document.forms[3].elements['edit-submit-1'];
weight = document.getElementById('edit-nonstandard-weight');
if (totalPrice.textLength != 0) {
submit.click();
}
else if (!weight) {
weight.select();
weight.value = '';
console.log('Problem on submitOrder! -> ' + weight + ' Total -> ' + totalPrice);
}
else {
weight.select();
weight.value = '';
console.log('Problem on submitOrder! -> ' + weight + ' Total -> ' + totalPrice);
return;
}
}
// Force MJF price calculation and submit order if elem exists
function forcePrice() {
latency = sessionStorage.getItem("latency");
weight = document.getElementById('edit-nonstandard-weight');
if (weight.textLength != 0) {
weight.onchange();
window.setTimeout(submitOrder, latency);
// Needs improvement - printlabel should fire when the element exists ie mutation observer
window.setTimeout(printLabel, latency*2.5);
}
else {console.log('Problem on forcePrice! -> ' + weight); return;}
}
// Print label function
function printLabel() {
printButton = document.getElementsByClassName('uberpos-button-label').item(0);
if (printButton != '') {
printButton.click();
}
else {return;}
}
//Credits breakingpar.com
function roundToHalf(value) {
var converted = parseFloat(value);
// Make sure we have a number
var decimal = (converted - parseInt(converted, 10));
decimal = Math.round(decimal * 10);
// Depending on weight value we return the adjustment or just the value
if (decimal == 3) {
return value;
}
if (decimal == 4) {
return value;
}
if (decimal == 5) {
return (parseInt(converted, 10) + 0.5);
}
if (decimal == 8) {
return value;
}
if (decimal == 9) {
return value;
}
if ((decimal < 3) || (decimal > 7)) {
return Math.round(converted);
} else {
//Credits StackOverFlow's "Dave"
return (Math.round(converted * 2) / 2).toFixed(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment