Skip to content

DevJSRuntime

Bashar Astifan edited this page Apr 12, 2025 · 41 revisions

๐Ÿ“œ ImMobile JSRuntime

The JSRuntime in ImMobile enables powerful scripting using QuickJS, giving developers access to internal APIs through a global object called imm. It allows you to create extensions, automation scripts, and interactive tools with familiar JavaScript syntax.


๐Ÿ”น Getting Started

To use JSRuntime:

  • Your script should access ImMobile's APIs via the global imm object.
  • You can use functions like setTimeout, sleepAsync, and many others.

Example:

// Delay execution by 2.5 seconds
imm.setTimeout(startTest, 2500);

// Async sleep for 1 second
await imm.sleepAsync(1000);

Note: The imm object in ImMobile contains os and std in QuickJS-NG documentation.

By default ImMobile JSRuntime will load this part before your script

import * as std from "std";
import * as os from "os";
imm.std = std;
imm.os = os;

imm.setTimeout = function(fun, delay) {
    return imm.os.setTimeout(fun, delay);
}

imm.clearTimeout = function(id) {
    return imm.os.clearTimeout(id);
}

imm.sleepAsync = async function(ms) {
    return await imm.os.sleepAsync(ms);
}

you don't have to do this it happens normally,

as you can see I shorten the way for timeout and sleep like imm.sleepAsync instead of imm.os.sleepAsync


QuickJS-NG

Using imm.os or imm.std you can access to the functions that integrated with QuickJS-NG runtime

Reset Runtime

It's very recommeneded to reset the runtime for your script before start using this code

imm.resetRuntime();

Check this file for more examples

Exceptions

ImMobile Runtime may not always throw exceptions for the script, so you mostly may not see error in the console

to catch the exact issue always ensure to use try/catch for the part you need to track

var handle = null;
try {
  handle = imm.os.open(filename, imm.os.O_RDONLY);
} catch (e) {
  console.error(e);
}

๐Ÿงฉ Constant Substitutions

JSRuntime supports special string constants that are auto-replaced with paths to relevant folders:

Constant Description
$exts Extensions folder
$runs Runtime scripts folder
$updates Updates folder
$startup Startup scripts folder
$cfgs Configuration folder
$fonts Fonts folder
$temp Temporary files directory
$local Local app data folder
$music Music folder (ImMobile scoped)
$pics Pictures folder (ImMobile scoped)
$vids Videos folder (ImMobile scoped)
$data Root working data folder
$backups Backups folder
$downloads Downloads folder
$textures Cached textures
$tmpfiles Temporary files

Script-Specific Constants

Constant Description
$jsID Unique ID for the script
$jsIndex Index of the script in execution stack
$jsTime Timestamp of when the script started

๐Ÿ“Ÿ Console Output

You can use console.log(...) or similar functions, and output will appear in the script-specific console in ImMobile.

note that constants my not appear in the real full value in console but that's fine I forgot to replace the value for output print.


๐Ÿ“˜ API Reference (imm object)

Below is a categorized list of available API functions accessible through imm.


๐Ÿ”” Notifications & Dialogs

  • imm.toast(title, message)
    Display a system notification.

  • imm.notify(message, type)
    Show an ImMobile in-app notification.
    type: "info", "success", "warn", "error"

  • imm.noticeDialog(title, text, onClose)
    Show an info dialog.

  • imm.confirmDialog(title, message, callback)
    Confirmation dialog with callback returning true/false.

  • imm.inputDialog(title, text, onClose)
    Prompt input dialog.


๐ŸŒ Network & GitHub Tools

  • imm.openURI(url)
    Open a URL in the default browser.

  • imm.quickDownload(url, dest)
    Download a file quickly to dest.

    dest expected as file name/sub path not a full path, file will be at downloads folder by default

  • imm.getOnlineFileName(url, defaultName)
    Get filename from a remote resource, it may return the string before '?' if fail, ensure to use your own way to cover fail case.

  • imm.getResponse(url)
    Fetch a remote response as string, return the response or undefined if fail.

  • imm.downloadResponse(url, dest)
    Download response to a file.

    dest expected as file name/sub path not a full path, file will be at downloads folder by default

  • imm.gitHubContent(user, repo, sub, callback)
    Load GitHub content (optionally from a subdirectory).

  • imm.gitHubLatestRelease(user, repo, callback)
    Get info about the latest release.

  • imm.showGitHubReadMeUI(url, token)
    Show UI to browse a GitHub README.

  • imm.showGitHubReleasesUI(url, token)
    UI for browsing and downloading GitHub releases.

  • imm.showGitHubFilesUI(url, token)
    UI for browsing and downloading repository content.

  • imm.isOnline()
    Returns true or false based on network availability.


๐Ÿ“ File System

Note that you can check QuickJS-NG Docs for more functions

  • imm.createFolder(parent, name)
    Create a folder under a given path, return the full path for folder or undefined if fail.

  • imm.filePutContents(path, content)
    Write text to a file.

  • imm.fileGetContents(path)
    Read the content of a file, return file content or undefined if fail.

  • imm.openFile(path)
    Open file with the default OS app.

  • imm.openFolder(path)
    Reveal folder in OS file explorer.

  • imm.unZip(file, dest, callback)
    Unzip archive with callback.

  • imm.zipFolder(folder, dest, callback)
    Zip folder with callback.


๐Ÿ“ฆ Package Management

  • imm.installPackage(path)
    Install .appx, .msix, or similar package.

  • imm.registerPackage(path)
    Register a package via its manifest.

  • imm.removePackage(id)
    Remove installed package by ID or name.


๐Ÿงฉ Extensions

  • imm.loadExten(path, window, confirm)
    Load an ImMobile extension.

  • imm.unloadExten(path)
    Unload an ImMobile extension.

  • imm.extenToggleGUI(path)
    Toggle extensionโ€™s UI visibility.

  • imm.isExtenLoaded(path)
    Check if an extension is currently loaded.


๐Ÿ“‚ Pickers

  • imm.chooseFile(callback)
    File picker; returns selected path.

  • imm.saveFile(callback)
    Save-as file dialog.

  • imm.chooseFolder(callback)
    Folder picker; returns selected path.


๐Ÿ“‹ Clipboard & UI

  • imm.setClipboard(text)
    Set system clipboard text.

  • imm.getClipboard()
    Get current clipboard text.

  • imm.showKeyboard(state)
    Show or hide on-screen keyboard.

  • imm.changeBackground(path)
    Change ImMobile background.

    I forgot to apply constants replacement on path at this function so ensure to use proper full path


๐Ÿ”น Path Issues

Some functions like change background has no constants replacement,

as solution for path constants you my do it like:

var downloadsFolder = "$downloads";
var bingWallpapers = "BingsWallpapers";

// Create if not exists
var folderPath = imm.createFolder(downloadsFolder, bingWallpapers);

// Now you have full resolved path: folderPath 

same goes for path parts, most functions do replace / by \ for windows

but some like background change don't, so ensure to use \\ by default to avoid issues


๐Ÿ“š See Also


Clone this wiki locally