Bearweb Page Util

The article demonstrates how to use the util functions to manipulate the webpage.

--by @ Jan 27, 2026

Index

Query Selector

For shortcuts:

For example:

DOM

dom(j) function is a shortcut of the vanilla element = document.createElement(), element.attrX = value and element.appendChild() functions. It accepts a DOM tree in JSON format, for example:


dom({_: 'ul', children:[
	{_: 'li', textContent: 'Point 1'},
	{_: 'li', textContent: 'Point 2, with attributes', style: 'color:red'},
	{_: 'li', textContent: 'Point 3, with children ', children: [
		{_: 'code', textContent: 'child'}
	]}
]})
	

In this JSON, there are 2 special keys:

The above example will generate:


<ul>
	<li>Point 1</li>
	<li style="color: red;">Point 2, with attributes</li>
	<li>Point 3, with children <code>child</code></li>
</ul>
	

You may pass a HTML string to create a DOM. For example, the following two are equivalent:


dom({_: 'p', textContent: 'ABC'});
dom('<p>ABC</p>').children[0];
	

Dialog

Dialog is used to provide passive information to the user.

The dialog will be displayed at the bottom of the viewport. You may stack multiple dialogs, the older dialog will be stacked at the bottom, newer at the top.

You may optionally provide a timeout. The dialog will be destroyed once timeout is reached. If the timeout is 0, the user needs to manually close the dialog by clicking the dialog.

Dialog is passive. The user can continue to read and interact with the webpage.

To display a dialog, use dialog(d, delay = 5000, color = 'lightgrey'), where:

For example:


dialog('Some string');
dialog('Need manual close', 0, 'blue');
dialog(dom(
	{_: 'p', children: [
		{_: 'span', textContent: 'Complex ', style: 'color:red'},
		{_: 'span', textContent: 'element ', style: 'color:blue'},
		{_: 'span', textContent: 'dialog', style: 'color:orange'}
	]}
), 0, 'blue');
	

Click the following buttons to fire the dialogs:

Bearweb provides 3 shortcuts specially optimized for client-server API use:


dialog_success(200, 'Success message');
dialog_warning(300, 'Warning message', 2000);
dialog_error(500, 'Error message', 0);
	

Where:

Click the following buttons to fire the dialogs:

Modal

On the other hand, modal is used to provide non-passive information to the user.

The modal will be displayed at the center of the viewport. You can only place one modal.

Modal is non-passive. The user cannot interact with the page while the modal is displayed.

To display the modal, usemodal(dom), where:

To close the modal, use modal() without parameters.

When you create the modal, remember to include a button to close the modal.

For example:


modal(dom({_: 'div', children: [
	{_: 'p', textContent: '🍪 This website uses cookies for essential functionalities. By continuing accessing the site, you accept the cookies.'},
	{_: 'button', textContent: 'I consent', onclick: () => modal(), style: 'width:100%;margin:0;'}
]}));
	

Click the following buttons to fire the modal: