Grant the userscript GM_xmlhttpRequest
, GM_getValue
, GM_setValue
and require the UserGui, like so.
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @require https://github.com/AugmentedWeb/UserGui/raw/Release-1.0/usergui.js
Please check the main page for the latest release version, this might be outdated.
const Gui = new UserGui;
Gui.settings
has configuration options for the GUI and its window, such as.
Gui.settings.window.title = "GUI Demo"; // set window title
Gui.settings.window.centered = true; // GUI starts at the center of the screen
Gui.settings.gui.internal.darkCloseButton = true; // Changes the close button to dark theme
Use BeautifyTools' Form Builder to create your GUI elements. After designing the form, press the "Get HTML" button.
NOTE: Other form builders are not supported, please only use the BeautifyTool's form builder.
Insert the form's HTML string, as a whole, into the Gui.addPage(tabName, htmlString)
function's @htmlString parameter, like so.
Gui.addPage("Some tab name", `
<div class="rendered-form">
<div class="formbuilder-button form-group field-button-1655324182259">
<button type="button" class="btn-default btn" name="button-1655324182259" access="false" style="default" id="button-1655324182259">Button</button>
</div>
</div>
`);
Learn more about addPage function
Use Gui.open(readyFunction)
to open the GUI.
Gui.open(() => {
// learn more in step 6
});
Learn more about open function
Currently, your GUI is a dead shell. We need to add event listeners to make it alive.
Inside the Gui.open(readyFunction)
's ready function, add events using event(name, event, eventFunction)
, like so.
Gui.open(() => {
Gui.event("button-1655324182259", "click", () => {
console.log("Button was clicked!");
});
});
Learn more about event function
In this example, since our @name parameter "button-1655324182259" has the type prefix "button-", we can use a simplified function smartEvent(name, eventFunction)
to achieve the same thing. The BeautifyTools' Form Builder has the prefix in the name as default, please don't change it if you want to use the smartEvent function.
Learn more about the name parameter
Gui.open(() => {
Gui.smartEvent("button-1655324182259", () => {
console.log("Button was clicked!");
});
});
Learn more about smartEvent function
The final result could look like this,
// ==UserScript==
// @name Example-GUI
// @namespace HKR
// @match https://example.com/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @version 1.0
// @author HKR
// @description This is an example userscript made for UserGui
// @require https://github.com/AugmentedWeb/UserGui/raw/Release-1.0/usergui.js
// ==/UserScript==
const Gui = new UserGui;
Gui.settings.window.title = "GUI Demo"; // set window title
Gui.settings.window.centered = true; // GUI starts at the center of the screen
Gui.settings.window.external = true; // GUI opens up externally
Gui.addPage("Some tab name", `
<div class="rendered-form">
<div class="formbuilder-button form-group field-button-1655324182259">
<button type="button" class="btn-default btn" name="button-1655324182259" access="false" style="default" id="button-1655324182259">Button</button>
</div>
</div>
`);
Gui.open(() => {
Gui.smartEvent("button-1655324182259", () => {
console.log("Button was clicked!");
});
});
UserGui has one parameter that you have to know if you want to achieve greatness. That's the name
parameter which you see on almost every function. It allows UserGui to locate the form element of your choosing, and traverse trough its elements.
Using the BeautifyTools' Form Builder, hover over your form element, and click on the pencil to edit the element. The menu that slides down has the name variable. That's what you're going to use when the function asks for a name
parameter!
It's also visible on your form element's HTML, like in this "Date Field" date-1655383407790
. How many can you spot?
<div class="formbuilder-date form-group field-date-1655383407790">
<label for="date-1655383407790" class="formbuilder-date-label">Date Field</label>
<input type="date" class="form-control" name="date-1655383407790" access="false" id="date-1655383407790">
</div>
If you answered 4, that's correct!
addPage(tabName, htmlString)
Adds a page object to the GUI page array. This function is used to add form content to the GUI. If multiple pages are added, a navigation bar will automatically be generated.
@tabName (String) Navigation bar tab's title
@htmlString (String) HTML form content taken from BeautifyTools' Form Builder
None
open(readyFunction)
Opens the GUI. Done either externally via a new window, or internally via iFrame.
@readyFunction (Function) Gets called after the GUI has initialized. Might contain user's event functions to functionalize the GUI.
None
close()
Closes the GUI. Externally,
window.close()
is called. Internally, the whole iFrame will be removed.None
None
saveConfig()
Saves the current GUI form element's values.
None
None
loadConfig()
Loads the saved GUI form element's values.
None
None
getConfig()
Gets the config from the userscript manager's storage.
None
(Array) The saved GUI config as an object array. The objects contain a form element's name and value.
resetConfig()
Resets the config. The array of stored values will be wiped.
None
None
dispatchFormEvent(name)
Dispatches an event on a GUI form element
@name (String) Form element's name, taken from BeautifyTools' Form Builder
None
setPrimaryColor(hex)
Changes the GUI Header's, Navbar's and Bootstrap's primary text's color
@hex (String) A hex color code
None
event(name, event, eventFunction)
Creates an event listener for a GUI form element.
@name (String) Form element's name, taken from BeautifyTools' Form Builder
@event (String) Event to listen for (e.g. click, change)
@eventFunction (Function) Function to be called when event is activated
None
smartEvent(name, eventFunction)
Creates an event listener for a GUI form element, but automatically determines the best listener type for the element. Requires the name parameter to have the type as a prefix (e.g. "button-", "select-"). An example name could be
select-1655377908386
, ortextbox-cool-text
.
"Button" -> listen for "click"
"Textarea" -> listen for "input"
e.g.
@name (String) Form element's name, taken from BeautifyTools' Form Builder. Has to have the type prefix (e.g. "button-", "select-")
@eventFunction (Function) Function to be called when event is activated
None
disable(name)
Disables a GUI form element.
@name (String) Form element's name, taken from BeautifyTools' Form Builder
None
enable(name)
Enables a GUI form element.
@name (String) Form element's name, taken from BeautifyTools' Form Builder
None
getValue(name)
Gets a GUI form element's value. Works on types "Text Field", "Textarea", "Date Field" & "Number".
@name (String) Form element's name, taken from BeautifyTools' Form Builder
(String, Integer) The GUI form element's value
setValue(name, newValue)
Sets a GUI form element's value. Works on types "Text Field", "Textarea", "Date Field" & "Number".
@name (String) Form element's name, taken from BeautifyTools' Form Builder
@newValue (String, Integer) The new GUI form element's value to be set
None
getSelection(name)
Gets a GUI form element's selected option's value. Works on type "Radio Group".
@name (String) Form element's name, taken from BeautifyTools' Form Builder
(String, Integer) The GUI form element's selected option's value
setSelection(name, newOptionsValue)
Selects a GUI form element's option. Works on type "Radio Group".
@name (String) Form element's name, taken from BeautifyTools' Form Builder
@newOptionsValue (String, Integer) The GUI form element's option's value, which will be checked
None
getChecked(name)
Gets a GUI form element's all checked options' values. Works on type "Checkbox Group".
@name (String) Form element's name, taken from BeautifyTools' Form Builder
(Array) Array of a GUI form element's checked options' values
setChecked(name, checkedArr)
Sets a GUI form element's checked options. Works on type "Checkbox Group".
@name (String) Form element's name, taken from BeautifyTools' Form Builder
@checkedArr (Array) String array of the value's to be checked
None
getFiles(name)
Gets a GUI form element's files. Works on type "File Upload".
@name (String) Form element's name, taken from BeautifyTools' Form Builder
(Object) Object containing the files and additional information (e.g
FileList {0: File, length: 1}
)
getOption(name)
Gets a GUI form element's selected option's value. Works on type "Select".
@name (String) Form element's name, taken from BeautifyTools' Form Builder
(String, Integer) The GUI form element's selected option's value
setOption(name, newOptionsValue)
Sets a GUI form element's selected option. Works on type "Select".
@name (String) Form element's name, taken from BeautifyTools' Form Builder
@newOptionsValue (String, Integer) The GUI form element's option's value to be selected
None
getData(name)
Automatically determines the suitable "get" function for a GUI form element. Requires the name parameter to have the type as a prefix (e.g. "button-", "select-"). An example name could be
select-1655377908386
, ortextbox-cool-text
.
"Checkbox Group" -> use
getChecked(name)
e.g.
@name (String) Form element's name, taken from BeautifyTools' Form Builder. Has to have the type prefix (e.g. "button-", "select-")
(String, Integer, Array, Object) The GUI form element's data (e.g. element's value, element's selected values)
setData(name, newData)
Automatically determines the suitable "set" function for a GUI form element. Requires the name parameter to have the type as a prefix (e.g. "button-", "select-"). An example name could be
select-1655377908386
, ortextbox-cool-text
.
"Checkbox Group" -> use
setChecked(name)
e.g.
@name (String) Form element's name, taken from BeautifyTools' Form Builder. Has to have the type prefix (e.g. "button-", "select-")
@newData (String, Integer, Array) The GUI form element's new data (e.g. new values, new selected options)
None
const Gui = new UserGui;
Gui.window
Contains the window element. Use this variable to access the GUI's window. It will always be seperate from the site's window.
const Gui = new UserGui;
Gui.document
Contains the document element. Use this variable to access the GUI's document. It will always be seperate from the site's document.
const Gui = new UserGui;
Gui.iFrame
Contains the iFrame element. If the GUI is internal, use this variable to access the GUI's iFrame.
{
"window" : {
"title" : "No title set",
"name" : "userscript-gui",
"external" : false,
"centered" : false,
"size" : {
"width" : 300,
"height" : 500,
"dynamicSize" : true
}
},
"gui" : {
"centeredItems" : false,
"internal" : {
"darkCloseButton" : false,
"style" : `[...]`
},
"external" : {
"popup" : true,
"style" : `[...]`
}
},
"messages" : {
"blockedPopups" : () => alert(`[...]`)
}
}
const Gui = new UserGui;
Gui.settings
Contains the settings for the GUI window and the GUI itself.