Refactor examples into per-app directories with separate HTML, CSS, and JS
- Give each example its own directory: chat/, whiteboard/, sdk-demo/, hrpc-demo/, data-demo/, screenshare/ - Split each into index.html, style.css, and app.js (no inline script/style; CSP-friendly) - Remove old flat files (e.g. chat.html, whiteboard.html, whiteboard.js) - Update README, examples/README, and docs/HRPC to use new paths
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
(function () {
|
||||
const logEl = document.getElementById('log');
|
||||
function log(msg, type) {
|
||||
const line = document.createElement('div');
|
||||
line.className = type || 'sys';
|
||||
line.textContent = '[' + new Date().toLocaleTimeString() + '] ' + msg;
|
||||
logEl.appendChild(line);
|
||||
logEl.scrollTop = logEl.scrollHeight;
|
||||
}
|
||||
|
||||
function request(type, payload) {
|
||||
return window.BridgeSwarm.request(type, payload || {}).then(function (r) {
|
||||
if (!r.ok) throw new Error(r.error || 'Request failed');
|
||||
return r;
|
||||
});
|
||||
}
|
||||
|
||||
(function initWhenReady(attempts) {
|
||||
if (attempts >= 50) {
|
||||
log('BridgeSwarm.request not available. Install extension and reload.', 'err');
|
||||
return;
|
||||
}
|
||||
if (typeof window.BridgeSwarm === 'undefined') {
|
||||
setTimeout(function () { initWhenReady(attempts + 1); }, 100);
|
||||
return;
|
||||
}
|
||||
window.BridgeSwarm.ready().then(function () {
|
||||
log('Data API ready. Use the buttons above.');
|
||||
|
||||
document.getElementById('coreInfo').onclick = async function () {
|
||||
try {
|
||||
const r = await request('coreInfo');
|
||||
log('Core: key=' + (r.key || '').slice(0, 16) + '… length=' + r.length + ' writable=' + r.writable, 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
document.getElementById('coreAppendBtn').onclick = async function () {
|
||||
const data = document.getElementById('coreAppend').value;
|
||||
if (!data) return;
|
||||
try {
|
||||
const base64 = btoa(unescape(encodeURIComponent(data)));
|
||||
await request('coreAppend', { base64 });
|
||||
log('Appended ' + data.length + ' bytes', 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
document.getElementById('beePut').onclick = async function () {
|
||||
const key = document.getElementById('beeKey').value;
|
||||
const value = document.getElementById('beeValue').value;
|
||||
if (!key) return;
|
||||
try {
|
||||
await request('beePut', { key, value });
|
||||
log('bee.put("' + key + '", "' + value + '")', 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
document.getElementById('beeGet').onclick = async function () {
|
||||
const key = document.getElementById('beeKey').value;
|
||||
if (!key) return;
|
||||
try {
|
||||
const r = await request('beeGet', { key });
|
||||
log('bee.get("' + key + '") => ' + (r.value === null ? 'null' : JSON.stringify(r.value)), 'val');
|
||||
if (r.value !== null) document.getElementById('beeValue').value = r.value;
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
document.getElementById('beeDel').onclick = async function () {
|
||||
const key = document.getElementById('beeKey').value;
|
||||
if (!key) return;
|
||||
try {
|
||||
await request('beeDel', { key });
|
||||
log('bee.del("' + key + '")', 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
document.getElementById('drivePut').onclick = async function () {
|
||||
const path = document.getElementById('drivePath').value || '/file.txt';
|
||||
const content = document.getElementById('driveContent').value;
|
||||
try {
|
||||
const base64 = btoa(unescape(encodeURIComponent(content || '')));
|
||||
await request('drivePut', { path, base64 });
|
||||
log('drive.put("' + path + '")', 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
document.getElementById('driveGet').onclick = async function () {
|
||||
const path = document.getElementById('drivePath').value || '/';
|
||||
try {
|
||||
const r = await request('driveGet', { path });
|
||||
const text = r.data ? decodeURIComponent(escape(atob(r.data))) : '(empty/null)';
|
||||
log('drive.get("' + path + '") => ' + text.slice(0, 80) + (text.length > 80 ? '…' : ''), 'val');
|
||||
document.getElementById('driveContent').value = r.data ? decodeURIComponent(escape(atob(r.data))) : '';
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
document.getElementById('driveList').onclick = async function () {
|
||||
try {
|
||||
const r = await request('driveList', { path: '/' });
|
||||
const keys = (r.entries || []).map(function (e) { return e.key; });
|
||||
log('drive.list("/") => ' + keys.join(', ') || '(none)', 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
document.getElementById('autobaseAppend').onclick = async function () {
|
||||
const value = document.getElementById('autobaseValue').value;
|
||||
try {
|
||||
await request('autobaseAppend', { value: value || '(empty)' });
|
||||
log('autobase.append("' + (value || '(empty)') + '")', 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
document.getElementById('autobaseInfo').onclick = async function () {
|
||||
try {
|
||||
const r = await request('autobaseInfo');
|
||||
log('Autobase length=' + r.length + ' signedLength=' + r.signedLength, 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
document.getElementById('autobaseView').onclick = async function () {
|
||||
try {
|
||||
const info = await request('autobaseInfo');
|
||||
const len = Math.min(info.length, 5);
|
||||
for (let i = 0; i < len; i++) {
|
||||
const r = await request('autobaseViewGet', { index: i });
|
||||
const text = r.data ? decodeURIComponent(escape(atob(r.data))) : null;
|
||||
log('view[' + i + '] = ' + (text || 'null'), 'val');
|
||||
}
|
||||
if (info.length === 0) log('view is empty', 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
|
||||
const collection = 'records';
|
||||
document.getElementById('hyperdbPut').onclick = async function () {
|
||||
const id = document.getElementById('hyperdbId').value;
|
||||
const value = document.getElementById('hyperdbValue').value;
|
||||
if (!id) return;
|
||||
try {
|
||||
await request('hyperdbInsert', { collection, doc: { id, value: value || '' } });
|
||||
log('hyperdb.insert(records, { id: "' + id + '", value: "' + (value || '') + '" })', 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
document.getElementById('hyperdbGet').onclick = async function () {
|
||||
const id = document.getElementById('hyperdbId').value;
|
||||
if (!id) return;
|
||||
try {
|
||||
const r = await request('hyperdbGet', { collection, query: { id } });
|
||||
log('hyperdb.get(records, { id: "' + id + '" }) => ' + (r.doc === null ? 'null' : JSON.stringify(r.doc)), 'val');
|
||||
if (r.doc) document.getElementById('hyperdbValue').value = r.doc.value || '';
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
document.getElementById('hyperdbDel').onclick = async function () {
|
||||
const id = document.getElementById('hyperdbId').value;
|
||||
if (!id) return;
|
||||
try {
|
||||
await request('hyperdbDelete', { collection, query: { id } });
|
||||
log('hyperdb.delete(records, { id: "' + id + '" })', 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
document.getElementById('hyperdbFind').onclick = async function () {
|
||||
try {
|
||||
const r = await request('hyperdbFindToArray', { collectionOrIndex: collection });
|
||||
const docs = r.docs || [];
|
||||
log('hyperdb.find(records) => ' + docs.length + ' doc(s) ' + JSON.stringify(docs), 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
document.getElementById('hyperdbFlush').onclick = async function () {
|
||||
try {
|
||||
await request('hyperdbFlush');
|
||||
log('hyperdb.flush()', 'val');
|
||||
} catch (e) { log(e.message, 'err'); }
|
||||
};
|
||||
}).catch(function () { log('BridgeSwarm not available.', 'err'); });
|
||||
})(0);
|
||||
})();
|
||||
Reference in New Issue
Block a user