corrigé

Anonymous
JavaScript
09/29/2026 6:52 PM
5.3 KB
25
No Index
Never
// ==UserScript==
// @name        Auto-ajoute un espace invisible JVC
// @match       https://www.jeuxvideo.com/forums/*.htm
// @grant       GM_addStyle
// @noframes
// ==/UserScript==

(() => {
  'use strict';

  GM_addStyle(`
    .container__postTitle{
    display:flex!important;
    }
    #charCountDisplay_us{
    margin-left:auto!important;
    font-size: .8625rem!important;
    font-weight: 400!important;
    }
    #charCountDisplay_us.errorExceeded_us{
    color:red!important;
    }
    .container__post:has(#charCountDisplay_us.errorExceeded_us) #message_reponse{
    border:2px solid red !important;
    }
  `);

  const charCountDisplay = document.createElement("span");
  charCountDisplay.textContent = "0/16 000";
  charCountDisplay.id = "charCountDisplay_us";

  const updateCharacterCount = (inputElement) => {
    const length = inputElement.value.length;
    charCountDisplay.textContent = length + "/16 000"
    if(length > 16000){
      charCountDisplay.classList.add("errorExceeded_us");
    } else {
      charCountDisplay.classList.remove("errorExceeded_us");
    }
  }

  const emojiPatterns = [
    ":)",":snif:",":gba:",":g)",
    ":-)",":snif2:",":bravo:",":d)",
    ":hap:",":ouch:",":pacg:",":cd:",
    ":-)))",":ouch2:",":pacd:",":cute:",
    ":content:",":p)",":-p",":noel:",
    ":oui:",":(",":peur:",":question:",
    ":cool:",":-(",":coeur:",":mort:",
    ":rire:",":-((",":fou:",":sleep:",
    ":-D",":nonnon:",":fier:",":honte:",
    ":rire2:",":non2:",":sarcastic:",":monoeil:",
    ":o))",":nah:",":doute:",":rouge:",
    ":ok:",":non:",":malade:",":fete:",
    ":sournois:",":hum:",":ange:",":diable:",
    ":gni:",":play:",":desole:",":spoiler:",
    ":merci:",":svp:",":sors:",":salut:",
    ":rechercher:",":hello:",":up:",":bye:",
    ":gne:",":lol:",":dpdr:",":dehors:",
    ":hs:",":banzai:",":bave:",":pf:",
    ":cimer:",":ddb:",":pave:",":objection:",
    ":siffle:",":opps:"
  ].sort((a, b) => b.length - a.length)
   .map(emoji => emoji.replace(/[)(]/g, '\\$&'))
   .join('|')

  const ZERO_WIDTH_JOINER = /‍/;
  const REPLACE_PATTERN = /(?<=\p{L})(\p{L})(\p{P}*)$/u;
  const FORMATTING_TAG_PATTERN = /<\/?(?:code|spoil|u|s)>/;
  const VIDEO_EMBED_PATTERN = /\[\[\p{L}+?:\S+?\]\]/;
  const URL_PATTERN = /https?:\/\/\S+\.\S+/;
  const WORD_PATTERN = /\S+/ugi;
  const TOKEN_PATTERN = new RegExp(`(${FORMATTING_TAG_PATTERN.source}|${emojiPatterns}|${VIDEO_EMBED_PATTERN.source}|${URL_PATTERN.source}|${WORD_PATTERN.source})`, "ugi");
  const TOKEN_PATTERN_NO_WORDS = new RegExp(`(${FORMATTING_TAG_PATTERN.source}|${emojiPatterns}|${VIDEO_EMBED_PATTERN.source}|${URL_PATTERN.source})`, "ugi");

  const notifyReactOnChange = (inputElement) => {
    const reactPropsKey = Object.keys(inputElement).find(k => k.startsWith('__reactProps$'));
    if (!reactPropsKey) return;

    const reactProps = inputElement[reactPropsKey];
    const changeEvent = new Event('change');
    Object.defineProperty(changeEvent, 'target', {value:inputElement});

    if (reactProps.onChange) {
      reactProps.onChange(changeEvent);
    }
  }

  const replacefc = (inputElement) => {
    if (!charCountDisplay.isConnected) {
      document.querySelector('.container__postTitle')?.append(charCountDisplay);
    }

    inputElement.value = inputElement.value.split(TOKEN_PATTERN).map(token => {
      if (!token || ZERO_WIDTH_JOINER.test(token) || TOKEN_PATTERN_NO_WORDS.test(token)) {
        const replace = token.replace(ZERO_WIDTH_JOINER.source, "");
        if(TOKEN_PATTERN_NO_WORDS.test(replace)){
          return replace;
        }
        return token;
      }

      if(WORD_PATTERN.test(token)){
        return token.replace(REPLACE_PATTERN, ZERO_WIDTH_JOINER.source + "$1$2");
      }

      return token;
    }).join("");

    notifyReactOnChange(inputElement);
    updateCharacterCount(inputElement);

  }

  document.body.addEventListener("input", (e)=>{
    const inputElement = e.target;
    if (inputElement.id !== "message_reponse") return;

    const cursorPos = inputElement.selectionStart;
    const oldValue = inputElement.value;

    replacefc(inputElement);

    const diff = inputElement.value.length - oldValue.length;
    const newCursorPos = cursorPos + diff;

    inputElement.setSelectionRange(newCursorPos, newCursorPos);

  }, true)

  document.addEventListener('mouseup', (e)=>{
    const target = e.target;
    if (!target.closest('[title="Citer le message"]')) return;

    requestAnimationFrame(() => {

      replacefc(document.querySelector("#message_reponse"));

    })
  },true)

  document.addEventListener('keyup', (e)=>{
    const inputElement = e.target;
    const key = e.key;
    if (inputElement.id !== "message_reponse" || key !== "Backspace") return;

    var start = inputElement.selectionStart;
    var end = inputElement.selectionEnd;

    var lettreAvant = start > 0 ? inputElement.value.charAt(start - 1) : "";

    if (start > 0 && inputElement.value.charAt(start - 1) === '‍') {
      inputElement.value = inputElement.value.substring(0, start - 1) + inputElement.value.substring(end);
      inputElement.selectionStart = inputElement.selectionEnd = start - 1;
    }

    notifyReactOnChange(inputElement);
    updateCharacterCount(inputElement);

  },true)

})()



Editor is loading...
Leave a Comment