{"id":27,"date":"2023-08-22T06:17:29","date_gmt":"2023-08-22T06:17:29","guid":{"rendered":"https:\/\/shadowboat.app\/lancexapp\/?p=27"},"modified":"2024-01-08T07:04:59","modified_gmt":"2024-01-08T07:04:59","slug":"scripting","status":"publish","type":"post","link":"https:\/\/shadowboat.app\/lancexapp\/scripting\/","title":{"rendered":"Scripting"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>You can use JavaScript to extend the ability of LanceX.<\/p>\n<h2>Public API<\/h2>\n<h3><code>$done()<\/code> function<\/h3>\n<p>Since all scripts run asynchronously, $done() should be called to indicate completion, even for scripts that do not require results. Otherwise, the script will print a warning due to a timeout.<\/p>\n<h3>Basic Information<\/h3>\n<ul>\n<li><code>$lancex<\/code><\/li>\n<\/ul>\n<p>The object means that you are in the environment of LanceX.<\/p>\n<h3>Persistent Store<\/h3>\n<ul>\n<li><code>$persistentStore.write(data&lt;String&gt;, [key&lt;String&gt;])<\/code><\/li>\n<\/ul>\n<p>Save data permanently. Only a string is allowed<\/p>\n<ul>\n<li><code>$persistentStore.read([key&lt;String&gt;])<\/code><\/li>\n<\/ul>\n<p>Get the saved data. Return a string or null.<\/p>\n<h3>Http Request<\/h3>\n<ul>\n<li><code>$httpClient.post(URL&lt;String&gt; or options&lt;Object&gt;, callback&lt;Function&gt;)<\/code><\/li>\n<\/ul>\n<p>Start an HTTP POST request. The first parameter can be a URL or object. An example object may look like that.<\/p>\n<pre><code>{\n    url: &quot;http:\/\/www.example.com\/&quot;,\n    headers: {\n        &quot;Content-Type&quot;: &quot;application\/json&quot;\n    },\n    body: &quot;{}&quot;,\n    timeout: 5,\n    callback: callback(error, response, data) {\n\n    }\n}\n<\/code><\/pre>\n<p>The <code>url<\/code> is always required. <code>body<\/code> can be a string or object. When presenting an object, it is encoded to JSON string, and the <code>Content-Type<\/code> is set to <code>application\/json<\/code> automatically.<\/p>\n<p>Similar function: <code>$httpClient.get<\/code>, <code>$httpClient.put<\/code>\uff0c<code>$httpClient.delete<\/code>, <code>$httpClient.head<\/code>, <code>$httpClient.options<\/code>, <code>$httpClient.patch<\/code>.<\/p>\n<ul>\n<li><code>fetch(URL&lt;String&gt;, options&lt;Object&gt;)<\/code><\/li>\n<\/ul>\n<p>It\u2019s an implementation of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Fetch_API\">Fetch API<\/a>, for example<\/p>\n<pre><code>fetch(&quot;http:\/\/www.example.com\/&quot;,{\n    method: &quot;POST&quot;,\n    headers: {\n        &quot;Content-Type&quot;: &quot;application\/json&quot;\n    },\n    body: &quot;{}&quot;\n}).then(v=&gt;v.text())\n.then(v=&gt;console.log(v));\n<\/code><\/pre>\n<h3>Utilities<\/h3>\n<ul>\n<li><code>console.log(message&lt;String&gt;)<\/code><\/li>\n<\/ul>\n<p>Log to LanceX logfile. Similar function: <code>console.warn<\/code>, <code>console.error<\/code>, <code>console.info<\/code>.<\/p>\n<ul>\n<li><code>setTimeout(function[, delay])<\/code><\/li>\n<\/ul>\n<p>Same as the setTimeout in browsers. Similar function: <code>setInterval<\/code>.<\/p>\n<ul>\n<li><code>$notification.post(title&lt;String&gt;, subtitle&lt;String&gt;, body&lt;String&gt;, info&lt;Object&gt;)<\/code><\/li>\n<\/ul>\n<p>Post a notification. Please turn on <code>Allow Notifications<\/code> before using it. <code>info<\/code> allows you to open a url when user clicks the notification. For example:<\/p>\n<pre><code>{\n   &quot;open-url&quot; : &quot;https:\/\/www.example.com&quot;\n}\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-usage"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/shadowboat.app\/lancexapp\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shadowboat.app\/lancexapp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shadowboat.app\/lancexapp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shadowboat.app\/lancexapp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shadowboat.app\/lancexapp\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":7,"href":"https:\/\/shadowboat.app\/lancexapp\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":142,"href":"https:\/\/shadowboat.app\/lancexapp\/wp-json\/wp\/v2\/posts\/27\/revisions\/142"}],"wp:attachment":[{"href":"https:\/\/shadowboat.app\/lancexapp\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shadowboat.app\/lancexapp\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shadowboat.app\/lancexapp\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}