-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingle-post-oembed.html
205 lines (174 loc) · 13.8 KB
/
single-post-oembed.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Single Post Embed Example</title>
<meta name="author" content="@[email protected]" />
<meta name="keywords" content="mastodon, fediverse, example, oembed" />
<meta name="description" content="Example showing how to embed a single Mastodon post using oEmbed" />
<meta property="og:title" content="Mastodon Post Embed Example" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://mastodon-embeds.glitch.me/single-post-oembed.html" />
<meta property="og:image" content="https://mastodon-embeds.glitch.me/mastodon-logo.svg" />
<meta property="og:description" content="Example showing how to embed a single Mastodon post using oEmbed." />
<meta property="og:site_name" content="Mastodon Embed Examples" />
<link rel="icon" type="image/png" sizes="96x96" href="images/favicon-96x96.png">
<link rel="icon" type="image/x-icon" href="images/favicon.ico">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css" />
<style>
.section.has-top-controls {
position: relative;
padding-top: 4rem;
}
.theme-toggle-button {
position: absolute;
top: 1.5rem;
right: 1.5rem;
width: 2.5rem;
height: 2.5rem;
padding: 0;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
}
.theme-toggle-button .icon svg {
width: 1.1em;
height: 1.1em;
}
</style>
</head>
<body>
<button class="button theme-toggle-button" aria-label="Toggle theme">
<span class="icon theme-icon">
</span>
</button>
<section class="section has-top-controls">
<div class="container">
<h1 class="title">Mastodon Single Post Embed</h1>
<div class="box">
<div class="content">
<h2 class="title is-4">Using the Default Mastodon Embed Code</h2>
<p>
You can quickly grab code for any Mastodon post by choosing the "Get embed code" option
from the [...] menu on the Mastodon web interface when the post is loaded.
This is the simplest method for single posts. It will likely give you the
same <code>blockquote</code> code shown below.
</p>
</div>
</div>
<div class="box">
<div class="content">
<h2 class="title is-4">Using the oEmbed API</h2>
<p>
Another method is to programmatically get the embed code directly via the <a
href="https://docs.joinmastodon.org/methods/oembed/">oEmbed</a> API.
For example, the embed below (from <code>macaw.social</code>, which is where my own account lives) could
be retrieved using:
</p>
<div style="padding-bottom: 1rem;">
<code>curl "https://macaw.social/api/oembed?url=https://macaw.social/@andypiper/109349688112355529"</code>
</div>
<p>The API returns JSON data like this:</p>
<pre><code>{
"type": "rich",
"version": "1.0",
"author_name": "Andy Piper",
"author_url": "https://macaw.social/@andypiper",
"provider_name": "macaw.social",
"provider_url": "https://macaw.social/",
"cache_age": 86400,
"html": "\u003cblockquote class=\"mastodon-embed\" data-embed-url=\"https://macaw.social/@andypiper/109349688112355529/embed\" style=\"background: #FCF8FF; border-radius: 8px; border: 1px solid #C9C4DA; margin: 0; max-width: 540px; min-width: 270px; overflow: hidden; padding: 0;\"\u003e \u003ca href=\"https://macaw.social/@andypiper/109349688112355529\" target=\"_blank\" style=\"align-items: center; color: #1C1A25; display: flex; flex-direction: column; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', Roboto, sans-serif; font-size: 14px; justify-content: center; letter-spacing: 0.25px; line-height: 20px; padding: 24px; text-decoration: none;\"\u003e \u003csvg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"32\" height=\"32\" viewBox=\"0 0 79 75\"\u003e\u003cpath d=\"M74.7135 16.6043C73.6199 8.54587 66.5351 2.19527 58.1366 0.964691C56.7196 0.756754 51.351 0 38.9148 0H38.822C26.3824 0 23.7135 0.756754 22.2966 0.964691C14.1319 2.16118 6.67571 7.86752 4.86669 16.0214C3.99657 20.0369 3.90371 24.4888 4.06535 28.5726C4.29578 34.4289 4.34049 40.275 4.877 46.1075C5.24791 49.9817 5.89495 53.8251 6.81328 57.6088C8.53288 64.5968 15.4938 70.4122 22.3138 72.7848C29.6155 75.259 37.468 75.6697 44.9919 73.971C45.8196 73.7801 46.6381 73.5586 47.4475 73.3063C49.2737 72.7302 51.4164 72.086 52.9915 70.9542C53.0131 70.9384 53.0308 70.9178 53.0433 70.8942C53.0558 70.8706 53.0628 70.8445 53.0637 70.8179V65.1661C53.0634 65.1412 53.0574 65.1167 53.0462 65.0944C53.035 65.0721 53.0189 65.0525 52.9992 65.0371C52.9794 65.0218 52.9564 65.011 52.9318 65.0056C52.9073 65.0002 52.8819 65.0003 52.8574 65.0059C48.0369 66.1472 43.0971 66.7193 38.141 66.7103C29.6118 66.7103 27.3178 62.6981 26.6609 61.0278C26.1329 59.5842 25.7976 58.0784 25.6636 56.5486C25.6622 56.5229 25.667 56.4973 25.6775 56.4738C25.688 56.4502 25.7039 56.4295 25.724 56.4132C25.7441 56.397 25.7678 56.3856 25.7931 56.3801C25.8185 56.3746 25.8448 56.3751 25.8699 56.3816C30.6101 57.5151 35.4693 58.0873 40.3455 58.086C41.5183 58.086 42.6876 58.086 43.8604 58.0553C48.7647 57.919 53.9339 57.6701 58.7591 56.7361C58.8794 56.7123 58.9998 56.6918 59.103 56.6611C66.7139 55.2124 73.9569 50.665 74.6929 39.1501C74.7204 38.6967 74.7892 34.4016 74.7892 33.9312C74.7926 32.3325 75.3085 22.5901 74.7135 16.6043ZM62.9996 45.3371H54.9966V25.9069C54.9966 21.8163 53.277 19.7302 49.7793 19.7302C45.9343 19.7302 44.0083 22.1981 44.0083 27.0727V37.7082H36.0534V27.0727C36.0534 22.1981 34.124 19.7302 30.279 19.7302C26.8019 19.7302 25.0651 21.8163 25.0617 25.9069V45.3371H17.0656V25.3172C17.0656 21.2266 18.1191 17.9769 20.2262 15.568C22.3998 13.1648 25.2509 11.9308 28.7898 11.9308C32.8859 11.9308 35.9812 13.492 38.0447 16.6111L40.036 19.9245L42.0308 16.6111C44.0943 13.492 47.1896 11.9308 51.2788 11.9308C54.8143 11.9308 57.6654 13.1648 59.8459 15.568C61.9529 17.9746 63.0065 21.2243 63.0065 25.3172L62.9996 45.3371Z\" fill=\"currentColor\"/\u003e\u003c/svg\u003e \u003cdiv style=\"color: #787588; margin-top: 16px;\"\u003ePost by @[email protected]\u003c/div\u003e \u003cdiv style=\"font-weight: 500;\"\u003eView on Mastodon\u003c/div\u003e \u003c/a\u003e \u003c/blockquote\u003e \u003cscript data-allowed-prefixes=\"https://macaw.social/\" async src=\"https://macaw.social/embed.js\"\u003e\u003c/script\u003e",
"width": 400,
"height": null
}</code></pre>
<p>The actual HTML embed code is found as an escaped string within the <code>html</code> property in the
JSON response.</p>
</div>
</div>
<div class="box">
<div class="content">
<h2 class="title is-4">Rendered embed</h2>
<p>This is what the embedded post looks like when rendered in the browser:</p>
<blockquote class="mastodon-embed" data-embed-url="https://macaw.social/@andypiper/109349688112355529/embed"
style="background: #FCF8FF; border-radius: 8px; border: 1px solid #C9C4DA; margin: 0; max-width: 540px; min-width: 270px; overflow: hidden; padding: 0;">
<a href="https://macaw.social/@andypiper/109349688112355529" target="_blank"
style="align-items: center; color: #1C1A25; display: flex; flex-direction: column; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', Roboto, sans-serif; font-size: 14px; justify-content: center; letter-spacing: 0.25px; line-height: 20px; padding: 24px; text-decoration: none;">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="32"
height="32" viewBox="0 0 79 75">
<path
d="M74.7135 16.6043C73.6199 8.54587 66.5351 2.19527 58.1366 0.964691C56.7196 0.756754 51.351 0 38.9148 0H38.822C26.3824 0 23.7135 0.756754 22.2966 0.964691C14.1319 2.16118 6.67571 7.86752 4.86669 16.0214C3.99657 20.0369 3.90371 24.4888 4.06535 28.5726C4.29578 34.4289 4.34049 40.275 4.877 46.1075C5.24791 49.9817 5.89495 53.8251 6.81328 57.6088C8.53288 64.5968 15.4938 70.4122 22.3138 72.7848C29.6155 75.259 37.468 75.6697 44.9919 73.971C45.8196 73.7801 46.6381 73.5586 47.4475 73.3063C49.2737 72.7302 51.4164 72.086 52.9915 70.9542C53.0131 70.9384 53.0308 70.9178 53.0433 70.8942C53.0558 70.8706 53.0628 70.8445 53.0637 70.8179V65.1661C53.0634 65.1412 53.0574 65.1167 53.0462 65.0944C53.035 65.0721 53.0189 65.0525 52.9992 65.0371C52.9794 65.0218 52.9564 65.011 52.9318 65.0056C52.9073 65.0002 52.8819 65.0003 52.8574 65.0059C48.0369 66.1472 43.0971 66.7193 38.141 66.7103C29.6118 66.7103 27.3178 62.6981 26.6609 61.0278C26.1329 59.5842 25.7976 58.0784 25.6636 56.5486C25.6622 56.5229 25.667 56.4973 25.6775 56.4738C25.688 56.4502 25.7039 56.4295 25.724 56.4132C25.7441 56.397 25.7678 56.3856 25.7931 56.3801C25.8185 56.3746 25.8448 56.3751 25.8699 56.3816C30.6101 57.5151 35.4693 58.0873 40.3455 58.086C41.5183 58.086 42.6876 58.086 43.8604 58.0553C48.7647 57.919 53.9339 57.6701 58.7591 56.7361C58.8794 56.7123 58.9998 56.6918 59.103 56.6611C66.7139 55.2124 73.9569 50.665 74.6929 39.1501C74.7204 38.6967 74.7892 34.4016 74.7892 33.9312C74.7926 32.3325 75.3085 22.5901 74.7135 16.6043ZM62.9996 45.3371H54.9966V25.9069C54.9966 21.8163 53.277 19.7302 49.7793 19.7302C45.9343 19.7302 44.0083 22.1981 44.0083 27.0727V37.7082H36.0534V27.0727C36.0534 22.1981 34.124 19.7302 30.279 19.7302C26.8019 19.7302 25.0651 21.8163 25.0617 25.9069V45.3371H17.0656V25.3172C17.0656 21.2266 18.1191 17.9769 20.2262 15.568C22.3998 13.1648 25.2509 11.9308 28.7898 11.9308C32.8859 11.9308 35.9812 13.492 38.0447 16.6111L40.036 19.9245L42.0308 16.6111C44.0943 13.492 47.1896 11.9308 51.2788 11.9308C54.8143 11.9308 57.6654 13.1648 59.8459 15.568C61.9529 17.9746 63.0065 21.2243 63.0065 25.3172L62.9996 45.3371Z"
fill="currentColor" />
</svg>
<div style="color: #787588; margin-top: 16px;">Post by @[email protected]</div>
<div style="font-weight: 500;">View on Mastodon</div>
</a>
</blockquote>
<script data-allowed-prefixes="https://macaw.social/" async src="https://macaw.social/embed.js"></script>
</div>
</div>
</div>
</section>
<footer class="footer">
<div class="content has-text-centered">
<p>
<strong><a href="https://mastodon-embeds.glitch.me">Mastodon Embed Examples</a></strong>.
Styled using <a href="https://bulma.io/" target="_blank" rel="noopener noreferrer">Bulma</a>.
Find me on Mastodon: <a href="https://macaw.social/@andypiper" target="_blank"
rel="noopener noreferrer">@[email protected]</a>.
</p>
</div>
</footer>
<script>
(function () {
// Simple inline SVGs for icons
const sunIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="m17.66 17.66 1.41 1.41"/><path d="M2 12h2"/><path d="M20 12h2"/><path d="m6.34 17.66-1.41 1.41"/><path d="m19.07 4.93-1.41 1.41"/></svg>`;
const moonIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"/></svg>`;
// Select ALL buttons with this class
const toggleButtons = document.querySelectorAll('.theme-toggle-button');
// Select ALL icon spans within those buttons
const iconSpans = document.querySelectorAll('.theme-toggle-button .theme-icon');
const htmlElement = document.documentElement;
const storageKey = 'theme-preference';
// Function to apply theme and update ALL icons
const applyTheme = (theme) => {
htmlElement.dataset.theme = theme;
const iconHTML = theme === 'dark' ? moonIcon : sunIcon;
iconSpans.forEach(span => {
// Check if span exists before setting innerHTML
if (span) span.innerHTML = iconHTML;
});
};
// Function to get the preferred theme
const getPreferredTheme = () => {
const savedTheme = localStorage.getItem(storageKey);
if (savedTheme) return savedTheme;
// Default to light if no system preference detected or needed
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
};
// Function to handle the button click
const handleToggleClick = () => {
// Read the current theme directly from the html element
let currentTheme = htmlElement.dataset.theme || getPreferredTheme();
let newTheme = currentTheme === 'dark' ? 'light' : 'dark';
applyTheme(newTheme);
localStorage.setItem(storageKey, newTheme);
}
// --- Initialization ---
// Apply the initial theme when the script runs
let currentTheme = getPreferredTheme();
applyTheme(currentTheme);
// Add listeners to all toggle buttons found
if (toggleButtons.length > 0) {
toggleButtons.forEach(button => {
button.addEventListener('click', handleToggleClick);
});
} else {
// It's okay if not found on pages without the button
// console.warn("Theme toggle button(s) not found on this page.");
}
})();
</script>
</body>
</html>