-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: functions to call javascript from narrat, better logging (#263)
* feat: function to call javascript from narrat * feat: adding call_js_method and run_js commands * feat: new logging feature, documented js interface functions * chore: update narrat version in deps
- Loading branch information
Showing
22 changed files
with
244 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: JavaScript interface in Narrat | ||
description: Narrat can call JavaScript methods, use JavaScript variables, and run arbitrary JavaScript code for cases where you need to interact with the browser or the page. | ||
--- | ||
|
||
## JS API for Narrat | ||
|
||
There are currently two commands that allow using JavaScript more directly in Narrat: | ||
|
||
- `call_js_method`: Calls a JavaScript method on the page. | ||
- `run_js`: Builds a function from an arbitrary snippet of JavaScript code and runs it, returning the result. | ||
|
||
This is intended to be used in cases where you need to do something narrat scripting can't do, but don't want to bother adding a new command to the language via a [plugin](https://docs.narrat.dev/plugins/plugins.html) | ||
|
||
## `call_js_method` | ||
|
||
Syntax: `call_js_method [target] [method] [...args [optional]]` | ||
|
||
- `target` [string|object]: The target object to call the method on. This can be an object, or a string that will be evaluated to an object. Examples: `$myVariable`, `"document.body"`, `"localStorage"`. Paths are evaluated in the context of the page, looking for the object in `window`. | ||
- `method` [string]: The method to call on the target object. | ||
- `args` [any]: Any number of arguments passed after the method will be passed to the JavaScript function being called too. | ||
|
||
Examples: | ||
|
||
```narrat | ||
test_js: | ||
var stuff (new Object) | ||
set stuff.hello "world" | ||
call_js_method localStorage setItem "test_js" (json_stringify $stuff) | ||
var stuff2 (json_parse (call_js_method localStorage getItem "test_js")) | ||
log $stuff2 | ||
var canvas (call_js_method document createElement "canvas") | ||
call_js_method document.body appendChild $canvas | ||
set canvas.class "test" | ||
call_js_method $canvas requestPointerLock | ||
``` | ||
|
||
## `run_js` | ||
|
||
Syntax: `run_js [code]` | ||
|
||
- `code` [string]: The JavaScript code to run. | ||
|
||
The code will be built using `Function`, in the format `return ${code}`. This will generate a function returning whatever `code` returns. The command then runs this function and returns the result. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"files": [ | ||
"dist/" | ||
], | ||
"type": "module", | ||
"pnpm": { | ||
"patchedDependencies": { | ||
"@types/[email protected]": "patches-@[email protected]" | ||
|
@@ -95,6 +96,7 @@ | |
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^6.0.0", | ||
"eslint-plugin-vue": "^9.15.1", | ||
"execa": "^8.0.1", | ||
"jsdom": "^20.0.0", | ||
"kolorist": "^1.5.1", | ||
"pinia": "^2.1.7", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
dev_test: | ||
run test_js | ||
run test_arrays | ||
run test_regex | ||
run test_text_autoadvance | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/narrat/src/examples/default/scripts/test_js.narrat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
test_js: | ||
var stuff (new Object) | ||
set stuff.hello "world" | ||
call_js_method localStorage setItem "test_js" (json_stringify $stuff) | ||
var stuff2 (json_parse (call_js_method localStorage getItem "test_js")) | ||
log $stuff2 | ||
// run test_js_2 | ||
|
||
test_js_2: | ||
var canvas (call_js_method document createElement "canvas") | ||
call_js_method document.body appendChild $canvas | ||
set canvas.class "test" | ||
call_js_method $canvas requestPointerLock | ||
var test (run_js "1 + 2") | ||
log $test | ||
var hello (run_js "localStorage.getItem('test_js')") | ||
log $hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export enum LogLevel { | ||
ERROR = 3, | ||
WARN = 2, | ||
INFO = 1, | ||
DEBUG = 0, | ||
NONE = -1, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { LogLevel } from '@/types/logging-types'; | ||
|
||
export function generateLoggingFunction( | ||
moduleName: string, | ||
logLevel: LogLevel, | ||
currentLogLevel: LogLevel, | ||
) { | ||
if (currentLogLevel >= logLevel) { | ||
let logKey = 'log'; | ||
switch (logLevel) { | ||
case LogLevel.ERROR: | ||
logKey = 'error'; | ||
break; | ||
case LogLevel.WARN: | ||
logKey = 'warn'; | ||
break; | ||
default: | ||
break; | ||
} | ||
return console.log.bind(console, logKey, `[${moduleName}]`); | ||
} else { | ||
return () => {}; | ||
} | ||
} | ||
export function createLogger(moduleName: string, logLevel: LogLevel) { | ||
return { | ||
error: generateLoggingFunction(moduleName, LogLevel.ERROR, logLevel), | ||
warn: generateLoggingFunction(moduleName, LogLevel.WARN, logLevel), | ||
info: generateLoggingFunction(moduleName, LogLevel.INFO, logLevel), | ||
debug: generateLoggingFunction(moduleName, LogLevel.DEBUG, logLevel), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.