Random Scriptable API Widget Script
Random Scriptable API Widget Script
Differentiating between running as a widget and in the app allows the script to tailor its behavior and resource usage. As a widget, it displays on the Home Screen and requires quick execution with minimal interactivity. In the app, it can offer a preview and more interactive elements, such as links and additional styling. This distinction optimizes performance and user interaction based on the context .
In the script, the `LinearGradient` object is used to set a background gradient for the widget. It is configured with a `locations` array specifying the start and end points of the gradient, and `colors` array defining the color transition from a dark grey (#141414) to a deep blue (#13233F).
The script retrieves the Scriptable API documentation through `loadDocs()`, which returns a JSON object of APIs. It then extracts API names into an array and selects one randomly using `Math.round(Math.random() * apiNames.length)`. The chosen API is returned with its name, documentation, and URL, subsequently used to generate the widget content with text elements displaying the API name and description .
The script defines and uses the asynchronous functions `loadDocs` and `loadAppIcon` to load external resources. `loadDocs` fetches and loads JSON data from the Scriptable documentation URL, and `loadAppIcon` retrieves the app icon image from a specified URL using HTTP requests .
To present API information, the script utilizes a `createWidget` function where a `ListWidget` object is configured. It sets a background gradient, includes an app icon and title using stacks, and displays API details with text elements showing the API name and description. Optional footer elements are added outside Siri execution for documentation links, enhancing detail accessibility and visual design .
The script maintains consistent styling through the use of `Color` and `Font` objects for various text elements, setting properties like `textColor`, `font`, and `textOpacity`. Gradients are managed by a `LinearGradient` object for the background. These ensure uniform visual themes across the widget components .
The presence or absence of interactive elements, such as the footer with links, governs UI interactivity. The script checks `config.runsWithSiri`; if true, it assumes a static UI and does not add interactive components like the footer link. This condition reflects the limitations of non-interactive presentations in Siri .
When the script does not run with Siri, it adds interactive elements such as a footer with a link to the API documentation. This includes a 'Read more' text linked to the API URL, a linked symbol for additional information, and an image linking to the Scriptable documentation. These enhance user interactivity by providing direct access to resources .
The script checks the condition `if (config.runsInWidget)` to decide. If `runsInWidget` is true, the script sets the widget to be shown on the Home Screen using `Script.setWidget(widget)`. Otherwise, it previews the widget within the app using `widget.presentMedium()` .
`Script.complete()` signals that the script's execution is finished, particularly optimizing performance when the script is executed from Shortcuts or Siri. This method can speed up execution by immediately freeing up system resources upon completion rather than waiting for the runtime environment to automatically conclude the script execution .