Skip to content

Commit

Permalink
Get HTML content #34
Browse files Browse the repository at this point in the history
  • Loading branch information
Lecrapouille committed Apr 21, 2024
1 parent 151acea commit c9fca57
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 16 deletions.
61 changes: 45 additions & 16 deletions addons/gdcef/demos/2D/CEF.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,54 @@

extends Control

# Default pages
# URL
const DEFAULT_PAGE = "user://default_page.html"
const RADIO_PAGE = "http://streaming.radio.co/s9378c22ee/listen"
# "https://www.programmes-radio.com/fr/stream-e8BxeoRhsz9jY9mXXRiFTE/ecouter-KPJK"
const SAVED_PAGE = "user://saved_page.html"
const HOME_PAGE = "https://github.com/Lecrapouille/gdcef"
const RADIO_PAGE = "http://streaming.radio.co/s9378c22ee/listen"
#const RADIO_PAGE = "https://www.programmes-radio.com/fr/stream-e8BxeoRhsz9jY9mXXRiFTE/ecouter-KPJK"

# The current browser as Godot node
@onready var current_browser = null

# Memorize if the mouse was pressed
@onready var mouse_pressed : bool = false

@onready var playback = null
# ==============================================================================
# Create the home page.
# ==============================================================================
func create_default_page():
var file = FileAccess.open(DEFAULT_PAGE, FileAccess.WRITE)
file.store_string("<html><body bgcolor=\"white\"><h2>Welcome to gdCEF !</h2><p>This a generated page.</p></body></html>")
file.close()
pass

# ==============================================================================
# Save page as html.
# ==============================================================================
func _on_saving_page(html, brower):
var path = ProjectSettings.globalize_path(SAVED_PAGE)
var file = FileAccess.open(SAVED_PAGE, FileAccess.WRITE)
if (file != null):
file.store_string(html)
file.close()
$AcceptDialog.title = brower.get_url()
$AcceptDialog.dialog_text = "Page saved at:\n" + path
else:
$AcceptDialog.title = "Alert!"
$AcceptDialog.dialog_text = "Failed creating the file " + path
$AcceptDialog.popup_centered(Vector2(0,0))
$AcceptDialog.show()
pass

# ==============================================================================
# Callback when a page has ended to load with success (200): we print a message
# ==============================================================================
func _on_page_loaded(node):
func _on_page_loaded(brower):
var L = $Panel/VBox/HBox/BrowserList
var url = node.get_url()
var url = brower.get_url()
L.set_item_text(L.get_selected_id(), url)
$Panel/VBox/HBox2/Info.set_text(url + " loaded as ID " + node.name)
print("Browser named '" + node.name + "' inserted on list at index " + str(L.get_selected_id()) + ": " + url)
$Panel/VBox/HBox2/Info.set_text(url + " loaded as ID " + brower.name)
print("Browser named '" + brower.name + "' inserted on list at index " + str(L.get_selected_id()) + ": " + url)
pass

# ==============================================================================
Expand Down Expand Up @@ -69,6 +94,7 @@ func create_browser(url):
return null

# Loading callbacks
browser.connect("on_html_content_requested", _on_saving_page)
browser.connect("on_page_loaded", _on_page_loaded)
browser.connect("on_page_failed_loading", _on_page_failed_loading)

Expand Down Expand Up @@ -242,9 +268,15 @@ func _input(event):
if current_browser == null:
return
if event is InputEventKey:
current_browser.set_key_pressed(
event.unicode if event.unicode != 0 else event.keycode, # Godot3: event.scancode,
event.pressed, event.shift_pressed, event.alt_pressed, event.is_command_or_control_pressed())
if event.is_command_or_control_pressed() && event.pressed && not event.echo:
if event.keycode == KEY_S:
# Will call the callback 'on_html_content_requested'
current_browser.request_html_content()
else:
current_browser.set_key_pressed(
event.unicode if event.unicode != 0 else event.keycode,
event.pressed, event.shift_pressed, event.alt_pressed,
event.is_command_or_control_pressed())
pass

# ==============================================================================
Expand All @@ -264,10 +296,7 @@ func _on_texture_rect_resized():
# Create a single briwser named "current_browser" that is attached as child node to $CEF.
# ==============================================================================
func _ready():
# Create the home page
var file = FileAccess.open(DEFAULT_PAGE, FileAccess.WRITE)
file.store_string("<html><body bgcolor=\"white\"><h2>Welcome to gdCEF !</h2><p>This a generated page.</p></body></html>")
file = null
create_default_page()

