DOM
API for DOM manipulation in Seanime plugins. Each DOM operation involves communication between the plugin and the browser, so understanding performance considerations is important.
Pitfall
Users having multiple tabs open can lead to unexpected behavior.
This happens because DOM manipulation events are broadcasted to all connected clients.
Core Methods
onReady
Executes a callback when the DOM is ready.
Parameters:
callback
: Function to execute
Example:
query
Queries the DOM for elements matching the selector.
Parameters:
selector
: CSS selector stringoptions
: (Optional)withInnerHTML
: Boolean - Include theinnerHTML
property in the matched elementsidentifyChildren
: Boolean - Assign IDs to all child elements
Returns: Promise<DOMElement[]>
Example:
queryOne
Queries the DOM for a single element matching the selector.
Parameters:
selector
: CSS selector stringoptions
: Same as query()
Returns: Promise<DOMElement | null>
Example:
observe
Observes changes to the DOM for elements matching the selector. Returns functions to stop observing and to manually refetch elements.
Parameters:
selector
: CSS selector stringcallback
: Function(elements: DOMElement[]) => voidoptions
: Same as query()
Returns: [stopObserving: () => void, refetch: () => void]
Example:
createElement
Creates a new DOM element.
Parameters:
tagName
: HTML tag name
Returns: Promise
Example:
asElement
Returns a DOM element object from an element ID. Useful when using identifyChildren option.
Parameters:
elementId
: String ID of the element
Returns: DOMElement
Example:
DOM Element Methods
Content Methods
getText()
Gets the text content of the element.
Returns: Promise
Example:
setText(text)
Sets the text content of the element.
Parameters:
text
: String to set as text content
Example:
getAttribute(name)
Gets the value of an attribute.
Parameters:
name
: Attribute name
Returns: Promise<string | null>
Example:
getAttributes()
Gets all attributes of the element.
Returns: Promise<Record<string, string>>
Example:
setAttribute(name, value)
Sets the value of an attribute.
Parameters:
name
: Attribute namevalue
: Attribute value
Example:
removeAttribute(name)
Removes an attribute.
Parameters:
name
: Attribute name
Example:
hasAttribute(name)
Checks if the element has an attribute.
Parameters:
name
: Attribute name
Returns: Promise
Example:
Style Methods
setStyle(property, value)
Sets a style property on the element.
Parameters:
property
: CSS property namevalue
: Property value
Example:
getStyle(property?)
Gets the style of the element.
Parameters:
property
: (Optional) Property name
Returns: Promise<string | Record<string, string>>
Example:
removeStyle(property)
Removes a style property.
Parameters:
property
: CSS property name
Example:
hasStyle(property)
Checks if the element has a style property set.
Parameters:
property
: CSS property name
Returns: Promise
Example:
getComputedStyle(property)
Gets the computed style of the element.
Parameters:
property
: CSS property name
Returns: Promise
Example:
CSS Class Methods
addClass(className)
Adds a class to the element.
Parameters:
className
: CSS class name
Example:
hasClass(className)
Checks if the element has a class.
Parameters:
className
: CSS class name
Returns: Promise
Example:
DOM Traversal and Manipulation
append(child)
Appends a child to the element.
Parameters:
child
: DOMElement to append
Example:
before(sibling)
Inserts a sibling before the element.
Parameters:
sibling
: DOMElement to insert
Example:
after(sibling)
Inserts a sibling after the element.
Parameters:
sibling
: DOMElement to insert
Example:
remove()
Removes the element from the DOM.
Example:
getParent(opts?)
Gets the parent of the element.
Parameters:
opts
: (Optional) Same options as query()
Returns: Promise<DOMElement | null>
Example:
getChildren(opts?)
Gets the children of the element.
Parameters:
opts
: (Optional) Same options as query()
Returns: Promise<DOMElement[]>
Example:
query(selector)
Queries the DOM for elements that are descendants of this element and match the selector.
Parameters:
selector
: CSS selector string
Returns: Promise<DOMElement[]>
Example:
queryOne(selector)
Queries the DOM for a single element that is a descendant of this element and matches the selector.
Parameters:
selector
: CSS selector string
Returns: Promise<DOMElement | null>
Example:
addEventListener(event, callback)
Adds an event listener to the element.
Parameters:
event
: Event name (e.g., "click")callback
: Function to call when the event occurs
Returns: Function to remove the event listener
Example:
Performance Best Practices
Minimize Roundtrips
Each async DOM method call represents a roundtrip between your plugin (on the server) and the browser. Minimize these for better performance.
Inefficient:
Efficient:
Use observe() Efficiently
When using observe(), apply performance optimizations to handle elements efficiently.
Example:
Last updated