ShokaX DOM API
Available Partition: page ts
(Accessible on the frontend)
$dom Series
$dom
Used to fetch the first HTML node that matches a CSS selector. When using an ID, it retrieves via getElementById
, otherwise through querySelector
.
Parameters
selector: string
: CSS selectorelement: Document
: (Optional, defaults to document) Parent node for query
Return Type
HTMLElement
: The queried child node
Example
const Container = $dom("#container"); // Retrieved using getElementById
const article = $dom(".index.wrap"); // Retrieved using querySelector
article.innerHTML = "<p>Hello world</p>";
$dom.all
Uses querySelectorAll
to return all nodes that match the criteria. Generally not directly used.
Parameters
selector: string
: CSS selectorelement: Document
: (Optional, defaults to document) Parent node for query
Return Type
NodeListOf<HTMLElement>
: List of queried child nodes
$dom.each
Iterates over all nodes that match the selector and executes a callback.
Parameters
selector: string
: CSS selectorcallback: (value: HTMLElement, key?: number, parent?: NodeListOf<Element>) => void
: Callback functionelement: Document
: (Optional, defaults to document) Parent node for query
Return Type
void
Example (Excerpt from ShokaX page ts)
$dom.each(".post.block p.gallery", (element) => {
const box = document.createElement("div");
box.className = "gallery";
box.attr("data-height", String(element.attr("data-height") || 220));
box.innerHTML = element.innerHTML.replace(/<br>/g, "");
element.parentNode.insertBefore(box, element);
element.remove();
});
Asynchronous DOM API
This section includes $dom.asyncify
Experimental and $dom.asyncifyEach
Experimental. Due to the unstable code implementation, documentation is not provided at this time.
Prototype Injection Series
Prototype injections involve setting HTMLElement.prototype
to inject APIs.
HTMLElement.prototype.createChild
Creates an HTML element and places it.
Parameters
tag: string
: Tag name of the HTML element to create ('div', 'span', etc.)obj: Object
: Contains attributes and values to apply to the newly created HTML elementposition?: string
: (Optional) Represents where the newly created HTML element is placed. Options:after
: Insert the new element afterthis
replace
: Clear the content ofthis
and add the newly created element as the only child withinthis
- Otherwise: Default to adding the element as the last child of
this
Return Type
HTMLElement
: Newly created child element
Example (Excerpt from ShokaX page ts)
const BODY = document.getElementsByTagName("body")[0];
let siteSearch;
if (!siteSearch) {
siteSearch = BODY.createChild("div", {
id: "search",
innerHTML:
'<div class="inner"><div class="header"><span class="icon"><i class="ic i-search"></i></span><div class="search-input-container"></div><span class="close-btn"><i class="ic i-times-circle"></i></span></div><div class="results"><div class="inner"><div id="search-stats"></div><div id="search-hits"></div><div id="search-pagination"></div></div></div></div>',
});
}
HTMLElement.prototype.wrapObject
Wraps this
with a new div element.
Parameters
obj: Object
: Contains attributes and values to apply to the newly created div element
Return Type
void
Example (Excerpt from ShokaX page ts)
$dom.each(".md table", (element) => {
element.wrapObject({
className: "table-container",
});
});
HTMLElement.prototype.changeOrGetHeight
Change or retrieve the height of this
.
Parameters
h: number | string
: (Optional) If provided, sets the height ofthis
to the specified value. If a number, converts to string and adds 'rem' unit; otherwise, uses the value directly
Return Type
number
: Height ofthis
HTMLElement.prototype.changeOrGetWidth
Change or retrieve the width of this
.
Parameters
w: number | string
: (Optional) If provided, sets the width ofthis
to the specified value. If a number, converts to string and adds 'rem' unit; otherwise, uses the value directly
Return Type
number
: Width ofthis
HTMLElement.prototype.getTop
Get the distance of this
from the top of the viewport (offset from the top edge).
Return Type
number
: Distance ofthis
from the top of the viewport
HTMLElement.prototype.left
Get the distance of this
from the left of the viewport (offset from the left edge).
Return Type
number
: Distance ofthis
from the left of the viewport
HTMLElement.prototype.attr
Manipulate attributes of this
.
Parameters
type: string
: Attribute name to manipulatevalue: string
: (Optional) Attribute value. Options:null
: Remove the attribute with the giventype
- Exists (neither
null
norundefined
): Set the attribute withtype
name tovalue
undefined
or omitted: Return the value of the attribute withtype
name
Return Type
void | EventTarget | string
: Varies based on the value ofvalue
:void
:value
isnull
EventTarget
:value
exists (neithernull
norundefined
), returnsthis
(itself)string
:value
isundefined
or omitted, returns the value of the attribute withtype
name
Example (Excerpt from ShokaX page ts)
if ((info = element.attr("title"))) {
$imageWrapLink.attr("data-caption", info);
const para = document.createElement("span");
const txt = document.createTextNode(info);
para.appendChild(txt);
para.addClass(captionClass);
element.insertAfter(para);
}
HTMLElement.prototype.insertAfter
Inserts another element after this
.
Parameters
element: HTMLElement
: Element to insert
Return Type
void
HTMLElement.prototype.display
Set/get the `display
property of
this`.
Parameters
d: string
: (Optional) The display state to set for the element. If passed, sets thedisplay
property ofthis
to the specified value
Return Type
string | EventTarget
: Varies based ond
:string
: Ifd
is omitted, returns thedisplay
property ofthis
EventTarget
: Ifd
is provided, returnsthis
(itself)
Example (Excerpt from ShokaX page ts)
if (qr.display() === "inline-flex") {
transition(qr, 0);
} else {
transition(qr, 1, () => {
qr.display("inline-flex");
}); // slideUpBigIn
}
HTMLElement.prototype.child
Find the first child node that matches the selector.
Parameters
selector: string
: CSS selector
Return Type
HTMLElement
: Queried child node
HTMLElement.prototype.find
Find all child nodes of this
that match the selector.
Parameters
selector: string
: CSS selector
Return Type
NodeListOf<HTMLElement>
: List of queried child nodes
HTMLElement.prototype._class
Manipulate class names. Generally not directly used.
Parameters
type: string
: Type of class name operation. Options:add
remove
toggle
replace
className: string
: Class name or list of class names to operate ondisplay: boolean
: (Optional) Fortoggle
operations, whether to add or remove the class name
Return Type
void
HTMLElement.prototype.addClass
Add a class name.
Parameters
className: string
: Class name or list of class names to operate on
Return Type
EventTarget
: Returnsthis
(itself)
HTMLElement.prototype.removeClass
Remove a class name.
Parameters
className: string
: Class name or list of class names to operate on
Return Type
EventTarget
: Returnsthis
(itself)
HTMLElement.prototype.toggleClass
Toggle a class name.
Parameters
className: string
: Class name or list of class names to operate ondisplay: boolean
: (Optional) Forces adding or removing the class, regardless of whether the class name exists
Return Type
EventTarget
: Returnsthis
(itself)
HTMLElement.prototype.hasClass
Check if the specified class name exists within this
.
Parameters
className: string
: Class name or list of class names to operate on
Return Type
boolean
: Whether the class name exists