Class UserContentManager

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public final class UserContentManager extends GObject

Manages user-defined content which affects web pages.

Using a WebKitUserContentManager user CSS style sheets can be set to be injected in the web pages loaded by a WebKitWebView, by webkit_user_content_manager_add_style_sheet().

To use a WebKitUserContentManager, it must be created using webkit_user_content_manager_new(), and then used to construct a WebKitWebView. User style sheets can be created with webkit_user_style_sheet_new().

User style sheets can be added and removed at any time, but they will affect the web pages loaded afterwards.

Since:
2.6
  • Constructor Details

    • UserContentManager

      public UserContentManager(MemorySegment address)
      Create a UserContentManager instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • UserContentManager

      public UserContentManager()
      Create a new UserContentManager.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the UserContentManager class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected UserContentManager asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • addFilter

      public void addFilter(UserContentFilter filter)

      Adds a WebKitUserContentFilter to the given WebKitUserContentManager.

      The same WebKitUserContentFilter can be reused with multiple WebKitUserContentManager instances.

      Filters need to be saved and loaded from WebKitUserContentFilterStore.

      Parameters:
      filter - A WebKitUserContentFilter
      Since:
      2.24
    • addScript

      public void addScript(UserScript script)

      Adds a WebKitUserScript to the given WebKitUserContentManager.

      The same WebKitUserScript can be reused with multiple WebKitUserContentManager instances.

      Parameters:
      script - A WebKitUserScript
      Since:
      2.6
    • addStyleSheet

      public void addStyleSheet(UserStyleSheet stylesheet)

      Adds a WebKitUserStyleSheet to the given WebKitUserContentManager.

      The same WebKitUserStyleSheet can be reused with multiple WebKitUserContentManager instances.

      Parameters:
      stylesheet - A WebKitUserStyleSheet
      Since:
      2.6
    • registerScriptMessageHandler

      public boolean registerScriptMessageHandler(String name, @Nullable String worldName)

      Registers a new user script message handler in script world.

      After it is registered, scripts can use window.webkit.messageHandlers.<name>.postMessage(value) to send messages. Those messages are received by connecting handlers to the WebKitUserContentManager::script-message-received signal. The handler name is used as the detail of the signal. To avoid race conditions between registering the handler name, and starting to receive the signals, it is recommended to connect to the signal before registering the handler name:

      WebKitWebView *view = webkit_web_view_new ();
      WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager ();
      g_signal_connect (manager, "script-message-received::foobar",
                        G_CALLBACK (handle_script_message), NULL);
      webkit_user_content_manager_register_script_message_handler (manager, "foobar", "world");
      

      Registering a script message handler will fail if the requested name has been already registered before.

      If null is passed as the worldName, the default world will be used.

      The registered handler can be unregistered by using webkit_user_content_manager_unregister_script_message_handler().

      Parameters:
      name - Name of the script message channel
      worldName - the name of a WebKitScriptWorld
      Returns:
      true if message handler was registered successfully, or false otherwise.
      Since:
      2.40
    • registerScriptMessageHandlerWithReply

      public boolean registerScriptMessageHandlerWithReply(String name, @Nullable String worldName)

      Registers a new user script message handler in script world with name worldName.

      Different from webkit_user_content_manager_register_script_message_handler(), when using this function to register the handler, the connected signal is script-message-with-reply-received, and a reply provided by the user is expected. Otherwise, the user will receive a default undefined value.

      If null is passed as the worldName, the default world will be used. See webkit_user_content_manager_register_script_message_handler() for full description.

      Registering a script message handler will fail if the requested name has been already registered before.

      The registered handler can be unregistered by using webkit_user_content_manager_unregister_script_message_handler().

      Parameters:
      name - Name of the script message channel
      worldName - the name of a WebKitScriptWorld
      Returns:
      true if message handler was registered successfully, or false otherwise.
      Since:
      2.40
    • removeAllFilters

      public void removeAllFilters()
      Removes all content filters from the given WebKitUserContentManager.
      Since:
      2.24
    • removeAllScripts

      public void removeAllScripts()

      Removes all user scripts from the given WebKitUserContentManager

      See also webkit_user_content_manager_remove_script().

      Since:
      2.6
    • removeAllStyleSheets

      public void removeAllStyleSheets()
      Removes all user style sheets from the given WebKitUserContentManager.
      Since:
      2.6
    • removeFilter

      public void removeFilter(UserContentFilter filter)

      Removes a filter from the given WebKitUserContentManager.

      Since 2.24

      Parameters:
      filter - A WebKitUserContentFilter
    • removeFilterById

      public void removeFilterById(String filterId)

      Removes a filter by the given identifier.

      Removes a filter from the given WebKitUserContentManager given the identifier of a WebKitUserContentFilter as returned by webkit_user_content_filter_get_identifier().

      Parameters:
      filterId - Filter identifier
      Since:
      2.26
    • removeScript

      public void removeScript(UserScript script)

      Removes a WebKitUserScript from the given WebKitUserContentManager.

      See also webkit_user_content_manager_remove_all_scripts().

      Parameters:
      script - A WebKitUserScript
      Since:
      2.32
    • removeStyleSheet

      public void removeStyleSheet(UserStyleSheet stylesheet)

      Removes a WebKitUserStyleSheet from the given WebKitUserContentManager.

      See also webkit_user_content_manager_remove_all_style_sheets().

      Parameters:
      stylesheet - A WebKitUserStyleSheet
      Since:
      2.32
    • unregisterScriptMessageHandler

      public void unregisterScriptMessageHandler(String name, @Nullable String worldName)

      Unregisters a previously registered message handler in script world with name worldName. If null is passed as the worldName, the default world will be used.

      Note that this does not disconnect handlers for the WebKitUserContentManager::script-message-received signal; they will be kept connected, but the signal will not be emitted unless the handler name is registered again.

      See also webkit_user_content_manager_register_script_message_handler().

      Parameters:
      name - Name of the script message channel
      worldName - the name of a WebKitScriptWorld
      Since:
      2.40
    • onScriptMessageReceived

      This signal is emitted when JavaScript in a web view calls window.webkit.messageHandlers..postMessage(), after registering using webkit_user_content_manager_register_script_message_handler()
      Parameters:
      detail - the signal detail
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      2.8
      See Also:
    • emitScriptMessageReceived

      public void emitScriptMessageReceived(@Nullable String detail, @Nullable Value value)
    • onScriptMessageWithReplyReceived

      This signal is emitted when JavaScript in a web view calls window.webkit.messageHandlers..postMessage(), after registering using webkit_user_content_manager_register_script_message_handler_with_reply()

      The given reply can be used to send a return value with webkit_script_message_reply_return_value() or an error message with webkit_script_message_reply_return_error_message(). If none of them are called, an automatic reply with an undefined value will be sent.

      It is possible to handle the reply asynchronously, by simply calling g_object_ref() on the reply and returning true.

      Parameters:
      detail - the signal detail
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      2.40
      See Also:
    • emitScriptMessageWithReplyReceived

      public boolean emitScriptMessageWithReplyReceived(@Nullable String detail, @Nullable Value value, @Nullable ScriptMessageReply reply)
    • builder

      public static UserContentManager.Builder<? extends UserContentManager.Builder> builder()
      A UserContentManager.Builder object constructs a UserContentManager with the specified properties. Use the various set...() methods to set properties, and finish construction with UserContentManager.Builder.build().
      Returns:
      the builder object