Add bookmarklet for auto sessionid extraction from TikTok

This commit is contained in:
Khoa.vo 2025-12-19 15:19:00 +07:00
parent 4073a84a7a
commit fd9b444222

View file

@ -1,24 +1,23 @@
import React, { useState } from 'react'; import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate, useSearchParams } from 'react-router-dom';
import axios from 'axios'; import axios from 'axios';
import { API_BASE_URL } from '../config'; import { API_BASE_URL } from '../config';
export const Login: React.FC = () => { export const Login: React.FC = () => {
const [sessionId, setSessionId] = useState('');
const [error, setError] = useState(''); const [error, setError] = useState('');
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [step, setStep] = useState<1 | 2>(1); const [searchParams] = useSearchParams();
const navigate = useNavigate(); const navigate = useNavigate();
const openTikTokLogin = () => { // Check if we have a sessionid from URL (bookmarklet redirect)
// Open TikTok login in new tab useEffect(() => {
window.open('https://www.tiktok.com/login', '_blank'); const sessionIdFromUrl = searchParams.get('sessionid');
setStep(2); if (sessionIdFromUrl) {
}; handleAutoConnect(sessionIdFromUrl);
}
const handleConnect = async () => { }, [searchParams]);
if (!sessionId.trim()) return;
const handleAutoConnect = async (sessionId: string) => {
setError(''); setError('');
setIsLoading(true); setIsLoading(true);
@ -26,7 +25,7 @@ export const Login: React.FC = () => {
const res = await axios.post(`${API_BASE_URL}/auth/credentials`, { const res = await axios.post(`${API_BASE_URL}/auth/credentials`, {
credentials: { credentials: {
http: { http: {
cookies: { sessionid: sessionId.trim() } cookies: { sessionid: sessionId }
} }
} }
}); });
@ -34,19 +33,39 @@ export const Login: React.FC = () => {
if (res.data.status === 'success') { if (res.data.status === 'success') {
navigate('/'); navigate('/');
} else { } else {
setError(res.data.message || 'Connection failed.'); setError('Connection failed. Please try again.');
setIsLoading(false);
} }
} catch (err: any) { } catch (err: any) {
setError(err.response?.data?.detail || 'Invalid session ID. Please try again.'); setError('Invalid session. Please try again.');
} finally {
setIsLoading(false); setIsLoading(false);
} }
}; };
// Get the current app URL for bookmarklet redirect
const appUrl = window.location.origin;
// Bookmarklet code - extracts sessionid and redirects back
const bookmarkletCode = `javascript:(function(){var c=document.cookie.split(';');var s='';for(var i=0;i<c.length;i++){var p=c[i].trim().split('=');if(p[0]==='sessionid'){s=p[1];break;}}if(s){window.location='${appUrl}/login?sessionid='+encodeURIComponent(s);}else{alert('Please login to TikTok first!');}})();`;
const copyBookmarklet = () => {
navigator.clipboard.writeText(bookmarkletCode);
alert('Bookmarklet code copied! Create a new bookmark and paste this as the URL.');
};
if (isLoading) {
return (
<div className="min-h-screen bg-gradient-to-br from-gray-950 via-black to-gray-950 flex flex-col items-center justify-center">
<div className="w-16 h-16 border-4 border-pink-500/30 border-t-pink-500 rounded-full animate-spin mb-4" />
<p className="text-white">Connecting to TikTok...</p>
</div>
);
}
return ( return (
<div className="min-h-screen bg-gradient-to-br from-gray-950 via-black to-gray-950 flex flex-col"> <div className="min-h-screen bg-gradient-to-br from-gray-950 via-black to-gray-950 flex flex-col">
{/* Header */} {/* Header */}
<div className="flex-shrink-0 pt-10 pb-6 px-6 text-center"> <div className="flex-shrink-0 pt-10 pb-4 px-6 text-center">
<div className="relative inline-block mb-3"> <div className="relative inline-block mb-3">
<div className="w-14 h-14 bg-gradient-to-r from-cyan-400 to-pink-500 rounded-2xl rotate-12 absolute -inset-1 blur-lg opacity-50" /> <div className="w-14 h-14 bg-gradient-to-r from-cyan-400 to-pink-500 rounded-2xl rotate-12 absolute -inset-1 blur-lg opacity-50" />
<div className="relative w-14 h-14 bg-gradient-to-r from-cyan-400 to-pink-500 rounded-2xl flex items-center justify-center"> <div className="relative w-14 h-14 bg-gradient-to-r from-cyan-400 to-pink-500 rounded-2xl flex items-center justify-center">
@ -68,84 +87,59 @@ export const Login: React.FC = () => {
</div> </div>
)} )}
{/* Step 1: Open TikTok */} {/* One-Time Setup (Desktop) */}
<div className={`transition-opacity ${step === 1 ? 'opacity-100' : 'opacity-50'}`}> <div className="bg-gradient-to-r from-cyan-500/10 to-pink-500/10 border border-white/10 rounded-2xl p-5 mb-6">
<div className="flex items-center gap-2 mb-3"> <h2 className="text-white font-semibold mb-3 flex items-center gap-2">
<div className="w-6 h-6 bg-cyan-500 rounded-full flex items-center justify-center text-white font-bold text-xs">1</div> <span className="text-lg">🖥</span> Desktop Setup (One-Time)
<span className="text-white font-medium">Login to TikTok</span> </h2>
</div> <ol className="text-gray-300 text-sm space-y-2 mb-4">
<button <li className="flex gap-2">
onClick={openTikTokLogin} <span className="text-cyan-400">1.</span>
className="w-full py-4 bg-black border-2 border-white/20 hover:border-white/40 rounded-xl text-white font-medium flex items-center justify-center gap-3 transition-all" <span>Drag this button to your bookmarks bar:</span>
</li>
</ol>
{/* Draggable Bookmarklet */}
<a
href={bookmarkletCode}
onClick={(e) => e.preventDefault()}
onDragEnd={() => { }}
className="inline-block py-2 px-4 bg-gradient-to-r from-pink-500 to-orange-500 text-white font-bold rounded-lg text-sm cursor-grab active:cursor-grabbing shadow-lg mb-4"
title="Drag me to your bookmarks bar!"
> >
<svg className="w-5 h-5" viewBox="0 0 24 24" fill="currentColor"> 📎 Get PureStream
<path d="M19.59 6.69a4.83 4.83 0 0 1-3.77-4.25V2h-3.45v13.67a2.89 2.89 0 0 1-5.2 1.74 2.89 2.89 0 0 1 2.31-4.64 2.93 2.93 0 0 1 .88.13V9.4a6.84 6.84 0 0 0-1-.05A6.33 6.33 0 0 0 5 20.1a6.34 6.34 0 0 0 10.86-4.43v-7a8.16 8.16 0 0 0 4.77 1.52v-3.4a4.85 4.85 0 0 1-1-.1z" /> </a>
</svg>
Open TikTok Login <p className="text-gray-500 text-xs">
</button> Can't drag? <button onClick={copyBookmarklet} className="text-cyan-400 underline">Copy code</button> and create a bookmark manually.
<p className="text-gray-500 text-xs text-center mt-2">Opens in new tab - login with your account</p> </p>
</div> </div>
{/* Arrow */} {/* How to Use */}
<div className="flex justify-center my-4"> <div className="bg-white/5 rounded-2xl p-5">
<div className="text-gray-600"></div> <h2 className="text-white font-semibold mb-3 flex items-center gap-2">
</div> <span className="text-lg">📱</span> How to Connect
</h2>
{/* Step 2: Get Session ID */} <ol className="text-gray-300 text-sm space-y-3">
<div className={`transition-opacity ${step === 2 ? 'opacity-100' : 'opacity-50'}`}> <li className="flex gap-3">
<div className="flex items-center gap-2 mb-3"> <div className="w-6 h-6 bg-cyan-500 rounded-full flex items-center justify-center flex-shrink-0 text-white font-bold text-xs">1</div>
<div className={`w-6 h-6 rounded-full flex items-center justify-center text-white font-bold text-xs ${step === 2 ? 'bg-pink-500' : 'bg-gray-600'}`}>2</div> <span>Go to <a href="https://www.tiktok.com" target="_blank" rel="noopener noreferrer" className="text-cyan-400 underline">tiktok.com</a> and login with your account</span>
<span className="text-white font-medium">Copy your Session ID</span> </li>
</div> <li className="flex gap-3">
<div className="w-6 h-6 bg-pink-500 rounded-full flex items-center justify-center flex-shrink-0 text-white font-bold text-xs">2</div>
<div className="bg-white/5 rounded-xl p-4 mb-4 text-sm"> <span>Click the <strong className="text-pink-400">"📎 Get PureStream"</strong> bookmark</span>
<p className="text-gray-300 mb-2">After logging in on TikTok:</p> </li>
<ol className="text-gray-400 space-y-1 ml-4 list-decimal text-xs"> <li className="flex gap-3">
<li>Press <code className="bg-black/50 px-1 rounded">F12</code> (or right-click Inspect)</li> <div className="w-6 h-6 bg-purple-500 rounded-full flex items-center justify-center flex-shrink-0 text-white font-bold text-xs">3</div>
<li>Go to <strong className="text-white">Application</strong> <strong className="text-white">Cookies</strong></li> <span>You'll be automatically redirected and logged in!</span>
<li>Find <code className="bg-cyan-500/20 text-cyan-400 px-1 rounded">sessionid</code> and copy its value</li> </li>
</ol> </ol>
</div> </div>
<input {/* Note */}
type="text" <p className="text-gray-600 text-xs text-center mt-6">
value={sessionId} The bookmarklet securely reads your TikTok session and connects it to PureStream.
onChange={(e) => setSessionId(e.target.value)} </p>
placeholder="Paste sessionid value here..."
className="w-full bg-black/60 border-2 border-white/10 rounded-xl p-4 text-white text-sm font-mono focus:outline-none focus:border-cyan-500/50 placeholder:text-gray-600 mb-4"
disabled={isLoading}
/>
<button
onClick={handleConnect}
disabled={!sessionId.trim() || isLoading}
className={`w-full py-4 text-white font-semibold rounded-xl transition-all text-base ${sessionId.trim() && !isLoading
? 'bg-gradient-to-r from-cyan-500 to-pink-500 shadow-lg shadow-pink-500/20'
: 'bg-gray-700 cursor-not-allowed'
}`}
>
{isLoading ? (
<span className="flex items-center justify-center gap-2">
<div className="w-4 h-4 border-2 border-white/30 border-t-white rounded-full animate-spin" />
Connecting...
</span>
) : (
'Connect'
)}
</button>
</div>
{/* Help */}
<div className="mt-8 pt-6 border-t border-white/10 text-center">
<a
href="https://www.youtube.com/results?search_query=how+to+find+tiktok+sessionid+cookie+chrome"
target="_blank"
rel="noopener noreferrer"
className="text-gray-500 text-xs underline hover:text-gray-400"
>
Need help? Watch a tutorial
</a>
</div>
</div> </div>
</div> </div>
</div> </div>