Skip to content

Commit

Permalink
Merge pull request #255 from Bioblaze/web-edits
Browse files Browse the repository at this point in the history
Adding Support for Discord Embed Headers, Social Headers, and Web Headers
  • Loading branch information
Bioblaze authored Jan 20, 2025
2 parents 2f84622 + 29328e3 commit 3410793
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 1 deletion.
2 changes: 2 additions & 0 deletions misc/dist/html/full-size.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0">
$BLAZIUM_DISCORD_EMBEDDED_HEADERS
$BLAZIUM_HEADER_EMBEDS
<title>$GODOT_PROJECT_NAME</title>
<style>
input[type="color"],
Expand Down
48 changes: 48 additions & 0 deletions platform/web/doc_classes/EditorExportPlatformWeb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,54 @@
<link title="Web documentation index">$DOCS_URL/tutorials/platform/web/index.html</link>
</tutorials>
<members>
<member name="blazium/discord_embed/autodetect" type="bool" setter="" getter="">
Autodetect and patch the url to work with discord if running on discord domain.
</member>
<member name="blazium/discord_embed/enabled" type="bool" setter="" getter="">
Enable if you want to use discord embedded app feature.
</member>
<member name="blazium/social_headers/card" type="String" setter="" getter="">
The card meta property of the web page.
</member>
<member name="blazium/social_headers/description" type="String" setter="" getter="">
The description meta property of the web page.
</member>
<member name="blazium/social_headers/enabled" type="bool" setter="" getter="">
Enable if you want to use social headers.
</member>
<member name="blazium/social_headers/image" type="String" setter="" getter="">
The image meta property of the web page.
</member>
<member name="blazium/social_headers/site" type="String" setter="" getter="">
The site meta property of the web page.
</member>
<member name="blazium/social_headers/title" type="String" setter="" getter="">
The title meta property of the web page.
</member>
<member name="blazium/social_headers/url" type="String" setter="" getter="">
The url meta property of the web page.
</member>
<member name="blazium/web_headers/description" type="String" setter="" getter="">
The description open graph property of the web page.
</member>
<member name="blazium/web_headers/enabled" type="bool" setter="" getter="">
Enable if you want to use web headers.
</member>
<member name="blazium/web_headers/image" type="String" setter="" getter="">
The image open graph property of the web page.
</member>
<member name="blazium/web_headers/site_name" type="String" setter="" getter="">
The site name open graph property of the web page.
</member>
<member name="blazium/web_headers/title" type="String" setter="" getter="">
The title open graph property of the web page.
</member>
<member name="blazium/web_headers/type" type="String" setter="" getter="">
The type open graph property of the web page.
</member>
<member name="blazium/web_headers/url" type="String" setter="" getter="">
The url open graph property of the web page.
</member>
<member name="custom_template/debug" type="String" setter="" getter="">
File path to the custom export template used for debug builds. If left empty, the default template is used.
</member>
Expand Down
84 changes: 84 additions & 0 deletions platform/web/export/export_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,63 @@ void EditorExportPlatformWeb::_fix_html(Vector<uint8_t> &p_html, const Ref<Edito
head_include += "<link rel=\"manifest\" href=\"" + p_name + ".manifest.json\">\n";
config["serviceWorker"] = p_name + ".service.worker.js";
}
String discord_head_include;
if (p_preset->get("blazium/discord_embed/enabled")) {
discord_head_include += "<meta name=\"discord_embed\" content=\"true\"/>\n";
if (p_preset->get("blazium/discord_embed/autodetect")) {
discord_head_include += "<meta name=\"discord_autodetect\" content=\"true\"/>\n";
}
}

String blazium_header_embeds;
if (p_preset->get("blazium/web_headers/enabled")) {
if (p_preset->has("blazium/web_headers/title")) {
blazium_header_embeds += "<meta property=\"og:title\" content=\"" + String(p_preset->get("blazium/web_headers/title")) + "\"/>\n";
}
if (p_preset->has("blazium/web_headers/description")) {
blazium_header_embeds += "<meta property=\"og:description\" content=\"" + String(p_preset->get("blazium/web_headers/description")) + "\"/>\n";
}
if (p_preset->has("blazium/web_headers/url")) {
blazium_header_embeds += "<meta property=\"og:url\" content=\"" + String(p_preset->get("blazium/web_headers/url")) + "\"/>\n";
}
if (p_preset->has("blazium/web_headers/image")) {
blazium_header_embeds += "<meta property=\"og:image\" content=\"" + String(p_preset->get("blazium/web_headers/image")) + "\"/>\n";
}
if (p_preset->has("blazium/web_headers/type")) {
blazium_header_embeds += "<meta property=\"og:type\" content=\"" + String(p_preset->get("blazium/web_headers/type")) + "\"/>\n";
}
if (p_preset->has("blazium/web_headers/site_name")) {
blazium_header_embeds += "<meta property=\"og:site_name\" content=\"" + String(p_preset->get("blazium/web_headers/site_name")) + "\"/>\n";
}
}
String social_headers;
if (p_preset->get("blazium/social_headers/enabled")) {
if (p_preset->has("blazium/social_headers/title")) {
blazium_header_embeds += "<meta property=\"twitter:title\" content=\"" + String(p_preset->get("blazium/social_headers/title")) + "\"/>\n";
}
if (p_preset->has("blazium/social_headers/description")) {
blazium_header_embeds += "<meta property=\"twitter:description\" content=\"" + String(p_preset->get("blazium/social_headers/description")) + "\"/>\n";
}
if (p_preset->has("blazium/social_headers/url")) {
blazium_header_embeds += "<meta property=\"twitter:url\" content=\"" + String(p_preset->get("blazium/social_headers/url")) + "\"/>\n";
}
if (p_preset->has("blazium/social_headers/image")) {
blazium_header_embeds += "<meta property=\"twitter:image\" content=\"" + String(p_preset->get("blazium/social_headers/image")) + "\"/>\n";
}
if (p_preset->has("blazium/social_headers/site")) {
blazium_header_embeds += "<meta property=\"twitter:site\" content=\"" + String(p_preset->get("blazium/social_headers/site")) + "\"/>\n";
}
if (p_preset->has("blazium/social_headers/card")) {
blazium_header_embeds += "<meta property=\"twitter:card\" content=\"" + String(p_preset->get("blazium/social_headers/card")) + "\"/>\n";
}
}

