badkarma / FixFR 4 Ogame

// ==UserScript==
// @name         FixFR 4 Ogame
// @namespace    http://tampermonkey.net/
// @description  Remplace les accents grave (de merde) par de vraies apostrophes
// @author       badkarma
// @version      1.2
// @include      *fr.ogame*gameforge.com/game/index.php*
// @include      *geo.gfsrv.net*
// @include      http*//topraider.eu/index.php?SR_KEYconv=*
// @updateURL    https://openuserjs.org/meta/badkarma/FixFR_4_Ogame.meta.js
// @grant        GM_xmlhttpRequest
// @licence      GPL-3.0-or-later
// ==/UserScript==

(function () {
  'use strict';
  var words = {
    'Points honorifiques': 'PH',
    '`': '\'',
    '': ''
  };
  var regexs = [
  ],
  replacements = [
  ],
  tagsWhitelist = [
    'PRE',
    'BLOCKQUOTE',
    'CODE',
    'INPUT',
    'BUTTON',
    'TEXTAREA'
  ],
  rIsRegexp = /^\/(.+)\/([gim]+)?$/,
  word,
  text,
  texts,
  i,
  userRegexp;
  // prepareRegex by JoeSimmons
  // used to take a string and ready it for use in new RegExp()
  function prepareRegex(string) {
    return string.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, '\\$1');
  }

  function isTagOk(tag) {
    return tagsWhitelist.indexOf(tag) === - 1;
  }
  delete words['']; 
  for (word in words) {
    if (typeof word === 'string' && words.hasOwnProperty(word)) {
      userRegexp = word.match(rIsRegexp);
      if (userRegexp) {
        regexs.push(new RegExp(userRegexp[1], 'g')
        );
      } else {
        regexs.push(new RegExp(prepareRegex(word).replace(/\\?\*/g, function (fullMatch) {
          return fullMatch === '\\*' ? '*' : '[^ ]*';
        }), 'gi')
        );
      }
      replacements.push(words[word]);
    }
  }
  texts = document.evaluate('//body//text()[ normalize-space(.) != "" ]', document, null, 6, null);
  for (i = 0; text = texts.snapshotItem(i); i += 1) {
    if (isTagOk(text.parentNode.tagName)) {
      regexs.forEach(function (value, index) {
        text.data = text.data.replace(value, replacements[index]);
      });
    }
  }
}());

// Partie AJAX
var replaceArry = [
    [/`/gi,'\''],
];
var numTerms = replaceArry.length;
                  //-- 5 times/second; Plenty fast.
var transTimer = setInterval (translateTermsOnPage, 222);

function translateTermsOnPage () {
    var txtWalker = document.createTreeWalker (
        document.body,
        NodeFilter.SHOW_TEXT, {
            acceptNode: function (node) {
                //-- Skip whitespace-only nodes
                if (node.nodeValue.trim() ) {
                    if (node.tmWasProcessed)
                        return NodeFilter.FILTER_SKIP;
                    else
                        return NodeFilter.FILTER_ACCEPT;
                }
                return NodeFilter.FILTER_SKIP;
            }
        },
        false
    );
    var txtNode = null;
    while (txtNode = txtWalker.nextNode () ) {
        txtNode.nodeValue = replaceAllTerms (txtNode.nodeValue);
        txtNode.tmWasProcessed = true;
    }
    var placeholderNodes = document.querySelectorAll ("[placeholder]");
    replaceManyAttributeTexts (placeholderNodes, "placeholder");

    var titleNodes = document.querySelectorAll ("[title]");
    replaceManyAttributeTexts (titleNodes, "title");
}

function replaceAllTerms (oldTxt) {
    for (var J = 0; J < numTerms; J++) {
        oldTxt = oldTxt.replace (replaceArry[J][0], replaceArry[J][1]);
    }
    return oldTxt;
}

function replaceManyAttributeTexts (nodeList, attributeName) {
    for (var J = nodeList.length - 1; J >= 0; --J) {
        var node = nodeList[J];
        var oldText = node.getAttribute (attributeName);
        if (oldText) {
            oldText = replaceAllTerms (oldText);
            node.setAttribute (attributeName, oldText);
        }
    }
}