if (window.location.host=="banglejs.com") { document.getElementById("apploaderlinks").innerHTML = 'This is the official Bangle.js App Loader - you can also try the Development Version for the most recent apps.'; } else if (window.location.host=="espruino.github.io") { document.title += " [Development]"; document.getElementById("apploaderlinks").innerHTML = 'This is the development Bangle.js App Loader - you can also try the Official Version for stable apps.'; } else if (window.location.hostname==='localhost') { document.title += " [Local]"; Const.APPS_JSON_FILE = "apps.local.json"; document.getElementById("apploaderlinks").innerHTML = 'This is your local Bangle.js App Loader - you can try the Official Version here.'; } else { document.title += " [Unofficial]"; document.getElementById("apploaderlinks").innerHTML = 'This is not the official Bangle.js App Loader - you can try the Official Version here.'; } var RECOMMENDED_VERSION = "2v18"; // could check http://www.espruino.com/json/BANGLEJS.json for this // We're only interested in Bangles DEVICEINFO = DEVICEINFO.filter(x=>x.id.startsWith("BANGLEJS")); // Where we get our usage data from Const.APP_USAGE_JSON = "https://banglejs.com/apps/appusage.json"; Const.APP_DATES_CSV = "appdates.csv"; // Set up source code URL (function() { let username = "espruino"; let githubMatch = window.location.href.match(/\/([\w-]+)\.github\.io/); if (githubMatch) username = githubMatch[1]; Const.APP_SOURCECODE_URL = `https://github.com/${username}/BangleApps/tree/master/apps`; })(); // When a device is found, filter the apps accordingly function onFoundDeviceInfo(deviceId, deviceVersion) { var fwURL = "#", fwExtraText = ""; if (deviceId == "BANGLEJS") { fwURL = "https://www.espruino.com/Bangle.js#firmware-updates"; Const.MESSAGE_RELOAD = 'Hold BTN3\nto reload'; } if (deviceId == "BANGLEJS2") { fwExtraText = "with the Firmware Update app in this App Loader, or " fwURL = "https://www.espruino.com/Bangle.js2#firmware-updates"; Const.MESSAGE_RELOAD = 'Hold button\nto reload'; } if (deviceId != "BANGLEJS" && deviceId != "BANGLEJS2") { showToast(`You're using ${deviceId}, not a Bangle.js. Did you want espruino.com/apps instead?` ,"warning", 20000); } else if (versionLess(deviceVersion, RECOMMENDED_VERSION)) { showToast(`You're using an old Bangle.js firmware (${deviceVersion}) and ${RECOMMENDED_VERSION} is available (see changes). You can update ${fwExtraText}with the instructions here` ,"warning", 20000); } // check against features shown? filterAppsForDevice(deviceId); /* if we'd saved a device ID but this device is different, ensure we ask again next time */ var savedDeviceId = getSavedDeviceId(); if (savedDeviceId!==undefined && savedDeviceId!=deviceId) setSavedDeviceId(undefined); } // Called when we refresh the list of installed apps function onRefreshMyApps() { /* if we're allowed to, send usage stats. We'll only actually send if the data has changed */ sendUsageStats(); } var submittedUsageInfo = ""; /* Send usage stats to servers if it has changed */ function sendUsageStats() { if (!SETTINGS.sendUsageStats) return; // not allowed! if (device.uid === undefined) return; // no data yet! if (!device.appsInstalled.length) return; // no installed apps or disconnected /* Work out what we'll send: * A suitably garbled UID so we can avoid too many duplicates * firmware version * apps installed * apps favourited */ var usageInfo = `uid=${encodeURIComponent(device.uid)}&fw=${encodeURIComponent(device.version)}&apps=${encodeURIComponent(device.appsInstalled.map(a=>a.id).join(","))}&favs=${encodeURIComponent(SETTINGS.favourites.join(","))}`; // Do a quick check for unchanged data to reduce server load if (usageInfo != submittedUsageInfo) { console.log("sendUsageStats: Submitting usage stats..."); var xmlhttp = new XMLHttpRequest(); // new HttpRequest instance xmlhttp.open("POST", "https://banglejs.com/submit_app_stats.php", true /*async*/); xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xmlhttp.onload = (e) => { if (xmlhttp.readyState === 4) console.log(`sendUsageStats (${xmlhttp.status}): ${xmlhttp.responseText}`); }; xmlhttp.onerror = (e) => { console.error("sendUsageStats ERROR: "+xmlhttp.statusText); }; xmlhttp.send(usageInfo); submittedUsageInfo = usageInfo; } } var originalAppJSON = undefined; function filterAppsForDevice(deviceId) { if (originalAppJSON===undefined && appJSON.length) originalAppJSON = appJSON; var device = DEVICEINFO.find(d=>d.id==deviceId); // set the device dropdown document.querySelector(".devicetype-nav span").innerText = device ? device.name : "All apps"; if (originalAppJSON) { // JSON might not have loaded yet if (!device) { if (deviceId!==undefined) showToast(`Device ID ${deviceId} not recognised. Some apps may not work`, "warning"); appJSON = originalAppJSON; } else { // Now filter apps appJSON = originalAppJSON.filter(app => { var supported = ["BANGLEJS"]; if (!app.supports) { console.log(`App ${app.id} doesn't include a 'supports' field - ignoring`); return false; } if (app.supports.includes(deviceId)) return true; //console.log(`Dropping ${app.id} because ${deviceId} is not in supported list ${app.supports.join(",")}`); return false; }); } } refreshLibrary(); } // If 'remember' was checked in the window below, this is the device function getSavedDeviceId() { let deviceId = localStorage.getItem("deviceId"); if (("string"==typeof deviceId) && DEVICEINFO.find(d=>d.id == deviceId)) return deviceId; return undefined; } function setSavedDeviceId(deviceId) { localStorage.setItem("deviceId", deviceId); } // At boot, show a window to choose which type of device you have... window.addEventListener('load', (event) => { let deviceId = getSavedDeviceId() if (deviceId !== undefined) return; // already chosen var html = `