# See API.md for more details. CEF Configuration is:
# resource_path := {"artifacts", CEF_ARTIFACTS_FOLDER}
Expand Down
3 changes: 3 additions & 0 deletions addons/gdcef/demos/2D/CEF.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ size_flags_horizontal = 3
mouse_filter = 0

[node name="ColorPopup" type="Popup" parent="."]
size = Vector2i(298, 471)

[node name="ColorPicker" type="ColorPicker" parent="ColorPopup"]
anchors_preset = 15
Expand All @@ -133,6 +134,8 @@ presets_visible = false

[node name="AudioStreamPlayer2D" type="AudioStreamPlayer2D" parent="."]

[node name="AcceptDialog" type="AcceptDialog" parent="."]

[connection signal="pressed" from="Panel/VBox/HBox/New" to="." method="_on_Add_pressed"]
[connection signal="pressed" from="Panel/VBox/HBox/Home" to="." method="_on_Home_pressed"]
[connection signal="pressed" from="Panel/VBox/HBox/Go" to="." method="_on_go_pressed"]
Expand Down
37 changes: 37 additions & 0 deletions addons/gdcef/gdcef/src/gdbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@
# include <omp.h>
#endif

//------------------------------------------------------------------------------
// Visit the html content of the current page.
class Visitor : public CefStringVisitor
{
public:
Visitor(GDBrowserView& node)
: m_node(node)
{}

virtual void Visit(const CefString& string) override
{
godot::String html = string.ToString().c_str();

m_node.emit_signal("on_html_content_requested", html, &m_node);
}

private:

GDBrowserView& m_node;
IMPLEMENT_REFCOUNTING(Visitor);
};

//------------------------------------------------------------------------------
// in a GDNative module, "_bind_methods" is replaced by the "_register_methods"
// method CefRefPtr<CefBrowser> m_browser; this is used to expose various methods
Expand All @@ -60,6 +82,7 @@ void GDBrowserView::_bind_methods()
ClassDB::bind_method(D_METHOD("is_loaded"), &GDBrowserView::loaded);
ClassDB::bind_method(D_METHOD("reload"), &GDBrowserView::reload);
ClassDB::bind_method(D_METHOD("stop_loading"), &GDBrowserView::stopLoading);
ClassDB::bind_method(D_METHOD("request_html_content"), &GDBrowserView::requestHtmlContent);
ClassDB::bind_method(D_METHOD("execute_javascript"), &GDBrowserView::executeJavaScript);
ClassDB::bind_method(D_METHOD("has_previous_page"), &GDBrowserView::canNavigateBackward);
ClassDB::bind_method(D_METHOD("has_next_page"), &GDBrowserView::canNavigateForward);
Expand Down Expand Up @@ -91,6 +114,8 @@ void GDBrowserView::_bind_methods()
ADD_SIGNAL(MethodInfo("on_page_failed_loading", PropertyInfo(Variant::BOOL, "aborted"),
PropertyInfo(Variant::STRING, "err_msg"), PropertyInfo(Variant::OBJECT, "node")));
ADD_SIGNAL(MethodInfo("on_browser_paint", PropertyInfo(Variant::OBJECT, "node")));
ADD_SIGNAL(MethodInfo("on_html_content_requested", PropertyInfo(Variant::STRING, "html"),
PropertyInfo(Variant::OBJECT, "node")));
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -358,6 +383,18 @@ void GDBrowserView::stopLoading()
m_browser->StopLoad();
}

//------------------------------------------------------------------------------
void GDBrowserView::requestHtmlContent()
{
CefRefPtr<Visitor> visitor = new Visitor(*this);
if (m_browser && m_browser->GetMainFrame())
{
m_browser->GetMainFrame()->GetSource(visitor);
}

BROWSER_ERROR("Not possible to retrieving text");
}

//------------------------------------------------------------------------------
void GDBrowserView::executeJavaScript(godot::String javascript)
{
Expand Down
6 changes: 6 additions & 0 deletions addons/gdcef/gdcef/src/gdbrowser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ class GDBrowserView : public godot::Node
// -------------------------------------------------------------------------
bool reload() const;

// -------------------------------------------------------------------------
//! \brief Request the HTML content of the page. The result is given by the
//! Godot callback
// -------------------------------------------------------------------------
void requestHtmlContent();

// -------------------------------------------------------------------------
//! \brief Exported method to Godot script. Execute Execute a string of
// JavaScript code in this browser.
Expand Down

0 comments on commit c9fca57

Please sign in to comment.