:right-sidebar: True UserContentManager =================================================================== .. currentmodule:: gi.repository.WebKit .. versionadded:: 2.6 .. class:: UserContentManager(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.GObject.Object` Manages user-defined content which affects web pages. Using a :obj:`~gi.repository.WebKit.UserContentManager` user CSS style sheets can be set to be injected in the web pages loaded by a :obj:`~gi.repository.WebKit.WebView`\, by :func:`~gi.repository.WebKit.UserContentManager.add_style_sheet`. To use a :obj:`~gi.repository.WebKit.UserContentManager`\, it must be created using :func:`~gi.repository.WebKit.UserContentManager.new`, and then used to construct a :obj:`~gi.repository.WebKit.WebView`\. User style sheets can be created with :func:`~gi.repository.WebKit.UserStyleSheet.new`. User style sheets can be added and removed at any time, but they will affect the web pages loaded afterwards. Constructors ------------ .. rst-class:: interim-class .. class:: UserContentManager :no-index: .. classmethod:: new() -> ~gi.repository.WebKit.UserContentManager Creates a new user content manager. .. versionadded:: 2.6 Methods ------- .. rst-class:: interim-class .. class:: UserContentManager :no-index: .. method:: add_filter(filter: ~gi.repository.WebKit.UserContentFilter) -> None Adds a :obj:`~gi.repository.WebKit.UserContentFilter` to the given :obj:`~gi.repository.WebKit.UserContentManager`\. The same :obj:`~gi.repository.WebKit.UserContentFilter` can be reused with multiple :obj:`~gi.repository.WebKit.UserContentManager` instances. Filters need to be saved and loaded from :obj:`~gi.repository.WebKit.UserContentFilterStore`\. .. versionadded:: 2.24 :param filter: A :obj:`~gi.repository.WebKit.UserContentFilter` .. method:: add_script(script: ~gi.repository.WebKit.UserScript) -> None Adds a :obj:`~gi.repository.WebKit.UserScript` to the given :obj:`~gi.repository.WebKit.UserContentManager`\. The same :obj:`~gi.repository.WebKit.UserScript` can be reused with multiple :obj:`~gi.repository.WebKit.UserContentManager` instances. .. versionadded:: 2.6 :param script: A :obj:`~gi.repository.WebKit.UserScript` .. method:: add_style_sheet(stylesheet: ~gi.repository.WebKit.UserStyleSheet) -> None Adds a :obj:`~gi.repository.WebKit.UserStyleSheet` to the given :obj:`~gi.repository.WebKit.UserContentManager`\. The same :obj:`~gi.repository.WebKit.UserStyleSheet` can be reused with multiple :obj:`~gi.repository.WebKit.UserContentManager` instances. .. versionadded:: 2.6 :param stylesheet: A :obj:`~gi.repository.WebKit.UserStyleSheet` .. method:: register_script_message_handler(name: str, world_name: str | None = None) -> bool Registers a new user script message handler in script world. After it is registered, scripts can use ``window.webkit.messageHandlers..postMessage(value)`` to send messages. Those messages are received by connecting handlers to the :obj:`~gi.repository.WebKit.UserContentManager`\::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: .. code-block:: c :dedent: 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"); Registering a script message handler will fail if the requested name has been already registered before. If :const:`None` is passed as the ``world_name``\, the default world will be used. The registered handler can be unregistered by using :func:`~gi.repository.WebKit.UserContentManager.unregister_script_message_handler`. .. versionadded:: 2.40 :param name: Name of the script message channel :param world_name: the name of a ``WebKitScriptWorld`` .. method:: register_script_message_handler_with_reply(name: str, world_name: str | None = None) -> bool Registers a new user script message handler in script world with name ``world_name``\. Different from :func:`~gi.repository.WebKit.UserContentManager.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 :const:`None` is passed as the ``world_name``\, the default world will be used. See :func:`~gi.repository.WebKit.UserContentManager.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 :func:`~gi.repository.WebKit.UserContentManager.unregister_script_message_handler`. .. versionadded:: 2.40 :param name: Name of the script message channel :param world_name: the name of a ``WebKitScriptWorld`` .. method:: remove_all_filters() -> None Removes all content filters from the given :obj:`~gi.repository.WebKit.UserContentManager`\. .. versionadded:: 2.24 .. method:: remove_all_scripts() -> None Removes all user scripts from the given :obj:`~gi.repository.WebKit.UserContentManager` See also :func:`~gi.repository.WebKit.UserContentManager.remove_script`. .. versionadded:: 2.6 .. method:: remove_all_style_sheets() -> None Removes all user style sheets from the given :obj:`~gi.repository.WebKit.UserContentManager`\. .. versionadded:: 2.6 .. method:: remove_filter(filter: ~gi.repository.WebKit.UserContentFilter) -> None Removes a filter from the given :obj:`~gi.repository.WebKit.UserContentManager`\. Since 2.24 :param filter: A :obj:`~gi.repository.WebKit.UserContentFilter` .. method:: remove_filter_by_id(filter_id: str) -> None Removes a filter by the given identifier. Removes a filter from the given :obj:`~gi.repository.WebKit.UserContentManager` given the identifier of a :obj:`~gi.repository.WebKit.UserContentFilter` as returned by :func:`~gi.repository.WebKit.UserContentFilter.get_identifier`. .. versionadded:: 2.26 :param filter_id: Filter identifier .. method:: remove_script(script: ~gi.repository.WebKit.UserScript) -> None Removes a :obj:`~gi.repository.WebKit.UserScript` from the given :obj:`~gi.repository.WebKit.UserContentManager`\. See also :func:`~gi.repository.WebKit.UserContentManager.remove_all_scripts`. .. versionadded:: 2.32 :param script: A :obj:`~gi.repository.WebKit.UserScript` .. method:: remove_style_sheet(stylesheet: ~gi.repository.WebKit.UserStyleSheet) -> None Removes a :obj:`~gi.repository.WebKit.UserStyleSheet` from the given :obj:`~gi.repository.WebKit.UserContentManager`\. See also :func:`~gi.repository.WebKit.UserContentManager.remove_all_style_sheets`. .. versionadded:: 2.32 :param stylesheet: A :obj:`~gi.repository.WebKit.UserStyleSheet` .. method:: unregister_script_message_handler(name: str, world_name: str | None = None) -> None Unregisters a previously registered message handler in script world with name ``world_name``\. If :const:`None` is passed as the ``world_name``\, the default world will be used. Note that this does *not* disconnect handlers for the :obj:`~gi.repository.WebKit.UserContentManager`\::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 :func:`~gi.repository.WebKit.UserContentManager.register_script_message_handler`. .. versionadded:: 2.40 :param name: Name of the script message channel :param world_name: the name of a ``WebKitScriptWorld`` Signals ------- .. rst-class:: interim-class .. class:: UserContentManager.signals :no-index: .. method:: script_message_received(value: ~gi.repository.JavaScriptCore.Value) -> None The type of the None singleton. .. versionadded:: 2.8 :param value: the value received from the JavaScript world. .. method:: script_message_with_reply_received(value: ~gi.repository.JavaScriptCore.Value, reply: ~gi.repository.WebKit.ScriptMessageReply) -> bool The type of the None singleton. .. versionadded:: 2.40 :param value: the value received from the JavaScript world. :param reply: the :obj:`~gi.repository.WebKit.ScriptMessageReply` to send the reply to the script message.