Wikipedia Anarchy Hack

Fun with Wikipedia.org, appealing for 'Ⓐnarchy!!'' on every 'A'

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          Wikipedia Anarchy Hack
// @namespace     greasyfork
// @description	  Fun with Wikipedia.org, appealing for 'Ⓐnarchy!!'' on every 'A'
// @author        Noudio
// @license       MIT
// @homepage      -
// @include       http://wikipedia.org/*
// @include       https://wikipedia.org/*
// @include       http://*.wikipedia.org/*
// @include       https://*.wikipedia.org/*
// @include       http://wikimedia.org/*
// @include       https://wikimedia.org/*
// @include       http://*.wikimedia.org/*
// @include       https://*.wikimedia.org/*
// @run-at        document-idle
// @version       1.3
// ==/UserScript==
(function() {

    function isDomain(x) {
        return document.domain.substring(document.domain.indexOf(x)) == x;
    }
    var isPedia = isDomain("wikipedia.org");
    var isMedia = isDomain("wikimedia.org");
    if (!isPedia && !isMedia)
        return;

    //console.log('anarchy1!');

    // -----------------------------
    // text replacement function
    function doSomething(element) {
      element.nodeValue =
          element.nodeValue.replace(/A([A-Za-z]*)/g,
              function(a,b){
                  exclNr = Math.floor(Math.random()*4.0);
                  if (b != '')
                      return "Ⓐ"+b+" - Ⓐnarchy"+"!".repeat(exclNr)+" -";
                      return "Ⓐ";});
      //console.log('anarchy el:'+element.nodeValue);
    }

    // -----------------------------
    // recursive scan for text nodes
    function recurse(element)
    {
        // recursively call your self on your child nodes
        element.childNodes.forEach(n => recurse(n));

        // do only something for capital letters A
        if (element.nodeType == Node.TEXT_NODE && /A/.test(element.nodeValue))
            doSomething(element);
    }

    // ---------------------------------------------------
    // handler to continuously observe dom for added nodes
    var observeDOM = (function(){
      var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;

      return function( obj, callback ){
        if( !obj || obj.nodeType !== 1 ) return;

        if( MutationObserver ){
          // define a new observer
          var mutationObserver = new MutationObserver(callback)

          // have the observer observe for changes in children
          mutationObserver.observe( obj, { childList:true, subtree:true })
          return mutationObserver
        }

        // browser support fallback
        else if( window.addEventListener ){
          obj.addEventListener('DOMNodeInserted', callback, false)
          obj.addEventListener('DOMNodeRemoved', callback, false)
        }
      }
    })();

    // ---------------------------------------------------
    // first time replace the body
    recurse(document.body);

    // --------------------------------------------------------------
    // Observe the body element and apply anarchy for added elements:
    observeDOM( document.body, function(m){
      var addedNodes = [], removedNodes = [];

      m.forEach(record => record.addedNodes.length & addedNodes.push(...record.addedNodes))
      //m.forEach(record => record.removedNodes.length & removedNodes.push(...record.removedNodes))
     //console.clear();
     //console.log('Added:', addedNodes, 'Removed:', removedNodes);
     //console.log('Added:', addedNodes);
     addedNodes.forEach(n => recurse(n));
   });


})();
//Taf!