Onche - Auto-refresh topic

Clique automatiquement sur la barre "Nouveaux messages" d'Onche, sans changer de page tout seul

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Onche - Auto-refresh topic
// @namespace    https://greasyfork.org/users/onche-autorefresh
// @version      2.6
// @description  Clique automatiquement sur la barre "Nouveaux messages" d'Onche, sans changer de page tout seul
// @author       toi
// @match        https://onche.org/topic/*
// @icon         https://onche.org/icons/favicon-32x32.png
// @grant        none
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  let paused = false;
  let cooldown = false;

  const badge = document.createElement('div');
  badge.style.cssText =
    'position:fixed;bottom:16px;right:16px;z-index:9999;padding:8px 12px;' +
    'border-radius:8px;font:13px Inter,sans-serif;background:#1b6ec2;color:#fff;' +
    'cursor:pointer;box-shadow:0 2px 8px rgba(0,0,0,.4);user-select:none;';
  badge.textContent = '⟳ Auto ON';
  badge.title = 'Cliquer pour mettre en pause / reprendre';
  badge.addEventListener('click', () => {
    paused = !paused;
    badge.textContent = paused ? '⏸ Auto OFF' : '⟳ Auto ON';
    badge.style.background = paused ? '#555' : '#1b6ec2';
  });
  document.body.appendChild(badge);

  // --- Option A : on n'agit que sur la DERNIÈRE page, pour ne jamais sauter de page tout seul ---
  function isLastPage() {
    const topic = document.querySelector('#topic');
    if (!topic) return true; // dans le doute, on laisse faire
    const page = parseInt(topic.dataset.page, 10);
    const nbPages = parseInt(topic.dataset.nbPages, 10);
    if (isNaN(page) || isNaN(nbPages)) return true;
    return page === nbPages;
  }

  function isShown(el) {
    return el && el.offsetParent !== null;
  }

  // Cherche l'élément cliquable "Nouveaux messages", peu importe l'indicateur de saisie à côté
  function findBar() {
    const ind = document.querySelector('#new-messages-indicator');
    if (
      isShown(ind) &&
      !ind.classList.contains('indicator-hidden') &&
      /nouveau/i.test(ind.textContent)
    ) {
      return ind;
    }
    const all = document.querySelectorAll('#left *');
    let best = null;
    for (const el of all) {
      if (!isShown(el)) continue;
      const txt = (el.textContent || '').trim();
      if (/nouveau[x]?\s+message/i.test(txt) && txt.length < 40) {
        best = el;
      }
    }
    return best;
  }

  function tick() {
    if (paused || cooldown) return;
    if (!isLastPage()) {
      // Sur une page intermédiaire : on n'auto-clique pas (sinon saut de page)
      badge.textContent = '⏯ Pas dernière page';
      badge.style.background = '#7a6a1b';
      return;
    } else if (badge.textContent === '⏯ Pas dernière page') {
      badge.textContent = '⟳ Auto ON';
      badge.style.background = '#1b6ec2';
    }

    const bar = findBar();
    if (!bar) return;

    const target =
      bar.querySelector('a, button, [class*="clickable"]') || bar;
    target.click();

    cooldown = true;
    setTimeout(() => (cooldown = false), 300);

    badge.textContent = '↻ Chargé';
    badge.style.background = '#2e9e4f';
    setTimeout(() => {
      if (!paused && badge.textContent === '↻ Chargé') {
        badge.textContent = '⟳ Auto ON';
        badge.style.background = '#1b6ec2';
      }
    }, 1000);
  }

  setInterval(tick, 400);
})();