File size: 13,371 Bytes
9391b1f
ef50eab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9391b1f
 
 
 
 
 
 
8f94222
 
9391b1f
 
 
8f94222
9391b1f
e3c88c2
f98edcd
 
 
e3c88c2
9391b1f
 
 
 
f98edcd
 
 
 
 
 
e3c88c2
f98edcd
 
 
 
 
e3c88c2
 
f98edcd
 
 
 
 
 
 
 
ef50eab
 
f98edcd
 
 
 
 
 
e3c88c2
 
 
f98edcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3c88c2
f98edcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3c88c2
f98edcd
e3c88c2
f98edcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3c88c2
 
 
f98edcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3c88c2
 
 
f98edcd
 
 
e3c88c2
ef50eab
 
e3c88c2
ef50eab
e3c88c2
 
 
 
 
 
f98edcd
 
 
 
 
 
 
 
e3c88c2
f98edcd
 
 
e3c88c2
 
 
 
f98edcd
e3c88c2
 
ef50eab
 
 
 
 
 
 
e3c88c2
 
 
 
 
 
 
 
 
 
 
ef50eab
 
e3c88c2
f98edcd
 
 
 
 
e3c88c2
 
 
f98edcd
ef50eab
e3c88c2
 
ef50eab
 
 
9391b1f
f98edcd
 
 
9391b1f
 
 
 
 
 
 
 
 
8f94222
9391b1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f94222
 
 
1bbe378
 
d37a434
 
f98edcd
 
 
1bbe378
 
 
 
9391b1f
 
 
 
 
 
 
 
 
 
 
 
