- Get link
- X
- Other Apps
Server Side/Cloudflare
https://falling-cherry-d9af.amanurung1985.workers.dev/
Catatan BUG PENGERJAAN:
pada saat Copy-Paste dr ChatGPT ke Area Input KODE Workers/bagian dr Cloudflare
mengalami masalah pada PASTE Kode
Coba Paste minimal 2x
kalau perlu hapus dulu hasil PASTE PERTAMA
sampai BANNER ERROR berwarna MERAH
menjadi BANNER Sukses! aka OK berwarna HIJAU
export default {
async fetch(request) {
// =============================
// CORS HEADERS
// =============================
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
"Content-Type": "application/json"
};
// =============================
// HANDLE PREFLIGHT
// =============================
if (request.method === "OPTIONS") {
return new Response(null, {
status: 204,
headers: corsHeaders
});
}
// =============================
// ONLY ALLOW POST
// =============================
if (request.method !== "POST") {
return new Response(
JSON.stringify({
status: "error",
message: "Method not allowed"
}),
{ status: 405, headers: corsHeaders }
);
}
// =============================
// PARSE BODY
// =============================
let data;
try {
data = await request.json();
} catch (e) {
return new Response(
JSON.stringify({
status: "error",
message: "Invalid JSON body"
}),
{ status: 400, headers: corsHeaders }
);
}
const url = data.url || "";
// =============================
// URL VALIDATION
// =============================
const match = url.match(/tiktok\.com\/@([^\/]+)\/video\/(\d+)/);
if (!match) {
return new Response(
JSON.stringify({
status: "error",
message: "Unsupported TikTok URL format"
}),
{ status: 400, headers: corsHeaders }
);
}
const username = match[1];
const videoId = match[2];
// =============================
// EMBED TEMPLATE
// =============================
const embedCode = `
<!-- HEADER -->
<h2 style="text-align:center;margin:16px 0;">
TikTok Archive – Embedded Preview
</h2>
<hr style="border:0;height:2px;background:#ddd;margin:16px 0;">
<div id="tiktok-js-wrapper">
<h3>Official TikTok Embed</h3>
<blockquote class="tiktok-embed"
cite="https://www.tiktok.com/@${username}/video/${videoId}"
data-video-id="${videoId}"
style="width:100%;">
<section></section>
</blockquote>
</div>
<div id="tiktok-iframe-wrapper" style="display:none;">
<h3>Iframe Embed (Fallback)</h3>
<div style="position:relative;width:100%;padding-top:177.77%;overflow:hidden;border-radius:12px;border:1px solid #ddd;">
<iframe
src="https://www.tiktok.com/embed/v2/${videoId}"
style="position:absolute;top:0;left:0;width:100%;height:100%;border:0;"
loading="lazy"
allowfullscreen>
</iframe>
</div>
</div>
<div id="tiktok-link-fallback" style="display:none;text-align:center;margin-top:16px;">
<a href="https://www.tiktok.com/@${username}/video/${videoId}" target="_blank">
▶ View this video on TikTok
</a>
</div>
<script async src="https://www.tiktok.com/embed.js"></script>
<script>
(function () {
var jsWrapper = document.getElementById('tiktok-js-wrapper');
var iframeWrapper = document.getElementById('tiktok-iframe-wrapper');
var iframe = iframeWrapper.querySelector('iframe');
var linkFallback = document.getElementById('tiktok-link-fallback');
setTimeout(function () {
if (!jsWrapper.querySelector('iframe')) {
jsWrapper.style.display = 'none';
iframeWrapper.style.display = 'block';
iframe.onerror = function () {
iframeWrapper.style.display = 'none';
linkFallback.style.display = 'block';
};
}
}, 2500);
})();
</script>
`;
// =============================
// RETURN SUCCESS
// =============================
return new Response(
JSON.stringify({
status: "success",
username,
videoId,
embedCode
}),
{ status: 200, headers: corsHeaders }
);
}
};
Comments