function resizeIframe(obj){ obj.style.height = 0; obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px'; obj.style.width = 100 +'%'; } function checkAmazonAd(adFrame) { var height = parseInt(adFrame.style.height); if (height < 20 && adFrame.src != '/amazon_generic_iframe.html') { // Ad didn't load adFrame.style.height = 200; document.getElementById('amazon_adFrame').src = '/amazon_generic_iframe.html'; } } 0 How to Set Up Fresh Bluetooth var iE=0; if (navigator.userAgent.search(/MSIE/) > -1)iE=1; var currentBarcode; var strInput=''; var srcBarcode; var countChar; document.getElementById('barcodeImg').style.display= 'none'; document.getElementById('onlyDigit').style.display= 'none'; document.getElementById('lengthMore').style.display= 'none'; document.getElementById('itemInfo').innerHTML=''; var parseQueryString = function() { var str = window.location.search; var objURL = {}; str.replace( new RegExp( "([^?=&]+)(=([^&]*))?", "g" ), function( $0, $1, $2, $3 ){ objURL[ $1 ] = $3; } ); return objURL; }; var params = parseQueryString(); var barcode_characteristic; var barcodeArg = params["barcode"]; if (barcodeArg) { document.getElementById('inputBarcode').value = barcodeArg; keyUp('0'); handleInput(); } checkUrlForBarcode(); function checkUrlForBarcode() { var url = document.location.href; //alert(url); var urlPartsArray = url.split("/"); for (var i = 0; i 0 ) { //alert('number = ' + urlPartsArray[i]); document.getElementById('inputBarcode').value = urlPartsArray[i]; keyUp('0'); handleInput(); return; } } } async function retrieveWebBluetoothBarcode() { try { console.log('Opening Device Picker..'); navigator.bluetooth.requestDevice({ filters: [{services: ['0ed6f857-53d3-43a5-a4d5-210a3301d76f']}]}) .then(device => { window.device = device; device.addEventListener('gattserverdisconnected', onDisconnected); console.log('Connecting to ' + device.name); document.getElementById('button-bluetooth').src = '/images/bluetooth_disabled.svg'; document.getElementById('button-bluetooth').title = 'Connecting...'; return device.gatt.connect(); }) .then(server => { console.log('Getting barcode service'); return server.getPrimaryService('0ed6f857-53d3-43a5-a4d5-210a3301d76f'); }) .then(service => { console.log('Getting barcode characteristic'); return service.getCharacteristic('8fa80ebb-c053-4e38-8b7b-48b173680404'); }) .then(characteristic => { barcode_characteristic = characteristic; return barcode_characteristic.startNotifications().then(_ => { console.log(' > Notifications Started > '); var bluetooth_button = document.getElementById('button-bluetooth'); bluetooth_button.src = '/images/bluetooth_active.svg'; bluetooth_button.title = 'Connected to ' + device.name; bluetooth_button.onclick = function() {disconnectDevice();}; bluetooth_button.onmouseover = function(){document.getElementById('button-bluetooth').src="/images/bluetooth_active_hover.svg";}; bluetooth_button.onmouseout = function(){document.getElementById('button-bluetooth').src="/images/bluetooth_active.svg";}; barcode_characteristic.addEventListener('characteristicvaluechanged',handleBluetoothScanNotification); }); }) .catch(error => { console.log(error + ' \n Stack: ' + error.stack); alert('Your Browser is not setup for Fresh Bluetooth. Instructions to setup Fresh Bluetooth are available at the bottom of the page.'); }); function disconnectDevice() { var bluetooth_button = document.getElementById('button-bluetooth'); bluetooth_button.src = '/images/bluetooth_disabled.svg'; window.device.gatt.disconnect(); } function onDisconnected(event) { var bluetooth_button = document.getElementById('button-bluetooth'); bluetooth_button.src = '/images/bluetooth_unselected.svg'; bluetooth_button.onclick= function() {retrieveWebBluetoothBarcode();}; bluetooth_button.onmouseover = function(){document.getElementById('button-bluetooth').src="/images/bluetooth_hover.svg";}; bluetooth_button.onmouseout=function(){document.getElementById('button-bluetooth').src="/images/bluetooth_unselected.svg";}; bluetooth_button.title='Activate Bluetooth'; } } catch (error) { console.log('Outer error!' + error + "\n" + error.stack); alert('Your Browser is not setup for Fresh Bluetooth. Instructions to setup Fresh Bluetooth are available at the bottom of the page.'); } } function handleBluetoothScanNotification(event) { console.log('Recieved Bluetooth Scan Notification, decoding...'); const decoder = new TextDecoder('utf-8'); const strVal = decoder.decode(event.target.value); console.log('Detected new barcode is ' + strVal); document.getElementById('inputBarcode').value = strVal; keyUp('0'); handleInput(); } function keyUp(ev) { document.getElementById('barcodeImg').style.display= 'none'; document.getElementById('itemInfo').style.display= 'none'; strInput=document.getElementById('inputBarcode').value; if (strInput.search(/[^\d,]/g)> -1) {document.getElementById('onlyDigit').style.display= 'block';} else {document.getElementById('onlyDigit').style.display= 'none';}; strInput=strInput.replace(/[^\d,]/g, ''); document.getElementById('inputBarcode').value=strInput; countChar = document.getElementById('inputBarcode').value.length; if ( countChar>13) {document.getElementById('barcodeImg').style.display= 'none'; document.getElementById('lengthMore').style.display= 'block';} else {document.getElementById('lengthMore').style.display= 'none';}; if ( countChar==8 || countChar==12 || countChar==13) {document.getElementById('charcount').style.color='green';} else document.getElementById('charcount').style.color='red'; document.getElementById('charcount').innerHTML=countChar; if(ev.keyCode == 13){ clickBut(); }; }; function clickBut() { document.getElementById('onlyDigit').style.display= 'none'; document.getElementById('lengthMore').style.display= 'none'; document.getElementById('itemInfo').innerHTML=''; countChar = document.getElementById('inputBarcode').value.length; if (history.pushState) { window.history.pushState(null,null,'/' + strInput); } else { // Browser does not support updating the page title dynamically // Not alot we can do in this situation } handleInput(); } function handleInput() { // Parse the text input, check the text is a barcode w/ valid length. countChar = document.getElementById('inputBarcode').value.length; if ( countChar!=8 && countChar!=12 && countChar!=13) { var inner= 'Barcode length invalid ('+countChar+')'; inner+=" That's not a barcode! Do a "; inner+=""; inner+="Product Search instead "; document.getElementById('itemInfo').innerHTML=inner; document.getElementById('itemInfo').style.display= 'block'; if (history.pushState && strInput.length > 0) { window.history.pushState(null,'Invalid Barcode Length','/' + strInput ); document.title = 'Invalid Barcode Length'; } } else if (strInput.search(/[^\d,]/g)> -1) { // alert('That\'s not a barcode!'); if (history.pushState) { window.history.pushState(null,'Invalid Barcode','/' + strInput ); document.title = 'Invalid Barcode'; } var inner= "That's not a barcode! Do a "; inner+=""; inner+="Product Search instead "; document.getElementById('itemInfo').innerHTML=inner; document.getElementById('itemInfo').style.display= 'block'; } else processingInput(strInput); }; function loadDeferredImages() { var deferredImages = document.getElementsByClassName("defferedimgDiv"); for (var i = 0; i "; } } function processingInput(strInp) { var barcodeHtml = "
"; document.getElementById('barcodeImg').innerHTML= barcodeHtml; document.getElementById('barcodeImg').style.display = 'block'; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { jsonResponse = JSON.parse(xhttp.responseText); var htmlToInsert= "

"; if (jsonResponse.haveResult==1) { window.query = jsonResponse.productName; document.getElementById('amazon_adFrame').src = '/amazon_search_iframe.html'; // With Links color:#529136;text-decoration: underline\' // Without Links color:#717376; text-decoration:none\' if ('extra-links' in jsonResponse) { var extralinks = jsonResponse["extra-links"]; var sources = jsonResponse["sources"]; for (var i = 0 ; i (" + sources[i] + ") " + jsonResponse.productName + " "; var link = document.createElement("a"); link.id = "productName" + i; link.href = extralinks[i]; link.style = "color:#529136;text-decoration: underline;"; link.innerHTML = "(" + sources[i] + ") " + jsonResponse.productName; link.title = jsonResponse.productName; htmlToInsert += link.outerHTML + " "; } } else { var link = document.createElement("a"); link.id = "productName"; link.style = "color:#717376; text-decoration:none;"; link.innerHTML = jsonResponse.productName; link.title = jsonResponse.productName; htmlToInsert += link.outerHTML; } var prodImgHtml = ""; if ('thumbnailURL' in jsonResponse) { prodImgHtml = "
"; } htmlToInsert+="" + prodImgHtml ; if ('productDetails' in jsonResponse) { // Product Details is set in JSON Response // Next, Check if a generic Description is available if ('description' in jsonResponse.productDetails) { // Description available in product Details htmlToInsert+=" " + jsonResponse.productDetails.description.replace(/(?:\r\n|\r|\n)/g, ' ') + " "; } else { // Description not available in product details htmlToInsert+=" No Description available for this product. "; } // Next, Check if nutritional information is available if ('nutrition' in jsonResponse.productDetails) { htmlToInsert += "
"; var tableColor = '#49913D'; var tableBarrier = ""; // Nutritional information available htmlToInsert+= " "; htmlToInsert+= "
Nutritional Information "; htmlToInsert+= tableBarrier; if ('number_of_servings' in jsonResponse.productDetails.nutrition) { htmlToInsert+="Servings" + jsonResponse.productDetails.nutrition.number_of_servings + ""; } if ('serving_size' in jsonResponse.productDetails.nutrition) { var uom = ""; if ('serving_size_uom' in jsonResponse.productDetails.nutrition) { uom = jsonResponse.productDetails.nutrition.serving_size_uom; } htmlToInsert+="Serving Size"+jsonResponse.productDetails.nutrition.serving_size + ' ' +uom+""; } htmlToInsert+=tableBarrier; htmlToInsert+="Amount Per Serving"; if ('energy' in jsonResponse.productDetails.nutrition) { // Energy is set, how we display this depends on whether energy uom is set // so we check that too. if ('energy_uom' in jsonResponse.productDetails.nutrition) { // Use European 'Energy' instead of American 'Calories' htmlToInsert += "Energy" + jsonResponse.productDetails.nutrition.energy + ' ' + jsonResponse.productDetails.nutrition.energy_uom + ""; if ('calories_from_fat' in jsonResponse.productDetails.nutrition) { htmlToInsert += " Calories From Fat" + jsonResponse.productDetails.nutrition.calories_from_fat + ""; } } else { // Use American 'Calories' instead of European 'Energy' // Also add Calories from fat if available htmlToInsert += "Calories" + jsonResponse.productDetails.nutrition.energy + ""; if ('calories_from_fat' in jsonResponse.productDetails.nutrition) { htmlToInsert += " Calories From Fat" + jsonResponse.productDetails.nutrition.calories_from_fat + ""; } } } htmlToInsert += tableBarrier; if ('fat' in jsonResponse.productDetails.nutrition) { var uom = ""; if ('fat_uom' in jsonResponse.productDetails.nutrition) { uom = jsonResponse.productDetails.nutrition.fat_uom; } htmlToInsert += "Total Fat" + jsonResponse.productDetails.nutrition.fat + ' '+ uom + ""; } if ('saturated_fat' in jsonResponse.productDetails.nutrition) { var uom = ""; if ('saturated_fat_uom' in jsonResponse.productDetails.nutrition) { uom = jsonResponse.productDetails.nutrition.saturated_fat_uom; } htmlToInsert += "Saturated Fat" + jsonResponse.productDetails.nutrition.saturated_fat + ' '+ uom + ""; } if ('trans_fat' in jsonResponse.productDetails.nutrition) { var uom = ""; if ('trans_fat_uom' in jsonResponse.productDetails.nutrition) { uom = jsonResponse.productDetails.nutrition.trans_fat_uom; } htmlToInsert+="Trans Fat" + jsonResponse.productDetails.nutrition.trans_fat + ' ' + uom + ""; } if ('cholestrol' in jsonResponse.productDetails.nutrition) { var uom = ""; if ('cholestrol_uom' in jsonResponse.productDetails.nutrition) { uom = jsonResponse.productDetails.nutrition.cholestrol_uom; } htmlToInsert+="Cholesterol" + jsonResponse.productDetails.nutrition.cholestrol + ' ' + uom + ""; } if ('sodium' in jsonResponse.productDetails.nutrition) { var uom = ""; if ('sodium_uom' in jsonResponse.productDetails.nutrition) { uom = jsonResponse.productDetails.nutrition.sodium_uom; } htmlToInsert += "Sodium" + jsonResponse.productDetails.nutrition.sodium + ' ' + uom + ""; } if ('carbohydrates' in jsonResponse.productDetails.nutrition) { var uom = ""; if ('carbohydrates_uom' in jsonResponse.productDetails.nutrition) { uom = jsonResponse.productDetails.nutrition.carbohydrates_uom; } htmlToInsert += "Total Carbohydrate" + jsonResponse.productDetails.nutrition.carbohydrates + ' ' + uom + ""; } if ('dietary_fiber' in jsonResponse.productDetails.nutrition) { var uom = ""; if ('dietary_fiber_uom' in jsonResponse.productDetails.nutrition) { uom = jsonResponse.productDetails.nutrition.dietary_fiber_uom; } htmlToInsert += "Dietary Fiber" + jsonResponse.productDetails.nutrition.dietary_fiber + ' ' + uom + ""; } if ('sugars' in jsonResponse.productDetails.nutrition) { var uom = ""; if ('sugars_uom' in jsonResponse.productDetails.nutrition) { uom = jsonResponse.productDetails.nutrition.sugars_uom; } htmlToInsert += "Sugars" + jsonResponse.productDetails.nutrition.sugars + ' ' + uom + ""; } if ('protein' in jsonResponse.productDetails.nutrition) { var uom = ""; if ('protein_uom' in jsonResponse.productDetails.nutrition) { uom = jsonResponse.productDetails.nutrition.protein_uom; } htmlToInsert += "Protein" + jsonResponse.productDetails.nutrition.protein + ' ' + uom + ""; } if (('vitamin_a' in jsonResponse.productDetails.nutrition) && ('vitamin_a_uom' in jsonResponse.productDetails.nutrition)) { htmlToInsert += "Vitamin A" + jsonResponse.productDetails.nutrition.vitamin_a + ' ' + jsonResponse.productDetails.nutrition.vitamin_a_uom + ""; } if (('vitamin_c' in jsonResponse.productDetails.nutrition )&& ('vitamin_c_uom' in jsonResponse.productDetails.nutrition)) { htmlToInsert += "Vitamin C"+ jsonResponse.productDetails.nutrition.vitamin_c + ' ' + jsonResponse.productDetails.nutrition.vitamin_c_uom + ""; } if (('calcium' in jsonResponse.productDetails.nutrition )&& ('calcium_uom' in jsonResponse.productDetails.nutrition)) { htmlToInsert += "Calcium"+ jsonResponse.productDetails.nutrition.calcium + ' ' + jsonResponse.productDetails.nutrition.calcium_uom + ""; } if (('iron' in jsonResponse.productDetails.nutrition )&& ('iron_uom' in jsonResponse.productDetails.nutrition)) { htmlToInsert += "Iron"+ jsonResponse.productDetails.nutrition.iron + ' ' + jsonResponse.productDetails.nutrition.iron_uom + ""; } if (('potassium' in jsonResponse.productDetails.nutrition )&& ('potassium_uom' in jsonResponse.productDetails.nutrition)) { htmlToInsert += "Potassium"+ jsonResponse.productDetails.nutrition.potassium + ' ' + jsonResponse.productDetails.nutrition.potassium_uom + ""; } if ('warning' in jsonResponse.productDetails.nutrition) { htmlToInsert += "Warning" + jsonResponse.productDetails.nutrition.warning + "" } if ('daily_percent_vitamin_a' in jsonResponse.productDetails.nutrition || 'daily_percent_vitamin_c' in jsonResponse.productDetails.nutrition || 'daily_percent_iron' in jsonResponse.productDetails.nutrition || 'daily_percent_calcium' in jsonResponse.productDetails.nutrition) { // Some form of daily percent available. htmlToInsert+=tableBarrier; htmlToInsert+="% Daily Value"; } if('daily_percent_vitamin_a' in jsonResponse.productDetails.nutrition) { htmlToInsert += "Vitamin A" + jsonResponse.productDetails.nutrition.daily_percent_vitamin_a + ""; } if('daily_percent_vitamin_c' in jsonResponse.productDetails.nutrition) { htmlToInsert += "Vitamin C" + jsonResponse.productDetails.nutrition.daily_percent_vitamin_c + ""; } if('daily_percent_calcium' in jsonResponse.productDetails.nutrition) { htmlToInsert += "Calcium" + jsonResponse.productDetails.nutrition.daily_percent_calcium + ""; } if ('daily_percent_iron' in jsonResponse.productDetails.nutrition) { htmlToInsert += "Iron" + jsonResponse.productDetails.nutrition.daily_percent_iron + ""; } htmlToInsert += tableBarrier; htmlToInsert += " "; htmlToInsert += ""; htmlToInsert += " Product recipes are often changed, this may affect nutrition and allergen information, therefore you should always check product labels and not rely solely on the information presented here. "; } else { // Nutritional information not available } // We also want to display USDA notice if any of our data came from USDA if ('data_sources' in jsonResponse.productDetails) { for (var i = 0; i Add it to the database. '; if (history.pushState) { window.history.pushState(null,'Not Found','/' + strInput ); } document.title = 'Not Found' document.getElementById('adverRegular').style.display= 'block'; document.getElementById('itemInfo').innerHTML=htmlToInsert; }; document.getElementById('itemInfo').style.display= 'block'; }; }; var reqUrl="/tomc/Servlet2?barcode="; reqUrl=reqUrl+strInp; xhttp.open("GET", reqUrl, true); xhttp.send(); }; function escapeHTML (unsafe_str) { return unsafe_str .replace(/&/g, '&') .replace(//g, '>') .replace(/\"/g, '"') .replace(/\'/g, '''); // ''' is not valid HTML 4 }