// Replaces HTML string
const String str_config = Variant(config).to_json_string();
const String custom_head_include = p_preset->get("html/head_include");
HashMap<String, String> replaces;
replaces["$BLAZIUM_DISCORD_EMBEDDED_HEADERS"] = discord_head_include;
replaces["$BLAZIUM_HEADER_EMBEDS"] = blazium_header_embeds;
replaces["$GODOT_URL"] = p_name + ".js";
replaces["$GODOT_PROJECT_NAME"] = GLOBAL_GET("application/config/name");
replaces["$GODOT_HEAD_INCLUDE"] = head_include + custom_head_include;
Expand Down Expand Up @@ -370,6 +422,38 @@ void EditorExportPlatformWeb::get_export_options(List<ExportOption> *r_options)
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/icon_180x180", PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/icon_512x512", PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "progressive_web_app/background_color", PROPERTY_HINT_COLOR_NO_ALPHA), Color()));

r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "blazium/web_headers/enabled"), false, true));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/web_headers/title", PROPERTY_HINT_PLACEHOLDER_TEXT, "Web Title"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/web_headers/description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Web Description"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/web_headers/url", PROPERTY_HINT_PLACEHOLDER_TEXT, "Web URL"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/web_headers/image", PROPERTY_HINT_PLACEHOLDER_TEXT, "Image URL"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/web_headers/type", PROPERTY_HINT_PLACEHOLDER_TEXT, "Web Type"), "website"));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/web_headers/site_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Site Name"), ""));

r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "blazium/social_headers/enabled"), false, true));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/social_headers/title", PROPERTY_HINT_PLACEHOLDER_TEXT, "Social Title"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/social_headers/description", PROPERTY_HINT_PLACEHOLDER_TEXT, "Social Description"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/social_headers/url", PROPERTY_HINT_PLACEHOLDER_TEXT, "Social URL"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/social_headers/image", PROPERTY_HINT_PLACEHOLDER_TEXT, "Social Image URL"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/social_headers/site", PROPERTY_HINT_PLACEHOLDER_TEXT, "Social Site"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "blazium/social_headers/card", PROPERTY_HINT_PLACEHOLDER_TEXT, "Site Card"), ""));

r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "blazium/discord_embed/enabled"), false, true));
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "blazium/discord_embed/autodetect"), false));
}

bool EditorExportPlatformWeb::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const {
if (p_option.begins_with("blazium/web_headers") && p_option != "blazium/web_headers/enabled") {
return p_preset->get("blazium/web_headers/enabled");
}
if (p_option.begins_with("blazium/social_headers") && p_option != "blazium/social_headers/enabled") {
return p_preset->get("blazium/social_headers/enabled");
}
if (p_option.begins_with("blazium/discord_embed") && p_option != "blazium/discord_embed/enabled") {
return p_preset->get("blazium/discord_embed/enabled");
}
return true;
}

String EditorExportPlatformWeb::get_name() const {
Expand Down
1 change: 1 addition & 0 deletions platform/web/export/export_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class EditorExportPlatformWeb : public EditorExportPlatform {
virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const override;

virtual void get_export_options(List<ExportOption> *r_options) const override;
virtual bool get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const override;

virtual String get_name() const override;
virtual String get_os_name() const override;
Expand Down
15 changes: 14 additions & 1 deletion platform/web/js/engine/preloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,20 @@ const Preloader = /** @constructor */ function () { // eslint-disable-line no-un
loaded: 0,
done: false,
};
return fetch(file).then(function (response) {

// Check for the two meta elements in the head
const discordAutodetect = document.querySelector('meta[name="discord_autodetect"]')?.content === 'true';
const discordEmbed = document.querySelector('meta[name="discord_embed"]')?.content === 'true';

// Determine the base URL based on the meta elements
let baseUrl = '';
if (discordAutodetect) {
baseUrl = window.location.hostname.includes('discord') ? '.proxy/' : '';
} else if (discordEmbed) {
baseUrl = '.proxy/';
}

return fetch(baseUrl + file).then(function (response) {
if (!response.ok) {
return Promise.reject(new Error(`Failed loading file '${file}'`));
}
Expand Down

0 comments on commit 3410793

Please sign in to comment.