diff --git a/CHANGELOG.md b/CHANGELOG.md index b9dc4b2..24085c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## 6.13.0 + +- Add: a live sync status next to the JavStore controls, so you can tell when a sync is still running and wait before opening new tabs. A tab opened during a long sync starts from the history as it was before the sync, and on AdGuard it then downloads the whole history again itself. + - **Downloading history… N visits** while this tab downloads a large history, such as the first sync after an update, with the count so far. + - **Syncing… N visits** while a longer sync runs. A quick sync shows nothing. + - **Waiting for another tab to sync…** while this tab's sync waits for another tab's to finish. + - **Another tab is syncing…** in the tab you are looking at, while any other JavStore tab syncs. + - **Sync complete** for a few seconds once a sync you could see finishes: new tabs opened from then on start from the downloaded history. +- How it works: AdGuard does not let a tab read what another tab saved, but every JavStore tab shares the sync's Web Lock, and the browser can say whether that lock is held. Only the tab in view asks, every two seconds, and nothing is stored or sent anywhere. Browsers without Web Locks show only the tab's own sync. +- Tapping the status opens the settings panel. Hover over it for why waiting helps. +- The worker version moves to 6.13.0 with the userscript; the worker is unchanged. + ## 6.12.0 A review of the whole history and sync design against how AdGuard actually stores userscript data. AdGuard for Mac, Windows and Android give each tab the stored values as they were when the tab loaded, and a tab never sees what other tabs save after that. The script had been designed as if every tab read the same, current storage. With many tabs left open, that broke in several ways, each now covered by a test that reproduces it. diff --git a/README.md b/README.md index 13933f9..d15795f 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ Once it is deployed, open the `JVS` panel → **Cloud sync**, enter the worker U - The Cloud sync panel shows the worker version from the worker's last answer. When the worker is behind the userscript, a **⚠ Redeploy worker** button appears on screen next to the JavStore controls; tap it for the versions. The worker redeploys through Cloudflare whenever `main` changes, so a lasting warning means its Cloudflare build did not go through. When the worker is ahead instead, an **⚠ Update script** button appears on screen and links to the latest release, because the userscript manager has not installed it yet. - AdGuard for Android clears a userscript's storage whenever it installs an update. After setting sync up, press **Copy sync link** and keep the link somewhere private, such as a password manager. After a wipe, open it on JavStore, or paste it into the endpoint box and save, to set everything up again. The link carries your token, so treat it like a password. - The endpoint and token live only on the device they were entered on. They are never written into the synced document, never appear in an exported backup, and are not left in the page: the token box stays empty once a token is stored and only reports that one exists. +- A status next to the JavStore controls shows a sync that is still running: **Downloading history…** with the number of visits so far, **Waiting for another tab to sync…**, **Another tab is syncing…** (found through the sync's shared Web Lock, which needs no storage), and **Sync complete** when it is done. A tab opened during a long download starts without what it downloads and fetches it again itself, so it is worth waiting for **Sync complete** before opening many tabs, especially after an update. - A worker that is unreachable does not affect anything locally: history is still saved to the userscript manager, and the next sync picks it up. Failed or incomplete restoration is not marked complete. If local history is actually lost, recovery bypasses the session marker and downloads it again; **Restored … visited URLs** reports that recovery, not ordinary page navigation. A history that is merely behind catches up on the difference without the note. - Current browsers coordinate cloud work with Web Locks, and no tab holds them for more than five minutes. Managers that notify tabs of each other's writes fall back to a userscript-storage lease where Web Locks are missing; elsewhere tabs sync side by side, which costs at most a duplicate request. Session metadata stays in userscript-manager storage; credentials and history are not placed in page `localStorage`. - **Sync now** reports both directions—what went up and what came down—so a device that is failing to push is not mistaken for one with nothing to push. diff --git a/javstore-full-layout-cleanup.user.js b/javstore-full-layout-cleanup.user.js index aefc20d..dbb426f 100644 --- a/javstore-full-layout-cleanup.user.js +++ b/javstore-full-layout-cleanup.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name JavStore Full Layout Cleanup - No Sidebars + Mosaic Overlay // @namespace http://tampermonkey.net/ -// @version 6.12.0 +// @version 6.13.0 // @description Clean up JavStore's layout, filter keyword-matched thumbnails, and track visited items with private, configurable controls. // @homepageURL https://github.com/jitdor/userscript-javstore // @supportURL https://github.com/jitdor/userscript-javstore/issues @@ -29,7 +29,7 @@ (function () { 'use strict'; - const SCRIPT_VERSION = '6.12.0'; + const SCRIPT_VERSION = '6.13.0'; // The same address as @downloadURL: opening it hands the newest release to the // userscript manager, which offers to install it. const INSTALL_URL = 'https://github.com/jitdor/userscript-javstore/releases/latest/download/javstore-full-layout-cleanup.user.js'; @@ -69,6 +69,7 @@ const SYNC_LEASE_KEY = 'javstore_sync_lease_v1'; const SYNC_SESSION_IDLE_MS = 30 * 60000; const SYNC_LEASE_MS = 90000; + const SYNC_LOCK_NAME = 'javstore-cloud-sync-v1'; // The longest one exchange may hold the sync lock. Its requests time out long before; // this is for whatever else could hang, so one stuck tab cannot stop every tab syncing. const SYNC_HOLD_MS = 5 * 60000; @@ -196,6 +197,21 @@ let remoteWorkerVersion = ''; let lastRemoteError = ''; let remoteSyncRunning = false; + // What the on-page sync status shows. A tab cannot read another tab's progress from + // storage on AdGuard, but every tab of the site shares the sync's Web Lock, and asking + // the browser whether it is held needs no storage at all. + const syncView = { + waitingSince: 0, // this tab's sync is queued behind another tab's + exchangeSince: 0, // this tab is talking to the worker + rows: 0, // rows this exchange has downloaded so far + downloading: false, // more of the history is still to come + otherTab: false, // another tab holds the sync lock + heldPolls: 0, + shown: false, + doneUntil: 0, + timer: 0, + pollTimer: 0, + }; let applyingRemoteState = false; // A merge can absorb protective metadata—a tombstone, a newer `resetAt`, `prunedBefore` // or `settingsUpdatedAt`—without removing anything that is on screen, so @@ -1855,7 +1871,18 @@ // Syncs are queued for the same reason writes are: two overlapping merge cycles would // each build on a snapshot the other has already moved past. function queueSync(options = {}) { - const run = () => withSyncLock(() => runSync(options)); + const run = () => { + const queuedAt = Date.now(); + syncView.waitingSince = queuedAt; + refreshLiveSync(); + return withSyncLock(() => { + syncView.waitingSince = 0; + return runSync(options); + }).finally(() => { + if (syncView.waitingSince === queuedAt) syncView.waitingSince = 0; + refreshLiveSync(); + }); + }; remoteQueue = remoteQueue.then(run, run).catch(error => { lastRemoteError = String(error?.message || 'sync failed'); if (options.manual) showToast(`Sync failed: ${lastRemoteError}`, true); @@ -1877,7 +1904,7 @@ // cannot stop every other tab from syncing. Its requests time out well before. const bounded = () => withTimeout(action(), SYNC_HOLD_MS, 'the sync took too long'); if (navigator.locks?.request) { - return navigator.locks.request('javstore-cloud-sync-v1', async () => { + return navigator.locks.request(SYNC_LOCK_NAME, async () => { if (pageClosed) return false; return bounded(); }); @@ -2013,6 +2040,10 @@ let downloadComplete = false; let uploadedMeta = ''; let restarted = false; + syncView.exchangeSince = Date.now(); + syncView.rows = 0; + syncView.downloading = reconciling; + refreshLiveSync(); for (let page = 0; page < MAX_SYNC_PAGES; page += 1) { const batch = outgoing.slice(0, REMOTE_PAGE_ROWS); @@ -2089,6 +2120,9 @@ if (lastMergeAdopted) absorbed = true; applyingRemoteState = false; downloadComplete = !answer.more; + syncView.rows += Array.isArray(answer.changes) ? answer.changes.length : 0; + syncView.downloading = Boolean(answer.more); + refreshLiveSync(); if (reconciling && downloadComplete) { reconciling = false; outgoing = reconcileWith(pulledAt, tag); @@ -2147,6 +2181,14 @@ if (manual) showToast(`Sync failed: ${lastRemoteError}`, true); return false; } finally { + if (syncView.exchangeSince) { + syncView.exchangeSince = 0; + syncView.downloading = false; + // A sync the user saw running says when it is over, so they know new tabs + // will start from the downloaded history. + if (syncView.shown) syncView.doneUntil = Date.now() + 3000; + refreshLiveSync(); + } applyingRemoteState = false; remoteSyncRunning = false; if (deferredRemotePush) { @@ -2243,6 +2285,82 @@ return `synced at ${new Date(lastRemoteSyncAt).toLocaleTimeString()}`; } + // Shown only once a sync has taken longer than a moment, so quick syncs do not flicker. + const LIVE_SYNC_DELAY_MS = 600; + + function describeLiveSync(now = Date.now()) { + if (!syncConfigured()) return null; + const rows = syncView.rows ? ` ${syncView.rows.toLocaleString()} visits` : ''; + if (syncView.exchangeSince && now - syncView.exchangeSince >= LIVE_SYNC_DELAY_MS) { + return syncView.downloading + ? { text: `Downloading history…${rows}`, busy: true, hint: true } + : { text: `Syncing…${rows}`, busy: true, hint: false }; + } + if (syncView.waitingSince && now - syncView.waitingSince >= LIVE_SYNC_DELAY_MS) { + return { text: 'Waiting for another tab to sync…', busy: true, hint: true }; + } + if (syncView.otherTab) return { text: 'Another tab is syncing…', busy: true, hint: true }; + if (syncView.doneUntil > now) return { text: 'Sync complete', busy: false, hint: false }; + return null; + } + + function renderLiveSync() { + if (!ui?.syncStatus) return; + const now = Date.now(); + const status = describeLiveSync(now); + syncView.shown = Boolean(status?.busy); + ui.syncStatus.hidden = !status; + if (!status) return; + ui.syncStatus.textContent = status.text; + ui.syncStatus.classList.toggle('busy', status.busy); + ui.syncStatus.title = status.hint + ? 'Tabs opened before this finishes start without what it downloads, and download it again themselves. Wait for "Sync complete" before opening new tabs.' + : ''; + } + + // Renders now, and again once the delay has passed or the "complete" note has run out. + function refreshLiveSync() { + renderLiveSync(); + window.clearTimeout(syncView.timer); + const now = Date.now(); + const pending = [ + syncView.exchangeSince && syncView.exchangeSince + LIVE_SYNC_DELAY_MS, + syncView.waitingSince && syncView.waitingSince + LIVE_SYNC_DELAY_MS, + syncView.doneUntil, + ].filter(at => at && at > now); + if (pending.length) syncView.timer = window.setTimeout(refreshLiveSync, Math.min(...pending) - now + 20); + } + + // Whether another tab holds the sync lock, asked of the browser every couple of seconds + // while this tab is in view. A lock held for a moment is an ordinary quick sync, so it + // only counts once two checks in a row find it held. + async function pollOtherTabSync() { + if (!syncConfigured() || document.visibilityState === 'hidden' || typeof navigator.locks?.query !== 'function') { + syncView.heldPolls = 0; + } else { + try { + const { held = [] } = await navigator.locks.query(); + const ours = remoteSyncRunning || syncView.waitingSince; + const heldElsewhere = !ours && held.some(lock => lock.name === SYNC_LOCK_NAME); + syncView.heldPolls = heldElsewhere ? syncView.heldPolls + 1 : 0; + } catch (error) { + syncView.heldPolls = 0; + } + } + const otherTab = syncView.heldPolls >= 2; + if (otherTab !== syncView.otherTab) { + if (syncView.otherTab && !otherTab) syncView.doneUntil = Date.now() + 3000; + syncView.otherTab = otherTab; + refreshLiveSync(); + } + } + + function startOtherTabPolling() { + window.clearInterval(syncView.pollTimer); + if (typeof navigator.locks?.query !== 'function') return; + syncView.pollTimer = window.setInterval(pollOtherTabSync, 2000); + } + settings = sanitizeSettings(); visited = new Map(); overrides = new Map(); @@ -3012,7 +3130,7 @@ min-height: 40px; border: 1px solid rgba(255,255,255,.2); border-radius: 999px; color: #fff; background: #111827; box-shadow: 0 8px 24px rgba(0,0,0,.28); } - .pill { padding: 8px 14px; } + .pill { padding: 8px 14px; white-space: nowrap; } .icon-button { width: 40px; padding: 0; font-size: 18px; } button:hover { background: #1f2937; } button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { @@ -3034,6 +3152,14 @@ .warning { padding: 8px; border-radius: 8px; color: #fecaca; background: #7f1d1d; } .warning a { color: #fff; } .pill.update, .pill.redeploy { border-color: #fca5a5; background: #991b1b; } + .pill.sync-status { border-color: #86efac; background: #166534; } + .pill.sync-status.busy { border-color: #93c5fd; background: #1e3a8a; } + .pill.sync-status.busy::before { + content: ''; display: inline-block; width: 10px; height: 10px; margin-right: 8px; + border: 2px solid #bfdbfe; border-right-color: transparent; border-radius: 50%; + vertical-align: -1px; animation: jvs-spin .8s linear infinite; + } + @keyframes jvs-spin { to { transform: rotate(360deg); } } .pill.update:hover, .pill.redeploy:hover { background: #7f1d1d; } .pill[hidden], .warning[hidden] { display: none; } .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; } @@ -3065,16 +3191,27 @@ .toast.error { background: #991b1b; } .toast.show { opacity: 1; transform: translateY(0); } @media (max-width: 520px) { - .dock { right: 10px; bottom: 10px; } + /* On a phone the controls wrap rather than run off the left edge, and the + sync status takes a row of its own above them so it is always readable. */ + .dock { + right: 10px; bottom: 10px; left: 10px; flex-wrap: wrap; justify-content: flex-end; + pointer-events: none; + } + .dock > * { pointer-events: auto; } + .pill.sync-status { order: -1; flex: 1 0 100%; text-align: center; } .panel { right: 10px; bottom: 62px; width: calc(100vw - 20px); max-height: calc(100vh - 76px); } .grid { grid-template-columns: 1fr; } .full { grid-column: auto; } } - @media (prefers-reduced-motion: reduce) { .toast { transition: none; } } + @media (prefers-reduced-motion: reduce) { + .toast { transition: none; } + .pill.sync-status.busy::before { animation: none; } + }