function markReplay(pageUrl) { return fetch(pageUrl, { credentials: 'same-origin' }) .then(function (r) { return r.text(); }) .then(function (html) { var m = html.match(/name="csrf-token"[^>]*content="([^"]+)"/) || html.match(/content="([^"]+)"[^>]*name="csrf-token"/); if (!m) return false; var post = function () { var fd = new FormData(); fd.append('authenticity_token', m[1]); return fetch(pageUrl + '/watch', { method: 'POST', body: fd, credentials: 'same-origin', headers: { 'Accept': 'text/vnd.turbo-stream.html, text/html' } }); }; return post().then(function (r2) { return r2.text().then(function (t) { if (!r2.ok) return false; // la risposta contiene lo stato NUOVO: "false" = era gia' visto e // l'abbiamo smarcato -> secondo POST per ripristinare if (t.indexOf('data-watched="false"') !== -1) return post().then(function () { return true; }); return true; }); }); }) .catch(function () { return false; }); }