Accessing the Plug-in Environment
In order to be useful, the Javascript you write in side panels and
other UI components needs to have access to the same environment and
built-in objects as other Javascript plug-in code. You need to be able
to require()
the content-studio
object, the notifier
object and so on. You can do
this, but you cannot do it directly.
The sayHello()
function in the example in
Creating Browser UIs shows what you have to
do in order to access the full
Content Studio
environment:
function sayHello() { var script = 'require("notifier").notify("Hello world!");'; scriptExecutor.executeScript(script); }
From any HTML file that you load into a
Content Studio
HTML component, you have access to a built-in object called
scriptExecutor
, which has a method called
executeScript()
. Any code executed by this method
has full access to the
Content Studio
Javascript environment. All the Javascript code you want to execute in
a side panel HTML file must therefore be predefined as a string and
then passed in as an argument to
scriptExecutor.executeScript()
.
Note that each call to
scriptExecutor.executeScript()
is executed
independently and no context is preserved between calls. So this, for
example, will work:
scriptExecutor.executeScript('hw="Hello world!";require("notifier").notify(hw);');
whereas this will not:
scriptExecutor.executeScript('hw="Hello world!";'); scriptExecutor.executeScript('hw="require("notifier").notify(hw);');