module.js
API
The module.js script is the actual logic behind a module. It exists in a separate context from the module webpage.
While on the module page within Somiibo, you can press cmd+option+i or ctrl+shift+i to open the DevTools which includes a console.
From within the module.js script you can require any core Node.js module as well as any of the supported 3rd party modules.
Overview
The module.js
script is the actual logic behind a module. It controls the webpages and performs actions on websites.
The Somiibo library is the first argument passed to the exported function, so as seen here, mod can be stored to a variable somiibo in the global scope so that it can be accessed throughout the rest of the module's lifecycle.
Using Node.js Modules in module.js
From within the module.js
script you can require
any Node.js module that is a dependency in Somiibo.
Visit somiibo://developer and go to the "Using Node.js Modules" tab to see the currently supported modules.
module.js
Skeleton File
Want to see a full example?
You can download a fully working Module Package from our examples repo on GitHub.API
Discover the module.js
API
Flow
Flow controls the initialization and looping of the module.
somiibo.initialize(fn)
fn
<function(Object)> A function to executedefaults
<Object> A mutable object representing the defaults for every methodreturns
<null>
.initialize()
runs only once and, as such, it is an easy way to implement any configuration or setup that does not need to be executed multiple times. Any subsequent calls to .initialize()
will be ignored. defaults for each method can be overwritten here via defaults[library][method][name] = 'new'
.
somiibo.loop(fn, delay)
fn
<function(Object)> A function to executedelay
<?number> Number of milliseconds to wait before the script loops and defaults to1000
returns
<null>
.loop()
drives the repeating nature of modules by executing the fn
passed to it, effectively starting the module from the top again.
somiibo.stop()
returns
<null>
Stop the module's execution. Calling this is the same as if the user pressed the 'stop' button on the module manager page.
somiibo.error(reason)
returns
<null>
Stop module execution and displays an error message with reason
.
somiibo.wait(minTime, maxTime)
minTime
<number> Minimum time in milliseconds to waitmaxTime
<number> Maximum time in milliseconds to waitreturns
<Promise(number)> The promise resolves when the wait is over with the number of milliseconds waited.
This method pauses execution of the script for the amount of minTime
specified in milliseconds. If maxTime
is also supplied, a random duration between the two is waited.
Settings
somiibo.getSetting(path, def)
path
<string> Path to the settings key to retrievedef
<any> The default value to provide if the setting does not existreturns
<any>
Get the user's settings for this module. If path
is not supplied, the entire settings object will be returned.
Development & Debugging
somiibo.log([, ...args])
...args
<...Serializable> Arguments to pass to the consolereturns
<null>
Executes console.log
in the module's DevTools console.
somiibo.openDevTools()
returns
<null>
Opens the module's dev tools.
Workers
somiibo.worker(fn, options)
fn
<Function> Function to be passed to the worker processoptions
<Object> Function to be passed to the worker processarguments
<Array> Arguments to be passed to the worker scripturl
<string> The URL to open the worker toreferrer
<string> An HTTP Referrer URLuserAgent
<string> A user agent originating the requestproxy
<string> A proxy that the worker should connect totimeout
<number> A maximum number of milliseconds before the worker is automatically closedwidth
<number> The width of the worker windowheight
<number> The height of the worker windowreturns
<Promise(number)> This method resolves when the worker launches successfully. The resolve will always happen before the worker times out.
This method launches a worker process—an isolated subpage—within the module webpage. Workers are useful for bulk tasks.
Note: Currently, launching a worker requires a page navigation to a custom inner page prior to execution. This process happens automatically. For workers to be enabled on a module, the main.json
module properties must contain webview: true
.
Miscellaneous methods
somiibo.alert(options)
options
<Object> Function to be passed to the alert processtype
<string> Type of the alerttitle
<string> Title of the alertcontent
<string> HTML enabled content of the alertbutton
<string> Text on the alert's buttonreturns
<null>
Display an alert message to the user.
Events
event: 'error'
event
<Event> The generic eventerror
<Error> The error object thrown by the modulemeta
<Object> Includes information about the errornavigation
<boolean> Whether or not the error was due to navigation
Emitted when the module.js script encounters a fatal error. The default behavior is to stop the module and display an error to the user but this can be prevented with event.preventDefault()
. Usually (but not always), navigation errors can be recovered from and calling event.preventDefault()
is advised.
event: 'new-worker'
event
<Event> The generic eventpayload
<Object> Includes information on the success or failure to setup the workersuccess
<boolean> Whether or not the new worker was setup successfully
Emitted when a worker completes its setup phase.
somiibo.browser()
API
This API is responsible for navigating and interacting with the single browser window that each module gets.
somiibo.browser().navigate(url, options)
url
<string> A URL to navigate tooptions
<Object>referrer
<string> An HTTP Referrer URLuserAgent
<string> A user agent originating the requestreturns
<Promise(null)> The promise resolves when the page finishes loading
Navigates the module webpage.
somiibo.browser().navigateBack(offset)
offset
<number> An offset to navigate backwardsreturns
<Promise(null)> The promise resolves when the page finishes loading
Navigates the module webpage backwards as if the user pressed the back button. offset
is the number of pages in the history to go back and defaults to 1
.
Note: If the offset
provided doesn't exist, no navigation will occur.
somiibo.browser().navigateForward(offset)
offset
<number> An offset to navigate forwardsreturns
<Promise(null)> The promise resolves when the page finishes loading
Navigates the module webpage forwards as if the user pressed the forward button. offset
is the number of pages in the history to go forward and defaults to 1
.
Note: If the offset
provided doesn't exist, no navigation will occur.
somiibo.browser().reload(offset)
returns
<Promise(null)> The promise resolves when the page finishes reloading
Reloads the current webpage without affecting the history index as if the user pressed the reload/refresh button.
somiibo.browser().getURL()
returns
<string> The promise resolves when the page finishes geturling
Returns the webpage's current URL.
somiibo.browser().select(selector, options)
selector
<string> A selector to query page foroptions
<Object>index
<?number, ?string> Index of the element array to returnfilters
<?Array(Object)> An array of filter objects that narrow down the returned arrayretrieve
<?Array(string)> An array of attributes or element properties to include in the returned arraywait
<?number> A maximum number of milliseconds to wait as the selector is periodically polledpositionMethod
<?string> Control whether the position is calculatedcumulative
(default) orbounding
returns
<Promise(Object)> This method resolves when the element is found or determined to not exist and returns a jQuery-like representation of the selected elements.
This method runs document.querySelectorAll
within the page and stores the result in somiibo.browser().properties.select
. The currently selected element is stored until overwritten by another call of .select()
or a navigation occurs.
The positionMethod
determines how the elements x and y are calculated.
Note: Since the module webpage runs in a separate process, it is not possible to access the ElementHandle directly from somiibo.browser().properties.select
. Instead, somiibo.browser().properties.select
will contain a jQuery-like representation of the result.
somiibo.browser().scroll(position, options)
position
<?string, ?Object> Position or element on page to scroll tooptions
<Object>offsetX
<?number> Offset scroll amount on x axisoffsetY
<?number> Offset scroll amount on y axisreturns
<Promise(Object)> This method resolves when the in-page scroll completes or when page navigation completes (if the scroll triggers a navigation) and returns the result of the scroll.
This method scrolls the page based on the current position of the mouse and stores the result in somiibo.browser().properties.scroll
.
If position is undefined
or somiibo.browser().$selected
, the method will try to scroll to the currently selected element.
offsetX
and offsetY
are absolutely necessary if there are fixed navs or footers on the page or else the element being scrolled to might get stuck under the nav/footer.
somiibo.browser().move(position, options)
position
<?string, ?Object> Position or element on page to move the mouse tooptions
<Object>offsetX
<?number> Offset move amount on x axisoffsetY
<?number> Offset move amount on y axisignoreScrollOffset
<?boolean> This method resolves when the in-page mouse movement is complete or when page navigation completes (if the mouse movement triggers a navigation) and returns the result of the mouse movement.returns
<Promise(Object)> This method resolves when the in-page move completes or when page navigation completes (if the move triggers a navigation) and returns the result of the move.
This method moves the mouse to a position or element and stores the result in somiibo.browser().properties.mouse
.
somiibo.browser().click(position, options)
position
<?string, ?Object> Position or element on page to clickoptions
<Object>offsetX
<?number> Offset click amount on x axisoffsetY
<?number> Offset click amount on y axismove
<?boolean> Whether to move the mouse to the position before the click event is fired (defaults totrue
)ignoreScrollOffset
<?boolean> Whether to ignore window.scrollX & window.scrollY when calculating path. This should usually be true if the target element exists on an overlay or popup.returns
<Promise(Object)> This method resolves when the in-page mouse click is complete or when page navigation completes (if the click triggers a navigation) and returns the result of the mouse click.
If position is undefined or somiibo.browser().$selected
, the method will try to click on the currently selected element.
somiibo.browser().type(input, options)
input
<Array(string)> An array of strings to typeoptions
<Object>delayMin
<?number> Minimum delay between keystrokesdelayMax
<?number> Maximum delay between keystrokesreturns
<Promise(Object)> This method resolves when the in-page typed string is complete or when page navigation completes (if the typed string triggers a navigation) and returns the result of the keyboad type.
Special keys such as Enter
must be in their own array element:
['Space', 'Tab', 'Backspace', 'Delete', 'Up', 'Down', 'Left', 'Right', 'Escape', 'Enter']
somiibo.browser().getVariable(path, compare, options)
path
<string> Path to the variablecompare
<?Function> Function to compare the result of the variablevalue
<any> The requested variable's current valueoptions
<Object>wait
<?number> A maximum number of milliseconds to wait for as the variable is periodically polled.returns
<Promise(any)> This method resolves when the variable is retrieved and either matches the compare function or times out.
Get and return a variable from within the webpage. Only global scoped variables are acessible. The variable polling will continue until either the compare function returns true
or a maximum amount of wait
time elapses.
somiibo.browser().execute(fn, options)
fn
<Function> Function as a string to execute within the module webpageoptions
<Object>arguments
<Array(...any)> Arguments to be passed to the scriptreturns
<Promise(Object)> A promise that resolves with an object with keyssuccess
<boolean>true
if the script executed successfully or elsefalse
result
<any> result returned from the fn or an Error message
This method executes a function in the context of the module webpage. Whatever is returned from the function will be passed to the result property of the resolved Promise value.
somiibo.browser().solveCaptcha(options)
options
<Object>mode
<string> The type of captcha to solve (currentlyreCaptchaV2
&hCaptcha
are supported)service
<string> The type of captcha to solve (currently2Captcha
&antiCaptcha
are supported)auto
<?boolean> Whether the captcha should be automatically detected and solved (Defaults totrue
)sitekey
<?string> The sitekey of the captcha (will be automatically determined ifauto
istrue
)returns
<Promise(Object)> A promise that is rejected with an Error if the captcha fails or resolves with an object with keys:result
<string> The result of the captcha solve
This method can detect and solve a captcha on the module webpage. When auto is true, the API will attempt to determine mode & sitekey automatically but it is better to supply them if the captcha mode is known. service will automatically be set to whatever the user has chosen in their preferences but it can be overriden by setting it manually.
somiibo.browser()
Events
event: 'new-window'
event
<Event> The generic eventwindow
<Class> The error object thrown by the module.setVisibility(state, options)
state
<string> Visibility state of the window which can be hide or showoptions
<Object> Options for the visibility functioninactive
<?boolean> Activity state of the window which can be true or false where true will not focus the windowreturns
<null>.navigate(url)
url
<string> URL to navigate toreturns
<Promise(null)> A promise that resolves when the page finishes navigating or rejects if the navigation fails.setAudioMuted(state)
state
<boolean> Audibility state of the window which can betrue
orfalse
returns
<null>.setFocusable(state)
state
<boolean> Controls the user's ability to focus the window which can betrue
orfalse
returns
<null>.setAlwaysOnTop(state)
state
<boolean> Controls the windows position on top of other windows which can betrue
orfalse
returns
<null>.getURL()
returns
<string> The current URL of the window.openDevTools()
Open the DevTools of the windowreturns
<null>.setScript(script, options)
Set a script that will be executed within the windowscript
<Function> Script that will be executed on every navigation of the windowoptions
<Object> Open the DevTools of the windowarguments
<Array(any)> An array of arguments that are passed to the scriptreturns
<null>- Note: The script has full access to the DOM and the
window
object since it is executed directly on the page
Emitted when the module browser window receives a request to open a new window. The default behavior is to open the new window but the window will be hidden, unfocused, and muted.
If you do not expect your module to open windows, you should listen for this event and call window.close() immediately.
somiibo.device()
API
This API is responsible for interacting with the user's device and getting information about the user's device and OS.
somiibo.device().getOS()
returns
<Object> An object with propertiesplatform
<string> The platform (in Node.js terms)name
<string> The platform's simple nameversion
<string> The OS version
Returns the current user's operating system data as an object.
somiibo.tabs()
API
This API is responsible for interacting with other open tabs on Somiibo
somiibo.tabs().query(fn)
fn
<Function> A filtering function that is called on tab object withtab
andindex
tab
<Object> A read-only object representing the tabid
<number> The ID of the tabactive
<boolean> Boolean representing if the tab has focusurl
<string> The current URL of the tabtype
<string> Type of the tabs (module or browser)module
<Object> Object representing the module if type is modulemoduleId
<string> The ID of the module from the module's packagepackageId
<string> The ID of the packageproperties
<Object> An object of module propertiesrunning
<boolean> The run state of the modulesettings
<Object> Object representing the module's running settingswebContentsId
<number> The webContents IDwebContentsId
<number> The webContents IDsession
<string> The Session ID of the tabpartition
<string> The Session partition of the tabuserAgent
<string> The userAgent string of the tabindex
<number> The index of the loopreturns
<Array(Class)> An array oftab
objects matching the query filter
Iterates through all opened tabs returns an array of tabs matching the filter fn
. Return true
inside the fn
to include the tab in the result.
somiibo.tabs().add(options)
options
<Object>url
<string> The URL of the new tabsession
<?string> The Session ID of the new tabreturns
<Promise(Object)> The promise resolves when the tab is opened and passes the newtab
object.
Opens a new tab with the options. In the future, this method will be able to open tabs with the module type. You can get the session ID of a Session from somiibo://settings in the Sessions panel.
somiibo.tabs().close(id)
id
<number> ID of the tab to closereturns
<Promise(null)> The promise resolves when the tab is closed.
Closes a tab with id
.
somiibo.tabs().navigate(id, url, options)
id
<number> ID of the tab to navigateurl
<string> The URL to navigate tooptions
<Object>referrer
<string> An HTTP Referrer URLuserAgent
<string> A user agent originating the requestreturns
<Promise(null)> The promise resolves when the page finishes loading.
Navigates the tab with id
to the url
.
somiibo.tabs().start(id)
id
<number> ID of the tab to startreturns
<Promise(null)> A Promise that resolves when the module finishes starting or rejects if startup failed
Start the tab's module with id
.
somiibo.tabs().stop(id)
id
<number> ID of the tab to stopreturns
<Promise(null)> A Promise that resolves when the module finishes stoping or rejects if stopup failed
Start the tab's module with id
.