Files
moon-gaming/new-login-and-save/login2.html
T

398 lines
13 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Sign Up / Login (FS)</title>
<script src="https://js.puter.com/v2/"></script>
<style>
body { background: #181818; color: #eee; font-family: system-ui, sans-serif; }
.container { max-width: 700px; margin: 40px auto; background: #222; padding: 24px; border-radius: 12px; box-shadow: 0 0 20px #0006; }
input, button, select { width: 100%; margin: 8px 0; padding: 10px; border-radius: 6px; border: none; box-sizing: border-box; }
button { background: #444; color: #fff; font-weight: bold; cursor: pointer; }
.msg { margin: 8px 0; color: #ffb; min-height: 1.2em; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }
.file-actions { display:flex; gap:8px; margin-top:8px; flex-wrap:wrap; }
.file-item { background:#2b2b2b; padding:8px; border-radius:6px; display:flex; gap:8px; align-items:center; }
.small-btn { padding:6px 8px; font-size:0.9em; width:auto; }
.disabled { opacity: 0.5; pointer-events: none; }
</style>
</head>
<body>
<div class="container">
<h2>Sign Up / Login (Filesystem)</h2>
<div class="row">
<button id="home" onclick="location.href='/index.html'">Home</button>
<button id="ensure">Ensure Cookies Dir</button>
</div>
<button id="signup">Sign In & Save Cookies (to file)</button>
<button id="refresh">Refresh File List</button>
<div style="margin-top:8px;">
<h4>Saved cookie files</h4>
<div id="filesList">Loading...</div>
</div>
<div class="file-actions" id="fileActions" style="margin-top:12px;"></div>
<div style="margin-top:12px;">
<button id="clearAll">Cloud Reset (Delete All Cookie Files)</button>
</div>
<div class="msg" id="msg"></div>
</div>
<script>
// Small, robust FS helpers that adapt to a few runtime variations of 'puter.fs' APIs.
const msg = document.getElementById('msg');
const filesListEl = document.getElementById('filesList');
const fileActionsEl = document.getElementById('fileActions');
const DIR = 'cookies';
function disableUI(reason) {
['ensure','signup','refresh','clearAll'].forEach(id => {
const el = document.getElementById(id);
if (el) {
el.classList.add('disabled');
el.title = reason || '';
}
});
}
function enableUI() {
['ensure','signup','refresh','clearAll'].forEach(id => {
const el = document.getElementById(id);
if (el) el.classList.remove('disabled');
});
}
// Basic cookie helpers
function getCookies() {
if (!document.cookie) return {};
return Object.fromEntries(document.cookie.split('; ').filter(Boolean).map(c => {
const [k, ...v] = c.split('=');
return [k, v.join('=')];
}));
}
function setCookies(cookies) {
Object.entries(cookies).forEach(([k, v]) => {
const safeVal = typeof v === 'string' ? v : JSON.stringify(v);
// encode cookie value to be safe
document.cookie = `${k}=${encodeURIComponent(safeVal)}; path=/;`;
});
}
// Detect puter and wrap file ops so the rest of the code stays clean.
function hasPuter() {
return typeof window.puter !== 'undefined' && !!puter.fs;
}
async function fsMkdir(dir) {
if (!hasPuter()) throw new Error('puter not available');
// try several possible mkdir signatures
if (puter.fs.mkdir) {
try { return await puter.fs.mkdir(dir); } catch (e) {
// some implementations accept options or throw when exists - try readdir to confirm
}
}
// fallback: try readdir to "ensure" existence
try { await puter.fs.readdir(dir); return true; } catch (e) { throw e; }
}
async function fsReaddir(dir) {
if (!hasPuter()) throw new Error('puter not available');
if (!puter.fs.readdir) throw new Error('puter.fs.readdir not available');
const res = await puter.fs.readdir(dir);
// Normalize many possible shapes:
// - array of strings ['a','b']
// - array of objects [{name:'a'},{name:'b'}]
// - object with entries property { entries: [...] }
if (!res) return [];
if (Array.isArray(res)) {
if (res.length === 0) return [];
if (typeof res[0] === 'string') return res;
if (res[0] && typeof res[0] === 'object' && 'name' in res[0]) return res.map(r => r.name);
// else try to stringify/return something readable
return res.map(String);
}
if (res.entries && Array.isArray(res.entries)) {
return res.entries.map(e => (typeof e === 'string' ? e : (e.name || String(e))));
}
// unknown format, try to coerce
return [String(res)];
}
async function fsWrite(path, data) {
if (!hasPuter()) throw new Error('puter not available');
// try write(path, data)
if (puter.fs.write) return await puter.fs.write(path, data);
// try writeFile
if (puter.fs.writeFile) return await puter.fs.writeFile(path, data);
// try put
if (puter.fs.put) return await puter.fs.put(path, data);
throw new Error('No supported write method on puter.fs');
}
async function fsRead(path) {
if (!hasPuter()) throw new Error('puter not available');
if (puter.fs.read) {
const r = await puter.fs.read(path);
// many implementations return string, Buffer, or object { data: '...' }
if (typeof r === 'string') return r;
if (r == null) return r;
if (typeof r === 'object') {
if ('data' in r) return r.data;
if ('content' in r) return r.content;
// fallback to JSON string
try { return JSON.stringify(r); } catch (e) { return String(r); }
}
return String(r);
}
if (puter.fs.readFile) {
const r = await puter.fs.readFile(path);
if (typeof r === 'string') return r;
if (r && r.toString) return r.toString();
return r;
}
throw new Error('No supported read method on puter.fs');
}
async function fsDelete(path) {
if (!hasPuter()) throw new Error('puter not available');
// try multiple names
if (puter.fs.delete) return await puter.fs.delete(path);
if (puter.fs.unlink) return await puter.fs.unlink(path);
if (puter.fs.rm) return await puter.fs.rm(path);
throw new Error('No supported delete method on puter.fs');
}
// UI / Actions
async function ensureDir() {
msg.textContent = 'Ensuring cookies directory...';
if (!hasPuter()) {
msg.textContent = 'puter runtime not available in this environment.';
disableUI('puter not available');
return;
}
try {
await fsMkdir(DIR);
msg.textContent = `Ensured directory: ${DIR}`;
} catch (e) {
// try readdir to check if exists
try {
await fsReaddir(DIR);
msg.textContent = `Directory already exists: ${DIR}`;
} catch (err) {
msg.textContent = 'Error ensuring directory: ' + (err.message || err);
console.error('ensureDir ->', e, err);
}
}
}
async function listFiles() {
filesListEl.textContent = 'Loading...';
fileActionsEl.innerHTML = '';
if (!hasPuter()) {
filesListEl.textContent = 'puter runtime not available. File listing is disabled.';
disableUI('puter not available');
return [];
}
try {
const entries = await fsReaddir(DIR).catch(err => {
// if directory doesn't exist, show empty list
return null;
});
if (!entries || entries.length === 0) {
filesListEl.textContent = 'No saved cookie files.';
return [];
}
filesListEl.innerHTML = '';
entries.forEach(name => {
const wrap = document.createElement('div');
wrap.className = 'file-item';
const label = document.createElement('div');
label.textContent = name;
label.style.flex = '1';
wrap.appendChild(label);
const loadBtn = document.createElement('button');
loadBtn.textContent = 'Load';
loadBtn.className = 'small-btn';
loadBtn.onclick = () => loadFile(name);
const delBtn = document.createElement('button');
delBtn.textContent = 'Delete';
delBtn.className = 'small-btn';
delBtn.onclick = () => deleteFile(name);
wrap.appendChild(loadBtn);
wrap.appendChild(delBtn);
filesListEl.appendChild(wrap);
});
return entries;
} catch (e) {
filesListEl.textContent = 'Error listing files: ' + (e.message || e);
console.error('listFiles error', e);
return [];
}
}
async function saveCookiesToFile() {
msg.textContent = 'Signing in and saving cookies to file...';
if (!hasPuter()) {
msg.textContent = 'puter runtime not available.';
disableUI('puter not available');
return;
}
try {
// ensure sign-in works
if (!puter.auth || !puter.auth.signIn) {
throw new Error('puter.auth.signIn not available');
}
const user = await puter.auth.signIn();
if (!user) throw new Error('Sign in failed or cancelled.');
await ensureDir();
const cookies = getCookies();
const filename = `cookies-${Date.now()}.txt`;
const path = `${DIR}/${filename}`;
await fsWrite(path, JSON.stringify(cookies, null, 2));
msg.textContent = `Saved cookies to ${path}`;
await listFiles();
} catch (e) {
msg.textContent = 'Error saving file: ' + (e.message || e);
console.error('saveCookiesToFile error', e);
}
}
async function loadFile(filename) {
msg.textContent = `Signing in and loading ${filename}...`;
if (!hasPuter()) {
msg.textContent = 'puter runtime not available.';
return;
}
try {
if (!puter.auth || !puter.auth.signIn) {
throw new Error('puter.auth.signIn not available');
}
const user = await puter.auth.signIn();
if (!user) throw new Error('Sign in failed or cancelled.');
// filename may sometimes already be a path
const path = filename.startsWith(DIR + '/') ? filename : `${DIR}/${filename}`;
const data = await fsRead(path);
if (!data) throw new Error('File empty or unreadable.');
let cookies;
if (typeof data === 'string') {
try {
cookies = JSON.parse(data);
} catch (parseErr) {
// If content is not JSON but looks like key=value lines, attempt to parse
const maybe = {};
data.split(/\r?\n/).forEach(line => {
if (!line) return;
const [k, ...v] = line.split('=');
if (!k) return;
maybe[k.trim()] = decodeURIComponent((v || []).join('=').trim());
});
if (Object.keys(maybe).length > 0) cookies = maybe;
else throw new Error('Saved data is not valid JSON.');
}
} else if (typeof data === 'object') {
cookies = data;
} else {
throw new Error('Unsupported file data type');
}
setCookies(cookies);
msg.textContent = `Cookies restored from ${filename}. Reloading...`;
setTimeout(() => location.reload(), 900);
} catch (e) {
msg.textContent = 'Error loading file: ' + (e.message || e);
console.error('loadFile error', e);
}
}
async function deleteFile(filename) {
if (!confirm(`Delete ${filename}? This cannot be undone.`)) return;
msg.textContent = `Deleting ${filename}...`;
if (!hasPuter()) {
msg.textContent = 'puter runtime not available.';
return;
}
try {
if (!puter.auth || !puter.auth.signIn) {
throw new Error('puter.auth.signIn not available');
}
const user = await puter.auth.signIn();
if (!user) throw new Error('Sign in failed or cancelled.');
const path = filename.startsWith(DIR + '/') ? filename : `${DIR}/${filename}`;
await fsDelete(path);
msg.textContent = `Deleted ${filename}.`;
await listFiles();
} catch (e) {
msg.textContent = 'Error deleting file: ' + (e.message || e);
console.error('deleteFile error', e);
}
}
async function clearAllFiles() {
if (!confirm('Delete ALL cookie files in cloud?')) return;
msg.textContent = 'Signing in and deleting all cookie files...';
if (!hasPuter()) {
msg.textContent = 'puter runtime not available.';
return;
}
try {
if (!puter.auth || !puter.auth.signIn) {
throw new Error('puter.auth.signIn not available');
}
const user = await puter.auth.signIn();
if (!user) throw new Error('Sign in failed or cancelled.');
const entries = await fsReaddir(DIR).catch(() => null);
if (!entries || entries.length === 0) {
msg.textContent = 'No files to delete.';
return;
}
for (const name of entries) {
const path = name.startsWith(DIR + '/') ? name : `${DIR}/${name}`;
await fsDelete(path).catch(err => {
console.warn('Failed deleting', name, err);
});
}
msg.textContent = 'All cookie files deleted.';
await listFiles();
} catch (e) {
msg.textContent = 'Error clearing files: ' + (e.message || e);
console.error('clearAllFiles error', e);
}
}
// Wire up UI
document.getElementById('ensure').onclick = async () => { await ensureDir(); await listFiles(); };
document.getElementById('signup').onclick = saveCookiesToFile;
document.getElementById('refresh').onclick = listFiles;
document.getElementById('clearAll').onclick = clearAllFiles;
// Initial load
(async () => {
if (!hasPuter()) {
msg.textContent = 'puter runtime not detected. Buttons that require cloud FS will be disabled.';
disableUI('puter not available');
filesListEl.textContent = 'puter runtime not detected.';
return;
}
try {
// Attempt to list files; if dir missing, listFiles handles it
enableUI();
await listFiles();
} catch (e) {
console.warn('Initial listing failed', e);
msg.textContent = 'Initial listing failed: ' + (e.message || e);
}
})();
</script>
</body>
</html>