e3c88c2
ef50eab
9391b1f
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
(() => {
  const MONTAIGNE_SUGGESTIONS = [
    {
      title: ['What occupies your thoughts', 'most lately?'],
      content: 'What occupies your thoughts most lately?'
    },
    {
      title: ['Which of your habits', 'deserve examination?'],
      content: 'Which of my habits deserve the closest examination, and how might I begin?'
    },
    {
      title: ['What do solitude and', 'conversation each reveal?'],
      content: "What do solitude and conversation each reveal about a person's character?"
    },
    {
      title: ['How have you changed', 'without noticing?'],
      content: 'How can someone recognize the ways they have changed without noticing?'
    },
    {
      title: ['Where does ambition', 'help or mislead you?'],
      content: 'Where does ambition improve a life, and where does it quietly mislead us?'
    },
    {
      title: ['What truth about yourself', 'is hardest to admit?'],
      content: 'What kinds of truths about ourselves are the hardest to admit, and why?'
    }
  ];

  const HIDE_TEXT = new Set([
    'Settings',
    'Archived Chats',
    'Keyboard shortcuts',
    'Update your status',
    'Playground',
    'Admin Panel',
    'Documentation',
    'Releases',
    'Set as default'
  ]);
  const HIDE_HREF = new Set(['/playground', '/admin']);
  const HIDE_ARIA = new Set(['Controls', 'Add Model', 'Remove Model', 'Voice Input', 'Voice mode']);

  const CHAT_COMPLETIONS_PATH = '/api/chat/completions';
  const GUEST_TOKEN_KEY = 'montaigne_guest_upgrade_token';
  const GUEST_CLAIM_URL_KEY = 'montaigne_guest_claim_url';
  const MONTAIGNE_TOKEN_KEY = 'montaigne_access_token';

  function normalize(text) {
    return (text || '').replace(/\s+/g, ' ').trim();
  }

  function removeGuestBanner() {
    const banner = document.getElementById('montaigne-guest-banner');
    if (banner) {
      banner.remove();
    }
  }

  function closeGuestModal() {
    const modal = document.getElementById('montaigne-guest-modal');
    if (modal) {
      modal.remove();
    }
  }

  async function submitGuestClaim(form, statusEl) {
    const guestUpgradeToken = localStorage.getItem(GUEST_TOKEN_KEY);
    const claimUrl = localStorage.getItem(GUEST_CLAIM_URL_KEY);
    const username = form.querySelector('[name="username"]').value.trim();
    const password = form.querySelector('[name="password"]').value;

    if (!guestUpgradeToken || !claimUrl) {
      statusEl.textContent = 'This guest session can no longer be claimed. Please refresh and try again.';
      return;
    }
    if (username.length < 3) {
      statusEl.textContent = 'Choose a username with at least 3 characters.';
      return;
    }
    if (password.length < 6) {
      statusEl.textContent = 'Choose a password with at least 6 characters.';
      return;
    }

    statusEl.textContent = 'Creating your account...';

    try {
      const response = await fetch(claimUrl, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          guest_upgrade_token: guestUpgradeToken,
          username,
          password
        })
      });
      const payload = await response.json();
      if (!response.ok) {
        throw new Error(payload.detail || 'Could not create your account.');
      }

      localStorage.setItem(MONTAIGNE_TOKEN_KEY, payload.access_token);
      localStorage.removeItem(GUEST_TOKEN_KEY);
      localStorage.removeItem(GUEST_CLAIM_URL_KEY);
      closeGuestModal();
      removeGuestBanner();

      const toast = document.createElement('div');
      toast.style.cssText =
        'position:fixed;bottom:18px;left:50%;transform:translateX(-50%);z-index:100000;' +
        'background:#16311f;color:#e6f6ea;padding:10px 14px;border-radius:10px;' +
        'font:13px Inter,system-ui,sans-serif;box-shadow:0 10px 30px rgba(0,0,0,.25);';
      toast.textContent = 'Account created. Send your next message to continue.';
      document.body.appendChild(toast);
      setTimeout(() => toast.remove(), 4000);
    } catch (error) {
      statusEl.textContent = error instanceof Error ? error.message : 'Could not create your account.';
    }
  }

  function openGuestModal() {
    closeGuestModal();

    const overlay = document.createElement('div');
    overlay.id = 'montaigne-guest-modal';
    overlay.style.cssText =
      'position:fixed;inset:0;z-index:100000;display:flex;align-items:center;justify-content:center;' +
      'background:rgba(22,16,12,.62);padding:20px;';

    const card = document.createElement('div');
    card.style.cssText =
      'width:min(420px,100%);border-radius:18px;padding:22px;background:#f5ede0;color:#2b2118;' +
      'box-shadow:0 20px 60px rgba(0,0,0,.35);font:14px Inter,system-ui,sans-serif;';

    const title = document.createElement('h2');
    title.textContent = 'Create your account';
    title.style.cssText = 'margin:0 0 8px;font:600 22px Georgia,serif;';

    const intro = document.createElement('p');
    intro.textContent = 'Keep this conversation and continue chatting with Montaigne.';
    intro.style.cssText = 'margin:0 0 16px;line-height:1.45;color:#5b4a3c;';

    const form = document.createElement('form');
    form.style.cssText = 'display:grid;gap:12px;';

    const username = document.createElement('input');
    username.name = 'username';
    username.placeholder = 'Username';
    username.autocomplete = 'username';
    username.style.cssText =
      'width:100%;padding:12px 14px;border:1px solid #cfb89b;border-radius:10px;background:#fffdf9;';

    const password = document.createElement('input');
    password.name = 'password';
    password.type = 'password';
    password.placeholder = 'Password';
    password.autocomplete = 'new-password';
    password.style.cssText = username.style.cssText;

    const status = document.createElement('div');
    status.style.cssText = 'min-height:18px;font-size:13px;color:#8a2f2f;';

    const actions = document.createElement('div');
    actions.style.cssText = 'display:flex;justify-content:flex-end;gap:10px;margin-top:4px;';

    const cancel = document.createElement('button');
    cancel.type = 'button';
    cancel.textContent = 'Cancel';
    cancel.style.cssText =
      'padding:10px 14px;border:1px solid #cfb89b;border-radius:10px;background:#fff8ef;cursor:pointer;';
    cancel.onclick = () => closeGuestModal();

    const submit = document.createElement('button');
    submit.type = 'submit';
    submit.textContent = 'Create account';
    submit.style.cssText =
      'padding:10px 14px;border:none;border-radius:10px;background:#8b6243;color:#fff;cursor:pointer;font-weight:600;';

    form.addEventListener('submit', async (event) => {
      event.preventDefault();
      await submitGuestClaim(form, status);
    });

    actions.appendChild(cancel);
    actions.appendChild(submit);
    form.appendChild(username);
    form.appendChild(password);
    form.appendChild(status);
    form.appendChild(actions);

    card.appendChild(title);
    card.appendChild(intro);
    card.appendChild(form);
    overlay.appendChild(card);
    overlay.addEventListener('click', (event) => {
      if (event.target === overlay) {
        closeGuestModal();
      }
    });
    document.body.appendChild(overlay);
    username.focus();
  }

  function showGuestBanner() {
    if (document.getElementById('montaigne-guest-banner')) {
      return;
    }

    const banner = document.createElement('div');
    banner.id = 'montaigne-guest-banner';
    banner.style.cssText =
      'position:fixed;bottom:0;left:0;right:0;z-index:99999;background:linear-gradient(135deg,#3d2b1f,#5c3d2e);' +
      'color:#f5e6d3;padding:14px 20px;display:flex;align-items:center;justify-content:center;gap:16px;' +
      'font:14px Inter,system-ui,sans-serif;box-shadow:0 -2px 12px rgba(0,0,0,.3);';

    const message = document.createElement('span');
    message.textContent = 'You have used your 5 free messages. Create a free account to continue chatting with Montaigne.';

    const button = document.createElement('button');
    button.type = 'button';
    button.textContent = 'Create free account';
    button.style.cssText =
      'background:#c9a96e;color:#2b1d0e;padding:8px 18px;border:none;border-radius:6px;' +
      'font-weight:600;white-space:nowrap;cursor:pointer;';
    button.onclick = () => openGuestModal();

    const close = document.createElement('button');
    close.type = 'button';
    close.textContent = 'x';
    close.style.cssText =
      'background:none;border:none;color:#f5e6d3;font-size:20px;cursor:pointer;padding:0 4px;line-height:1;';
    close.onclick = () => removeGuestBanner();

    banner.appendChild(message);
    banner.appendChild(button);
    banner.appendChild(close);
    document.body.appendChild(banner);
  }

  function installFetchPatch() {
    if (window.__montaigneFetchPatched) {
      return;
    }
    window.__montaigneFetchPatched = true;

    const originalFetch = window.fetch.bind(window);

    window.fetch = async (...args) => {
      try {
        const input = args[0];
        const rawUrl = input instanceof Request ? input.url : String(input);
        const url = new URL(rawUrl, window.location.origin);

        if (url.pathname === CHAT_COMPLETIONS_PATH) {
          const body = args[1] && args[1].body;
          if (typeof body === 'string') {
            try {
              const parsed = JSON.parse(body);
              parsed.variables = parsed.variables || {};
              const montaigneToken = localStorage.getItem(MONTAIGNE_TOKEN_KEY);
              if (montaigneToken) {
                parsed.variables.montaigne_token = montaigneToken;
              }
              args[1] = { ...args[1], body: JSON.stringify(parsed) };
            } catch (_) {
              // Ignore non-JSON payloads.
            }
          }
        }
      } catch (_) {
        // Ignore URL parsing errors.
      }

      const response = await originalFetch(...args);

      try {
        const input = args[0];
        const rawUrl = input instanceof Request ? input.url : String(input);
        const url = new URL(rawUrl, window.location.origin);

        if (url.pathname === '/api/config') {
          const payload = await response.clone().json();
          payload.default_prompt_suggestions = MONTAIGNE_SUGGESTIONS;
          const headers = new Headers(response.headers);
          headers.set('content-type', 'application/json');
          headers.delete('content-length');
          return new Response(JSON.stringify(payload), {
            status: response.status,
            statusText: response.statusText,
            headers
          });
        }

        if (url.pathname === CHAT_COMPLETIONS_PATH && response.status === 403) {
          const payload = await response.clone().json().catch(() => null);
          if (payload && payload.code === 'guest_limit_reached' && payload.guest_upgrade_token && payload.claim_guest_url) {
            localStorage.setItem(GUEST_TOKEN_KEY, payload.guest_upgrade_token);
            localStorage.setItem(GUEST_CLAIM_URL_KEY, payload.claim_guest_url);
            showGuestBanner();
          }
        }
      } catch (_) {
        // Keep the UI resilient.
      }

      return response;
    };
  }

  function hide(el) {
    if (!el || el.dataset.montaigneHidden === '1') {
      return;
    }
    el.dataset.montaigneHidden = '1';
    el.style.display = 'none';
  }

  function hideMenuItem(el) {
    hide(el.closest('button, a, [role="menuitem"]') || el);
  }

  function hideModelSelector(trigger) {
    hide(trigger);
    let node = trigger;
    for (let i = 0; i < 6 && node; i += 1) {
      const className = typeof node.className === 'string' ? node.className : '';
      if (className.includes('overflow-hidden')) {
        hide(node.parentElement || node);
        return;
      }
      node = node.parentElement;
    }
    hide(trigger);
  }

  function applyMinimalUi() {
    document
      .querySelectorAll('[id^="model-selector-"][id$="-button"]')
      .forEach(hideModelSelector);

    document.querySelectorAll('#voice-input-button, [aria-label="Voice mode"]').forEach(hide);
    document.querySelectorAll('[aria-label^="Get information on "]').forEach(hide);

    document.querySelectorAll('[role="dialog"], dialog').forEach((el) => {
      const text = normalize(el.textContent);
      if (text.includes("What's New") || text.includes('Release Notes')) {
        const closeButton = el.querySelector('button[aria-label="Close"]');
        if (closeButton) {
          closeButton.click();
        }
        hide(el);
      }
    });

    document.querySelectorAll('button, a, [role="menuitem"]').forEach((el) => {
      const text = normalize(el.textContent);
      const aria = normalize(el.getAttribute('aria-label'));
      const href = el.getAttribute('href');
      if (HIDE_TEXT.has(text) || HIDE_ARIA.has(aria) || HIDE_HREF.has(href)) {
        hideMenuItem(el);
      }
    });

    document.querySelectorAll('#temporary-chat-button').forEach(hide);
  }

  installFetchPatch();

  const run = () => window.requestAnimationFrame(applyMinimalUi);

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', run, { once: true });
  } else {
    run();
  }

  new MutationObserver(run).observe(document.documentElement, {
    childList: true,
    subtree: true
  });
})();