diff --git a/applications/system/js_app/examples/apps/Scripts/js_examples/badusb_demo.js b/applications/system/js_app/examples/apps/Scripts/js_examples/badusb_demo.js index 971c16c9e..758cffd13 100644 --- a/applications/system/js_app/examples/apps/Scripts/js_examples/badusb_demo.js +++ b/applications/system/js_app/examples/apps/Scripts/js_examples/badusb_demo.js @@ -59,7 +59,7 @@ eventLoop.subscribe(views.dialog.input, function (_sub, button, eventLoop, gui) notify.error(); } - // Optional, but allows to interchange with usbdisk + // Optional, but allows to unlock usb interface to switch profile badusb.quit(); eventLoop.stop(); diff --git a/applications/system/js_app/examples/apps/Scripts/js_examples/gui.js b/applications/system/js_app/examples/apps/Scripts/js_examples/gui.js index 7e61e04ed..10e88b649 100644 --- a/applications/system/js_app/examples/apps/Scripts/js_examples/gui.js +++ b/applications/system/js_app/examples/apps/Scripts/js_examples/gui.js @@ -8,6 +8,7 @@ let textInputView = require("gui/text_input"); let byteInputView = require("gui/byte_input"); let textBoxView = require("gui/text_box"); let dialogView = require("gui/dialog"); +let filePicker = require("gui/file_picker"); let flipper = require("flipper"); // declare view instances @@ -40,6 +41,7 @@ let views = { "Text input & Dialog", "Byte input", "Text box", + "File picker", "Exit app", ], }), @@ -63,12 +65,22 @@ eventLoop.subscribe(views.demos.chosen, function (_sub, index, gui, eventLoop, v } else if (index === 4) { gui.viewDispatcher.switchTo(views.longText); } else if (index === 5) { + let path = filePicker.pickFile("/ext", "*"); + if (path) { + views.helloDialog.set("text", "You selected:\n" + path); + } else { + views.helloDialog.set("text", "You didn't select a file"); + } + views.helloDialog.set("center", "Nice!"); + gui.viewDispatcher.switchTo(views.helloDialog); + } else if (index === 6) { eventLoop.stop(); } }, gui, eventLoop, views); // say hi after keyboard input eventLoop.subscribe(views.keyboard.input, function (_sub, name, gui, views) { + views.keyboard.set("defaultText", name); // Remember for next usage views.helloDialog.set("text", "Hi " + name + "! :)"); views.helloDialog.set("center", "Hi Flipper! :)"); gui.viewDispatcher.switchTo(views.helloDialog); diff --git a/applications/system/js_app/examples/apps/Scripts/js_examples/interactive.js b/applications/system/js_app/examples/apps/Scripts/js_examples/interactive.js index 63db6c5e0..29f3bb253 100644 --- a/applications/system/js_app/examples/apps/Scripts/js_examples/interactive.js +++ b/applications/system/js_app/examples/apps/Scripts/js_examples/interactive.js @@ -11,8 +11,9 @@ storage.makeDirectory("/ext/.tmp/js"); storage.rmrf("/ext/.tmp/js/repl") storage.makeDirectory("/ext/.tmp/js/repl") let ctx = { - tmp_template: "/ext/.tmp/js/repl/", - tmp_number: 0, + tmpTemplate: "/ext/.tmp/js/repl/", + tmpNumber: 0, + persistentScope: {}, }; let views = { @@ -41,28 +42,40 @@ eventLoop.subscribe(views.dialog.input, function (_sub, button, gui, views) { eventLoop.subscribe(views.textInput.input, function (_sub, text, gui, views, ctx) { gui.viewDispatcher.switchTo(views.loading); - let path = ctx.tmp_template + toString(ctx.tmp_number++); + let path = ctx.tmpTemplate + toString(ctx.tmpNumber++); let file = storage.openFile(path, "w", "create_always"); - file.write("({run:function(){return " + text + ";},})"); + file.write(text); file.close(); // Hide GUI before running, we want to see console and avoid deadlock if code fails gui.viewDispatcher.sendTo("back"); - let result = load(path).run(); + let result = load(path, ctx.persistentScope); // Load runs JS and returns last value on stack storage.remove(path); - gui.viewDispatcher.sendTo("front"); // Must convert to string explicitly - if (typeof result === "number") { + if (result === null) { // mJS: typeof null === "null", ECMAScript: typeof null === "object", IDE complains when checking "null" type + result = "null"; + } else if (typeof result === "string") { + result = "'" + result + "'"; + } else if (typeof result === "number") { result = toString(result); - } else if (typeof result === "undefined") { - result = "undefined"; + } else if (typeof result === "bigint") { // mJS doesn't support BigInt() but might aswell check + result = "bigint"; } else if (typeof result === "boolean") { result = result ? "true" : "false"; + } else if (typeof result === "symbol") { // mJS doesn't support Symbol() but might aswell check + result = "symbol"; + } else if (typeof result === "undefined") { + result = "undefined"; } else if (typeof result === "object") { - result = JSON.stringify(result); + result = "object"; // JSON.stringify() is not implemented + } else if (typeof result === "function") { + result = "function"; + } else { + result = "unknown type: " + typeof result; } + gui.viewDispatcher.sendTo("front"); views.dialog.set("header", "JS Returned:"); views.dialog.set("text", result); gui.viewDispatcher.switchTo(views.dialog); @@ -74,4 +87,8 @@ eventLoop.subscribe(gui.viewDispatcher.navigation, function (_sub, _, eventLoop) }, eventLoop); gui.viewDispatcher.switchTo(views.dialog); + +// Message behind GUI if something breaks +print("If you're stuck here, something went wrong, re-run the script") eventLoop.run(); +print("\n\nFinished correctly :)") diff --git a/applications/system/js_app/examples/apps/Scripts/js_examples/load.js b/applications/system/js_app/examples/apps/Scripts/js_examples/load.js index 3b8f2d8fd..82b2d2046 100644 --- a/applications/system/js_app/examples/apps/Scripts/js_examples/load.js +++ b/applications/system/js_app/examples/apps/Scripts/js_examples/load.js @@ -1,3 +1,3 @@ -let math = load(__dirpath + "/load_api.js"); +let math = load(__dirname + "/load_api.js"); let result = math.add(5, 10); print(result); diff --git a/applications/system/js_app/examples/apps/Scripts/js_examples/path.js b/applications/system/js_app/examples/apps/Scripts/js_examples/path.js index 9d3159d9f..0be31b81d 100644 --- a/applications/system/js_app/examples/apps/Scripts/js_examples/path.js +++ b/applications/system/js_app/examples/apps/Scripts/js_examples/path.js @@ -1,9 +1,9 @@ let storage = require("storage"); -print("script has __dirpath of" + __dirpath); -print("script has __filepath of" + __filepath); -if (storage.fileExists(__dirpath + "/math.js")) { +print("script has __dirname of" + __dirname); +print("script has __filename of" + __filename); +if (storage.fileExists(__dirname + "/math.js")) { print("math.js exist here."); } else { print("math.js does not exist here."); -} \ No newline at end of file +} diff --git a/applications/system/js_app/examples/apps/Scripts/js_examples/storage.js b/applications/system/js_app/examples/apps/Scripts/js_examples/storage.js index 5f273c628..c0ec8bfa4 100644 --- a/applications/system/js_app/examples/apps/Scripts/js_examples/storage.js +++ b/applications/system/js_app/examples/apps/Scripts/js_examples/storage.js @@ -25,5 +25,5 @@ storage.remove(path); print("Done") -// You don't need to close the file after each operation, this is just to show some different ways to use -// There's also many more functions and options, check types docs in firmware repo \ No newline at end of file +// You don't need to close the file after each operation, this is just to show some different ways to use the API +// There's also many more functions and options, check type definitions in firmware repo \ No newline at end of file diff --git a/applications/system/js_app/examples/apps/Scripts/js_examples/stringutils.js b/applications/system/js_app/examples/apps/Scripts/js_examples/stringutils.js index c1f4d447a..7c57f9bc5 100644 --- a/applications/system/js_app/examples/apps/Scripts/js_examples/stringutils.js +++ b/applications/system/js_app/examples/apps/Scripts/js_examples/stringutils.js @@ -12,8 +12,8 @@ let searchStr = "World"; let result2 = toString(sampleText.indexOf(searchStr)); print(result2); -let upperCaseText = "Text in upper case: " + toUpperCase(sampleText); +let upperCaseText = "Text in upper case: " + sampleText.toUpperCase(); print(upperCaseText); -let lowerCaseText = "Text in lower case: " + toLowerCase(sampleText); +let lowerCaseText = "Text in lower case: " + sampleText.toLowerCase(); print(lowerCaseText);