Onche - Auto-refresh topic

Clique automatiquement sur la barre "Nouveaux messages" d'Onche

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Onche - Auto-refresh topic
// @namespace    onche.autorefresh
// @version      2.0
// @description  Clique automatiquement sur la barre "Nouveaux messages" d'Onche
// @match        https://onche.org/topic/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';

  const INTERVAL = 3000; // vérifie toutes les 3s si la barre est apparue

  let paused = false;

  // --- Indicateur de statut ---
  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);

  function clickNewMessages() {
    if (paused || document.hidden) return;

    // L'indicateur de nouveaux messages d'Onche
    const indicator = document.querySelector('#new-messages-indicator');
    if (!indicator) return;

    // Visible seulement quand il y a du nouveau
    const visible =
      !indicator.classList.contains('indicator-hidden') &&
      indicator.offsetParent !== null &&
      indicator.textContent.trim().length > 0;

    if (visible) {
      // On clique sur l'élément cliquable à l'intérieur (ou l'indicateur lui-même)
      const target =
        indicator.querySelector('a, button, [class*="clickable"], div') ||
        indicator;
      target.click();

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

  setInterval(clickNewMessages, INTERVAL);
})();