Files
Febe/tools/Febe.SitoDiProva/PageModules.cs
Alby96andClaude Opus 5.5 883ea4683c Sessione salvata: copia alla chiusura della finestra e alla fine della guida, «Serve «Ho finito»», scelta per la copia incompleta e stato in Collegamento (#36, S4b)
Risposte 1, 5, 6, 9 e 10 di Alberto a S4. I valori __spin_r, __spin_b e __spin_t si copiano dalla pagina come gli altri (su Instagram da SiteData, ipotesi H23, operazioni ancora non configurate; nel sito di prova dal modulo GiroProva). La sessione si copia anche quando si chiude la finestra del browser su Windows e quando il permesso di guida finisce, sempre prima che la pagina torni al motore e con le garanzie di «Ho finito». Con «richieste» «Collegato» vuole una sessione salvata leggibile, altrimenti lo stato è «Serve «Ho finito»» e il motore aspetta. Nuova impostazione sessione.copia-incompleta (sostituisci, predefinita, o tieni-completa) e riquadro della sessione salvata nella pagina Collegamento, con soli conteggi e nomi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 11:41:35 +01:00

102 lines
5.1 KiB
C#

using System.Globalization;
using System.Text;
using System.Text.Json.Nodes;
namespace Febe.SitoDiProva;
/// <summary>
/// The modules of the pages (issue #36, S4): a tiny registry of its own (<c>__d</c> to declare a module,
/// <c>require</c> to read it, an error for an unknown name) with the values a real site's page holds and the copy at
/// «Ho finito» reads — tokens, account, version fields, the doc_id of each GraphQL query. The names are the test
/// site's own (the <c>page</c> section of <c>richieste.prova.json</c>), never Instagram's; the values are the
/// constants the site checks in <c>/api/</c>.
/// </summary>
public sealed partial class TestSite
{
/// <summary>The suffix of the module that exports the doc_id of a GraphQL query: <c>ProvaProfiloQuery_operazioneProva</c>.</summary>
public const string DocIdModuleSuffix = "_operazioneProva";
/// <summary>
/// What the pages declare, by module and property, as text (numbers as the page would turn them into text); the
/// doc_id modules export the value itself (property <c>""</c>).
/// </summary>
/// <param name="accountId">The id of the account the pages show as connected (<see cref="World.Viewer"/>).</param>
public static IReadOnlyDictionary<string, IReadOnlyDictionary<string, string>> PageModules(string accountId)
{
var modules = new Dictionary<string, IReadOnlyDictionary<string, string>>(StringComparer.Ordinal);
foreach (var (name, module) in Modules(accountId))
{
modules[name] = module is JsonObject properties
? properties.ToDictionary(p => p.Key, p => Text(p.Value), StringComparer.Ordinal)
: new Dictionary<string, string>(StringComparer.Ordinal) { [string.Empty] = Text(module) };
}
return modules;
}
/// <summary>The inline script of the registry, for the head of every page but the verification and session-ended ones.</summary>
internal static string ModuleRegistryScript(string accountId)
{
var text = new StringBuilder();
text.Append("<script>(function () {\n");
text.Append(" var declared = {}, made = {};\n");
text.Append(" var own = function (o, k) { return Object.prototype.hasOwnProperty.call(o, k); };\n");
text.Append(" window.__d = function (name, deps, factory) { declared[name] = factory; };\n");
text.Append(" window.require = function (name) {\n");
text.Append(" if (own(made, name)) { return made[name].exports; }\n");
text.Append(" if (!own(declared, name)) { throw new Error('Modulo sconosciuto: ' + name); }\n");
text.Append(" var module = { exports: {} };\n");
text.Append(" made[name] = module;\n");
text.Append(" declared[name](module);\n");
text.Append(" return module.exports;\n");
text.Append(" };\n");
foreach (var (name, module) in Modules(accountId))
{
// JSON text in a script: the serializer escapes «<», so no value can close the element.
text.Append(CultureInfo.InvariantCulture, $" __d({JsonValue.Create(name).ToJsonString()}, [], function (module) {{ module.exports = {module!.ToJsonString()}; }});\n");
}
text.Append("})();</script>");
return text.ToString();
}
/// <summary>Puts <paramref name="script"/> first in the head of <paramref name="html"/>.</summary>
internal static string InsertAfterHead(string html, string script)
{
var at = html.IndexOf("<head>", StringComparison.Ordinal);
return at < 0 ? html : html.Insert(at + "<head>".Length, script);
}
private static IEnumerable<(string Name, JsonNode Module)> Modules(string accountId)
{
yield return ("GettoniProva", new JsonObject { ["lsd"] = Lsd, ["dtsg"] = Dtsg });
yield return ("UtenteProva", new JsonObject { ["id"] = accountId, ["applicazione"] = AppId });
yield return ("VersioneProva", new JsonObject
{
["revisione"] = Revision,
["hs"] = HasteSession,
["hsi"] = HasteSessionId,
// Numbers in the page, as a real page keeps them: the copy turns them into text.
["dpr"] = int.Parse(Dpr, CultureInfo.InvariantCulture),
["comet"] = int.Parse(CometReq, CultureInfo.InvariantCulture),
});
yield return ("ConnessioneProva", new JsonObject { ["classe"] = ConnectionClass });
yield return ("AvvioProva", new JsonObject { ["nonce"] = EntryNonce });
yield return ("GiroProva", new JsonObject
{
["revisione"] = int.Parse(SpinRevision, CultureInfo.InvariantCulture),
["ramo"] = SpinBranch,
["tempo"] = int.Parse(SpinTime, CultureInfo.InvariantCulture),
});
foreach (var (operation, docId) in DocIds.OrderBy(d => d.Key, StringComparer.Ordinal))
{
yield return (operation + DocIdModuleSuffix, JsonValue.Create(docId));
}
}
private static string Text(JsonNode? node) => node is JsonValue value && value.TryGetValue<int>(out var number)
? number.ToString(CultureInfo.InvariantCulture)
: node?.GetValue<string>() ?? string.Empty;
}