{"id":1449,"date":"2024-04-03T14:57:05","date_gmt":"2024-04-03T12:57:05","guid":{"rendered":"https:\/\/inseri.swiss\/?p=1449"},"modified":"2025-11-26T14:57:05","modified_gmt":"2025-11-26T14:57:05","slug":"javascript-code-block","status":"publish","type":"post","link":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/2024\/04\/javascript-code-block\/","title":{"rendered":"JavaScript Code Block"},"content":{"rendered":"\n<p>The JavaScript Code Block allows to run JavaScript code in a separate web worker. It is therefore ideal for performing computations in the background.<\/p>\n\n\n\n<p>It is possible to install new packages using <code>importScripts<\/code>. For example we will get the current date and time.<\/p>\n\n\n\n<p>The JavaScript block can run on demand (press <strong>Run<\/strong>), automatically, and one can even hide it.<\/p>\n\n\n\n<div class=\"wp-block-inseri-core-javascript\" data-attributes=\"{&quot;autoTrigger&quot;:false,&quot;blockId&quot;:&quot;iCdFY5DDwAQPYE4ofrTuJ&quot;,&quot;content&quot;:&quot;importScripts('https%3A%2F%2Fcdn.jsdelivr.net%2Fnpm%2Fdayjs%401.11.10%2Fdayjs.min.js')%0Aconst%20now%20%3D%20dayjs().format()&quot;,&quot;editable&quot;:false,&quot;height&quot;:110,&quot;inputCode&quot;:&quot;&quot;,&quot;inputs&quot;:{},&quot;isVisible&quot;:true,&quot;label&quot;:&quot;Click Run to run the JavaScript code&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;JS-now&quot;},&quot;mode&quot;:&quot;editor&quot;,&quot;outputs&quot;:[[&quot;now&quot;,&quot;application\/json&quot;]]}\">is loading &#8230;<\/div>\n\n\n\n<p>And below we display the value in a Text Viewer block.<\/p>\n\n\n\n<div class=\"wp-block-inseri-core-text-viewer\" data-attributes=\"{&quot;blockId&quot;:&quot;Xo37odHy9bV3VaQ22RTws&quot;,&quot;content&quot;:&quot;&quot;,&quot;height&quot;:60,&quot;inputKey&quot;:&quot;iCdFY5DDwAQPYE4ofrTuJ\/now&quot;,&quot;label&quot;:&quot;The value of now:&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;textViewer-eZY&quot;}}\">is loading &#8230;<\/div>\n\n\n\n<p>The block allows asynchronous behaviour and <code>await<\/code> is permitted within the main body of the code. Imagine that we want to fetch an image. We can use <code>fetch<\/code> directly without having it within an <code>async<\/code> function.<\/p>\n\n\n\n<div class=\"wp-block-inseri-core-javascript\" data-attributes=\"{&quot;autoTrigger&quot;:false,&quot;blockId&quot;:&quot;s662RNO6aATrVBG4LvHau&quot;,&quot;content&quot;:&quot;const%20urlImage%20%3D%20%22https%3A%2F%2Fraw.githubusercontent.com%2Finseri-swiss%2Finseri-core-wp%2Fmain%2F.wordpress-org%2Fbanner-1544x500.jpg%22%0Aconst%20response%20%3D%20await%20fetch(urlImage)%0Aconst%20responseImage%20%3D%20await%20response.blob()&quot;,&quot;editable&quot;:false,&quot;height&quot;:155,&quot;inputCode&quot;:&quot;&quot;,&quot;inputs&quot;:{},&quot;isVisible&quot;:true,&quot;label&quot;:&quot;Click Run to fetch the image&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;JS-fetch&quot;},&quot;mode&quot;:&quot;editor&quot;,&quot;outputs&quot;:[[&quot;responseImage&quot;,&quot;image\/jpeg&quot;]]}\">is loading &#8230;<\/div>\n\n\n\n<p>Next we display the image by using the Image Box block.<\/p>\n\n\n\n<div class=\"wp-block-inseri-core-image\" data-attributes=\"{&quot;altText&quot;:&quot;&quot;,&quot;blockId&quot;:&quot;2ixBDe1iN3keszi6BI186&quot;,&quot;caption&quot;:&quot;&quot;,&quot;fit&quot;:&quot;contain&quot;,&quot;height&quot;:null,&quot;inputKey&quot;:&quot;s662RNO6aATrVBG4LvHau\/responseImage&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;image-zfM&quot;}}\">is loading &#8230;<\/div>\n\n\n\n<p>For more details please see the corresponding\u00a0<a href=\"https:\/\/inseri-swiss.github.io\/inseri-core-wp\/blocks\/javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">documentation<\/a>.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:60%\"><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-inseri-core-export alignright\" data-attributes=\"{&quot;align&quot;:&quot;right&quot;,&quot;blockId&quot;:&quot;vaxQQCzmrnvQCf4meQgfk&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;export&quot;},&quot;postId&quot;:1449}\">is loading &#8230;<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-inseri-core-share alignright\" data-attributes=\"{&quot;align&quot;:&quot;right&quot;,&quot;blockId&quot;:&quot;KTETwIissQyi0jL3ZpuwV&quot;,&quot;copiedText&quot;:&quot;Copied&quot;,&quot;metadata&quot;:{&quot;name&quot;:&quot;share&quot;},&quot;showIcon&quot;:true,&quot;text&quot;:&quot;Share&quot;}\">is loading &#8230;<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The JavaScript Code Block allows to run JavaScript code in a separate web worker. It is therefore ideal for performing computations in the background. It is possible to install new packages using importScripts. For example we will get the current date and time. The JavaScript block can run on demand (press Run), automatically, and one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[40,43,44,50,53],"class_list":["post-1449","post","type-post","status-publish","format-standard","hentry","category-inseri-core-block","tag-export","tag-image-box","tag-javascript-code","tag-share","tag-text-viewer"],"_links":{"self":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/1449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/comments?post=1449"}],"version-history":[{"count":1,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/1449\/revisions"}],"predecessor-version":[{"id":2129,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/posts\/1449\/revisions\/2129"}],"wp:attachment":[{"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/media?parent=1449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/categories?post=1449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zi.uzh.ch\/whp\/science-it\/inseri\/wp-json\/wp\/v2\/tags?post=1449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}