From 029455cb40057109e37621517a634acdf4a8348d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 25 Nov 2019 00:28:18 +0100 Subject: [PATCH] Browser: Add a simple "view source" menu action This opens the source of the current document in TextEditor. For file:// URLs, we open the local file, but for all other protocols, a temporary file is generated, containing the document source. Longer-term we should build some kind of viewer into the Browser app instead. That would avoid silly problems like how this forgets to delete the temporary files, for instance :^) --- Applications/Browser/main.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Applications/Browser/main.cpp b/Applications/Browser/main.cpp index f9b614754e..5f3c463aa0 100644 --- a/Applications/Browser/main.cpp +++ b/Applications/Browser/main.cpp @@ -24,6 +24,7 @@ #include #include #include +#include static const char* home_url = "file:///home/anon/www/welcome.html"; @@ -135,6 +136,25 @@ int main(int argc, char** argv) RefPtr dom_tree_view; auto inspect_menu = make("Inspect"); + inspect_menu->add_action(GAction::create("View source", { Mod_Ctrl, Key_U }, [&](auto&) { + String filename_to_open; + char tmp_filename[] = "/tmp/view-source.XXXXXX"; + ASSERT(html_widget->document()); + if (html_widget->document()->url().protocol() == "file") { + filename_to_open = html_widget->document()->url().path(); + } else { + int fd = mkstemp(tmp_filename); + ASSERT(fd >= 0); + auto source = html_widget->document()->source(); + write(fd, source.characters(), source.length()); + close(fd); + filename_to_open = tmp_filename; + } + if (fork() == 0) { + execl("/bin/TextEditor", "TextEditor", filename_to_open.characters(), nullptr); + ASSERT_NOT_REACHED(); + } + })); inspect_menu->add_action(GAction::create("Inspect DOM tree", [&](auto&) { if (!dom_inspector_window) { dom_inspector_window = GWindow::construct();