Interface Browser
- All Superinterfaces:
Advisable<BrowserCallback>
,AutoCloseable
,Closeable
,Observable<BrowserEvent>
The Browser
instance itself is running in a separate native process that allocates
memory and system resources that must be released. So, when a Browser
instance is no
longer needed, it must be closed through the close()
method to release all the allocated
memory and system resources. For example:
Browser browser = engine.newBrowser(); ... browser.close();
Any attempt to use an already closed Browser
instance will lead to the
IllegalStateException
.
The Browser
instance is closed automatically when its Engine
is closed or
unexpectedly crashed. If the instance represents a popup window created by JavaScript via the
window.open()
function, then JavaScript can close the instance using the
window.close()
function.
To get notifications that the Browser
instance has been closed please subscribe to
the following event:
browser.on(BrowserClosed.class, event -> {
// The Browser instance has been closed.
});
-
Method Summary
Modifier and TypeMethodDescriptionaudio()
ReturnsAudio
that allows controlling audio on a web page loaded in the current browser instance.bitmap()
Returns aBitmap
that contains the image of the currently loaded web page.Returns an immutable list of all active capture sessions initiated by the browser.cast
(MediaReceiver receiver) Starts casting the browser content to the mediareceiver
.castScreen
(MediaReceiver receiver) Starts casting screen's content by selecting the screen in the picker dialog.castScreen
(MediaReceiver receiver, ScreenCastOptions options) Starts casting the screen's content to the mediareceiver
.void
close()
Closes the current browser instance and releases all allocated resources.void
close
(CloseOptions options) Closes the current browser instance according to the givenoptions
.Returns the default JavaScriptPresentationRequest
specified on the page.devTools()
ReturnsDevTools
that allows working with Chromium Developer Tools.void
dispatch
(KeyPressed event) Dispatches the key pressed event to the currently focused element on the loaded web page.void
dispatch
(KeyReleased event) Dispatches the key released event to the currently focused element on the loaded web page.void
Dispatches the key typed event to the currently focused element on the loaded web page.void
dispatch
(MouseDragged event) Dispatches the mouse dragged event to the currently loaded web page.void
dispatch
(MouseEntered event) Dispatches the mouse entered event to the currently loaded web page.void
dispatch
(MouseExited event) Dispatches the mouse exited event to the currently loaded web page.void
dispatch
(MouseMoved event) Dispatches the mouse moved event to the currently loaded web page.void
dispatch
(MousePressed event) Dispatches the mouse pressed event to the currently loaded web page.void
dispatch
(MouseReleased event) Dispatches the mouse released event to the currently loaded web page.void
dispatch
(MouseWheel event) Dispatches the mouse wheel event to the currently loaded web page.display()
Returns the display where the current browser instance is located.engine()
Returns the engine instance of the current browser.favicon()
Returns aBitmap
that contains the favicon of the currently loaded web page or aBitmap
that contains default Chromium favicon if the browser did not load any web pages or loaded web page URL is invalid.void
focus()
Tells the current browser instance that it has focus and must be activated.Returns anOptional
that contains the focusedFrame
on the currently loaded web page, otherwise an emptyOptional
.frames()
Returns all the frames on the currently loaded web page or an empty collection if the current browser did not load any web pages.Returns the service that controls the fullscreen mode.Returns anOptional
that contains the mainFrame
of the currently loaded web page if it exists, otherwise an emptyOptional
.ReturnsNavigation
that allows controlling navigation in the current browser instance.profile()
Returns the profile that the browser belongs to.void
replaceMisspelledWord
(String word) Replaces misspelled word under cursor on the currently loaded web page with the givenword
.void
resize
(int width, int height) Updates size of this Browser instance with the given width and height.void
Updates size of the current browser instance with the given one.boolean
saveWebPage
(Path htmlFilePath, Path resourcesDir, SavePageType saveType) Initiates the saving process of the currently loaded web page.settings()
ReturnsBrowserSettings
that allows configuring the current browser instance.size()
Returns the size of the current browser instance in the logical pixels.ReturnsTextFinder
that allows finding text on a web page loaded in the current browser instance.title()
Returns a string that represents the title of the currently loaded web page or an empty string if the browser hasn't loaded any web page yet.void
unfocus()
Tells the current browser instance that it does not have focus and must be deactivated.url()
Returns a string that represents URL of the currently loaded web page or an empty string if the browser hasn't loaded any web page yet.Returns a string that represents the user-agent of the current browser instance.void
Updates the user-agent string of the current browser instance and reloads the currently loaded web page.zoom()
ReturnsZoom
that allows zooming content of a web page loaded in the current browser instance.Methods inherited from interface com.teamdev.jxbrowser.event.Observable
on
-
Method Details
-
engine
Engine engine()Returns the engine instance of the current browser. -
profile
Profile profile()Returns the profile that the browser belongs to. -
mainFrame
Returns anOptional
that contains the mainFrame
of the currently loaded web page if it exists, otherwise an emptyOptional
.Make sure that the web page is loaded completely before accessing its main frame.
- Throws:
ObjectClosedException
- when the browser is already closed
-
focusedFrame
Returns anOptional
that contains the focusedFrame
on the currently loaded web page, otherwise an emptyOptional
.Make sure that the web page is loaded completely before accessing its focused frame.
- Throws:
ObjectClosedException
- when the browser is already closed
-
frames
Returns all the frames on the currently loaded web page or an empty collection if the current browser did not load any web pages.- Throws:
ObjectClosedException
- when the browser is already closed
-
bitmap
Bitmap bitmap()Returns aBitmap
that contains the image of the currently loaded web page.The bitmap size depends on the size of the current browser instance and the device scale factor of the display where it is located at the moment. If the current browser size is empty, then the bitmap will be empty as well.
For example, if the device scale factor of the display where the browser instance is located at the moment is 2.0 and the browser size is 100x100, then the size of the bitmap is expected to be 200x200.
- Throws:
ObjectClosedException
- when the browser is already closedBitmapTimeoutException
- when the bitmap can not be retrieved within10
seconds- Since:
- 7.1
- See Also:
-
favicon
Bitmap favicon()Returns aBitmap
that contains the favicon of the currently loaded web page or aBitmap
that contains default Chromium favicon if the browser did not load any web pages or loaded web page URL is invalid.Maximum bitmap size is 16x16. If actual favicon size is bigger it will be resized to fit this constant.
- Throws:
ObjectClosedException
- when the browser is already closed- Since:
- 7.2
-
textFinder
TextFinder textFinder()ReturnsTextFinder
that allows finding text on a web page loaded in the current browser instance. -
zoom
Zoom zoom()ReturnsZoom
that allows zooming content of a web page loaded in the current browser instance. -
captureSessions
List<CaptureSession> captureSessions()Returns an immutable list of all active capture sessions initiated by the browser.- Throws:
ObjectClosedException
- if the browser is closed- Since:
- 7.20
-
audio
Audio audio()ReturnsAudio
that allows controlling audio on a web page loaded in the current browser instance. -
settings
BrowserSettings settings()ReturnsBrowserSettings
that allows configuring the current browser instance. -
devTools
DevTools devTools()ReturnsDevTools
that allows working with Chromium Developer Tools.- Since:
- 7.1
-
fullScreen
FullScreen fullScreen()Returns the service that controls the fullscreen mode.- Since:
- 7.28
-
cast
Starts casting the browser content to the mediareceiver
.In case when the web page has the default
PresentationRequest
:const presentationRequest = new PresentationRequest(['receiver/index.html']); navigator.presentation.defaultRequest = presentationRequest;
the presentation of the media content specified in this request is started instead of casting the browser content. This implies that the
receiver
should support the presentation as a media source. Otherwise, if there is no defaultPresentationRequest
, thereceiver
should support any browser's content as a media source.Returns a new
CompletableFuture
that is completed when the cast session has been started. If the cast session has not been started, the future is completed withCastSessionStartFailedException
. If the browser is closed during the casting start, the future will be canceled.- Throws:
IllegalStateException
- if media routing is disabledObjectClosedException
- if the browser is closed- Since:
- 7.29
- See Also:
-
castScreen
Starts casting screen's content by selecting the screen in the picker dialog.Returns a new
CompletableFuture
that is completed when the cast session has been started. If the cast session has not been started, the future is completed withCastSessionStartFailedException
. If the browser is closed during the casting start, the future will be canceled.The
receiver
should support theCastMode.SCREEN
cast mode.- Throws:
IllegalStateException
- if media routing is disabledObjectClosedException
- if the browser is closed- Since:
- 7.29
- See Also:
-
castScreen
Starts casting the screen's content to the mediareceiver
.Returns a new
CompletableFuture
that is completed when the cast session has been started. If the cast session has not been started, the future is completed withCastSessionStartFailedException
. If the browser is closed during the casting start, the future will be canceled.The
receiver
should support theCastMode.SCREEN
cast mode.- Throws:
IllegalStateException
- if media routing is disabledObjectClosedException
- if the browser is closed- Since:
- 7.29
- See Also:
-
defaultPresentationRequest
Optional<PresentationRequest> defaultPresentationRequest()Returns the default JavaScriptPresentationRequest
specified on the page.Usually, the default
PresentationRequest
is specified on resources like YouTube. The developer can specify it this way:const presentationRequest = new PresentationRequest(['receiver/index.html']); navigator.presentation.defaultRequest = presentationRequest;
- Throws:
ObjectClosedException
- if the browser is closed- Since:
- 7.34
-
replaceMisspelledWord
Replaces misspelled word under cursor on the currently loaded web page with the givenword
. If there is no misspelled word under cursor, this method does nothing.- Parameters:
word
- a string that represents the word for replacement- Throws:
IllegalArgumentException
- whenword
is an empty or blank string
-
saveWebPage
Initiates the saving process of the currently loaded web page. The web page can be saved as a single HTML file or the file with resources. Before saving a web page make sure that it is not being loaded. It is recommended to completely loaded the web page and only then save it.- Parameters:
htmlFilePath
- an absolute path to a file in which the web page will be savedresourcesDir
- an absolute path to a directory in which the resources (e.g. images, css) of the web page will be saved. If the directory does not exist, it will be createdsaveType
- determines how the web page will be saved: as an HTML file with all the required resources (e.g. images, css etc.), a single HTML or MHTML file- Returns:
true
if the saving process has been initialized successfully- Throws:
ObjectClosedException
- when the browser is already closed
-
url
String url()Returns a string that represents URL of the currently loaded web page or an empty string if the browser hasn't loaded any web page yet.- Throws:
ObjectClosedException
- when the browser is already closed
-
title
String title()Returns a string that represents the title of the currently loaded web page or an empty string if the browser hasn't loaded any web page yet.- Throws:
ObjectClosedException
- when the browser is already closed
-
userAgent
String userAgent()Returns a string that represents the user-agent of the current browser instance.The user-agent string can be overridden for each browser instance via
userAgent(String)
. If the string has not been overridden, then this method returns the default user-agent string obtained throughNetwork.userAgent()
.- Throws:
ObjectClosedException
- when the browser is already closed- See Also:
-
userAgent
Updates the user-agent string of the current browser instance and reloads the currently loaded web page.- Parameters:
userAgent
- a new user-agent string- Throws:
ObjectClosedException
- when the browser is already closedIllegalArgumentException
- whenuserAgent
is empty or blank
-
focus
void focus()Tells the current browser instance that it has focus and must be activated.- Throws:
ObjectClosedException
- when the browser is already closed
-
unfocus
void unfocus()Tells the current browser instance that it does not have focus and must be deactivated.- Throws:
ObjectClosedException
- when the browser is already closed
-
resize
Updates size of the current browser instance with the given one. By default, the size of the browser is empty. Many web pages rely on the browser size and require that it is not empty. The DOM document of a web page might not be loaded and displayed at all, because there is no sense in loading and rendering DOM document when it is empty.Use this method when you do not need to display content of the loaded web page, but the web page must "think" it has been loaded in a browser instance with a non-empty size.
- Parameters:
size
- a new size in the logical pixels- Throws:
IllegalArgumentException
- whensize
is emptyObjectClosedException
- when the browser is already closed
-
resize
void resize(int width, int height) Updates size of this Browser instance with the given width and height. By default, Browser's size is empty. Many web pages rely on the Browser's size and require that it is not empty. DOM document of a web page might not be loaded and displayed at all, because there is no sense in loading and rendering DOM document when it is empty.Use this method when you do not need to display content of the loaded web page, but the web page must "think" it has been loaded in a Browser instance with non-empty size.
- Parameters:
width
- the specified width in the logical pixelsheight
- the specified height in the logical pixels- Throws:
IllegalArgumentException
- whenwidth
orheight
is not positiveObjectClosedException
- when this Browser is already closed
-
size
Size size()Returns the size of the current browser instance in the logical pixels. By default, the size of the browser is empty.- Throws:
ObjectClosedException
- when the browser is already closed- Since:
- 7.1
-
display
Display display()Returns the display where the current browser instance is located. By default, it is a primary display.- Throws:
ObjectClosedException
- when the browser is already closed- Since:
- 7.1
-
dispatch
Dispatches the key pressed event to the currently focused element on the loaded web page. It does nothing if the browser instance hasn't loaded any web page.- Parameters:
event
- the key pressed event- Throws:
ObjectClosedException
- when the browser is already closed
-
dispatch
Dispatches the key released event to the currently focused element on the loaded web page. It does nothing if the browser instance hasn't loaded any web page.- Parameters:
event
- the key released event- Throws:
ObjectClosedException
- when the browser is already closed
-
dispatch
Dispatches the key typed event to the currently focused element on the loaded web page. It does nothing if the browser instance hasn't loaded any web page.- Parameters:
event
- the key typed event- Throws:
ObjectClosedException
- when the browser is already closed
-
dispatch
Dispatches the mouse pressed event to the currently loaded web page. It does nothing if the browser instance hasn't loaded any web page.- Parameters:
event
- the mouse pressed event- Throws:
ObjectClosedException
- when the browser is already closed
-
dispatch
Dispatches the mouse released event to the currently loaded web page. It does nothing if the browser instance hasn't loaded any web page.- Parameters:
event
- the mouse released event- Throws:
ObjectClosedException
- when the browser is already closed
-
dispatch
Dispatches the mouse entered event to the currently loaded web page. It does nothing if the browser instance hasn't loaded any web page.- Parameters:
event
- the mouse entered event- Throws:
ObjectClosedException
- when the browser is already closed
-
dispatch
Dispatches the mouse exited event to the currently loaded web page. It does nothing if the browser instance hasn't loaded any web page.- Parameters:
event
- the mouse exited event- Throws:
ObjectClosedException
- when the browser is already closed
-
dispatch
Dispatches the mouse dragged event to the currently loaded web page. It does nothing if the browser instance hasn't loaded any web page.- Parameters:
event
- the mouse dragged event- Throws:
ObjectClosedException
- when the browser is already closed
-
dispatch
Dispatches the mouse moved event to the currently loaded web page. It does nothing if the browser instance hasn't loaded any web page.- Parameters:
event
- the mouse moved event- Throws:
ObjectClosedException
- when the browser is already closed
-
dispatch
Dispatches the mouse wheel event to the currently loaded web page. It does nothing if the browser instance hasn't loaded any web page.- Parameters:
event
- the mouse wheel event- Throws:
ObjectClosedException
- when the browser is already closed
-
close
void close()Closes the current browser instance and releases all allocated resources.The
unload
andbeforeunload
JavaScript events will not be fired in this case. This method is equivalent ofclose(CloseOptions.newBuilder().build())
.If the browser is already closed, then this method does nothing.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- See Also:
-
close
Closes the current browser instance according to the givenoptions
.If the currently loaded web page registers the
onbeforeunload
JavaScript event and the given options tell the browser to fire thebeforeunload
event, then this method will not close the browser right away.The
BeforeUnloadCallback
will be invoked in this case and if the callback tells the browser to stay on the web page, then the browser will not be closed.The following example demonstrates how to close the browser instance as if a user manually clicking the close button of the tab/window.
browser.close(CloseOptions.newBuilder() .fireBeforeUnload() .build());
If the browser is already closed, then this method does nothing.
- Since:
- 7.6
-