User:AuFCL/common.js/typoscan.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

// <nowiki>
 
var typoFuse;  // overriden by actions.limit
 
function HighlightTyposUnder( node, actions ){
    if( typoFuse >= 0 && node ){
    if( node.nodeType == 3 /* TEXT_NODE */ ){
      for( var I = 0; I < actions.patterns.length; I++ ){
        var pattern = actions.patterns[ I ];
 
        if( node.nodeValue.match( pattern ) ){
          if( node.parentNode ){
            node.parentNode.innerHTML = node.parentNode.innerHTML.replace( pattern, '<span style="' + actions.styling + '">$&</span>' );
            typoFuse--;
          }
        }
      }
    }
 
    if( node.childNodes.length ){
      for( var subnode=0; subnode < node.childNodes.length; subnode++ ){
        if( node.childNodes[ subnode ].getAttribute ){
          if( !/pagenumber/.test( node.childNodes[ subnode ].getAttribute( "class" ) ) ){
            HighlightTyposUnder( node.childNodes[ subnode ], actions );
          }
        } else {
          HighlightTyposUnder( node.childNodes[ subnode ], actions );
        }
      }
    }
  }
}
 
function HighlightTyposLike( actions ){
  self.typoscan = self.typoscan || { exclude: true };
 
  if( !( actions.exclude ) ){       //don't bother scanning historical, edit-in-progress or given ns pages.
 
    var content=document.getElementById('wikiPreview'); //presume currently editing page (must not touch unsafe structures like wpEditToken!)
 
    if( !content ){
	  	content=document.getElementById('mw-content-text'); //user not currently editing: assume safe to address entire display region
	  }
 
    typoFuse = actions.limit;

    for( var N=0; N<actions.groups.length; N++ ){
      if( actions.groups[ N ].include ){
        HighlightTyposUnder( content, actions.groups[ N ] );
      }
    }
  }
}
 
self.typoscan={
  exclude:
    /(action=history|(diff|oldid|search)=|(author|category|extension|file|help|index|mediawiki|meta|module|special|talk|template|topic|user|wiki([mp]edia|source))(:|%3A))/i.test( location.href ),
  limit:
    25,
  groups: [
    {
      include:
        true,                                       // this group applies to all pages not already excluded
      patterns: [
        /\\\S/g,                                    // back-slash escape?
        /;[.,:]/g,                                  // chained punctuation, semicolon-led
        /,[.,;:]/g,                                 // chained punctuation, comma-led
        /:[.,;]/g,                                  // chained punctuation, colon-led
        /\s["'`;:,!?$%*()=+~]\s/g,                  // floating punctuation mark: WARNING: modern style: floating "=" OK
        /modem/gi,                                  // typo of "modern"
        /nearlv/gi,                                 // typo of "nearly"
        /vear/gi,                                   // (possible) typo of "year"
        /lI/g,                                      // typo of "ll"
        /\Wnm\W/g,                                  // typo of "run"
        /\w&/g,                                     // embedded or trailing "&"
        /&(?!c\.)\w/g,                              // leading "&" ("&c." O.K.)
        /\Wim(der|desirable|less|productive)/g,     // typo of "under"/"undesirable"/"unless"/"unproductive"
        /(?!na)vv(?!(ies|y))/g,                     // typo of "w" (though legitimate in "navvy" or plural)
        /stimt/g                                    // typo of "stunt"
      ],
      styling:
        'outline:5px double olivedrab;'
    },
    {
      include:
        true,                                       // this group applies to all pages not already excluded
      patterns: [
        /(^|[^'])Ave(?!([.!]| Maria))(\W|$)/g,      // typo of "we" (but "'Ave", "Ave." or "Ave Maria" is O.K.)
        /\|[-+]?/g,                                 // wikicode leaking into HTML  (yes the '?' is dodgy but enlarges the match for table caption/row)
        /(^|\W)Sts?(?!\.)(\W|$)/g,                  // typo of "St." 
        /llsh/g                                     // typo of "lish" (embedded fragment: e.g. "establishment")
      ],
      styling:
        'background:salmon;outline:1px solid olivedrab;'
    },
    {
      include:
        /Popular.Science.Monthly/.test( location.href ), // this group applies only to PSM pages
      patterns: [
        /(^|\W)[a-z]+[A-Z]+[A-Za-z]+(\W|$)/g,       // upper case embedded within lower case word
        /(^|\W)[A-Z]{2,}[a-z]+[A-Za-z]+(\W|$)/g,    // lower case embedded within upper case word
        /(^|\W)[a-zA-Z]+\d+[a-zA-Z]*(\W|$)/g,       // digit embedded within word
        /(^|\W)\d+[a-zA-Z]+\d+(\W|$)/g,             // alphabetic embedded within digits
        /[{[\]}^]{1,}/g,                            // mis-terminated template, link or standalone "^"?
        /(^|\W)[a-zA-Z]+[.,]+[a-zA-Z]+(\W|$)/g      // period surrounded by letters
      ],
      styling:
        'background:orange;outline:1px solid blue;'
    },
    {
      include:
        /(Popular.Science.Monthly)/.test( location.href ), // this group applies only to PSM pages
      patterns: [
        /[-–—][ \t]/g,                              // unattached hyphen/ndash/mdash, leading case
        /[ \t][-–—][^.\d]/g,                        // unattached hyphen/ndash/mdash, trailing case (O.K if digit or "."" following)
        /[ \t]{3,}/g                                // three or more consecutive space or tabs (worst case two can be legitimate in transclusion)
      ],
      styling:
        'outline:1px solid blue;'
    }
  ]
};
 
jQuery( document ).ready(
  HighlightTyposLike( self.typoscan )
);
 
// </nowiki>