Advertisement
Guest User

Untitled

a guest
Apr 7th, 2015
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. for those curious about the code, some refactoring and annotation (no guarantee this will run):
  2. [code]
  3. function isGraphicUnicode(inChar) { // check if an input character is a graphic unicode
  4.     var inCharCode = inChar.charCodeAt(0) // Get the character code of the input's first character
  5.     var graphicUnicodes = [ // define an array with some unicodes to dislike
  6.             [9472, 9583], // box drawings
  7.             [9600, 9631], // blocks
  8.             [9632, 9727] // various geometric shapes
  9.     ]
  10.     for (i = 0; i < graphicUnicodes.length; i++) { // loop over all the unicodes we dislike
  11.         if (inCharCode > graphicUnicodes[i][0] && t <= graphicUnicodes[i][1]) { // if the character being evaluated is between one of the two values,
  12.             return true; // declare it a graphic unicode
  13.         }
  14.     }
  15.     return false // otherwise it's not a graphic unicode
  16. }
  17.  
  18. function contentScore(element) {
  19.     var score = 0; // start the score out at zero
  20.     var childNodes = Array.prototype.slice.call(element.childNodes) // get all child nodes of the element
  21.     var score = childNodes.reduce(function(tempScore, node) { // the reduce method calls a function for each of the elements of the array and adds together all the values returned by that function
  22.         if ($(node).attr("skip")) { return tempScore } // if the node has an attribute 'skip', don't add to the tempScore
  23.         if (node.nodeType == 1) { return tempScore + contentScore(node) } // if the node is an element itself, call the contentScore function on this (recursion!)
  24.         if (node.nodeType == 3) { // if it's plaintext
  25.             var unicodeScore = 0;
  26.             for (character of node.nodeValue) { // a way of looping over each element in an array, in this case characters
  27.               if (isGraphicUnicode(character)) { unicodeScore++; } // if it's a graphic unicode character, increase the unicode score
  28.             }
  29.             var parentFontSize = Number(getComputedStyle(node.parentNode).fontSize.match(/(\d*(\.\d*)?)px/)[1]); // get the parent node's font size if set in pixels (via regex capture)
  30.             return tempScore + parentFontSize * parentFontSize * unicodeScore // add these four together and add that to the tempScore
  31.         }
  32.         return tempScore // in all other cases, don't add to the tempScore
  33.     }, 0); // start tempScore out at 0
  34.     return score
  35. }
  36.  
  37. function formatScoreEx(element) {
  38.     if (element.backgroundcolor) { return 1; } // if the element has a background color, score it as 1 by default
  39.     var score = 0;
  40.     if (element.fontsize >= 14) {  // if the element's font size is over 14
  41.       score += .05 + Math.max(0, .02 * (element.fontsize - 14)) // increase the score based on just how much over 14 it is.  Make sure it doesn't 'increase' by a negative number (in case of quirk), using max()
  42.     }
  43.     if (element.color) { score += .2) } // if the element has color, increase score
  44.     if (element.font) { score += .03 } // if the element has a custom font, increase score
  45.     if (element.underline) { score += .05 } // if the element has underlines, increase score
  46.     if (element.bold) { score += .05 } // if the element uses a bold tag, increase score
  47.     if (element.table) { score += .2 } // if the element uses a table, increase score
  48.     return Math.min(score, 1)) // return the score, or 1, whichever is smaller.
  49. }
  50.  
  51. function formatScore(element, testCopy) {
  52.     var originalCopy = testCopy;
  53.     if (typeof t == "undefined") {
  54.         originalCopy = testCopy = {}
  55.     }
  56.     else {
  57.         testCopy = $.extend({}, testCopy);
  58.     }
  59.  
  60.     if (element.style["font-size"] && (element.style["font-size"] != "inherit")) { // if the element has a font-size that isn't 'inherit'
  61.       testCopy.fontsize = Number(element.style["font-size"].match(/(\d+)pt/)[1]); // set the test copy's font size value to N, where N is the value in points (via regex capture)
  62.     }
  63.     if (element.style.color && (element.style.color != "inherit") { testCopy.color = true; } // if the element has color specified that isn't 'inherit', set the test's color state to true
  64.     if (element.style["background-color"] && (element.style["background-color"] != "inherit") { testCopy.backgroundcolor = true; } // if the element has background-color specified that isn't 'inherit', set the test's background color state to true
  65.     if (element.style["font-family"] && (element.style["font-family"] != "inherit") { testCopy.font = true; } // if the element has a font specified that isn't 'inherit', set the test's font use state to true
  66.     if (element.style["text-decoration"].indexOf("underline") != -1) { testCopy.underline = true; } // if the element has underline anywhere in it, set the test's use of underline state to true
  67.     if (element.tagName == "B") { testCopy.bold = true; } // if it's a bold tag, set the test's use of bold state to true
  68.     if (element.tagName == "TABLE") { testCopy.table = true; } // if it's a table tag, set the test's use of table state to true
  69.  
  70.     var score = ($(element).width() * $(element).height() * formatScoreEx(testCopy)) - ($(element).width() * $(element).height() * formatScoreEx(originalCopy)); // get the difference in format scores for the original and the test
  71.  
  72.     var childNodes = Array.prototype.slice.call(element.childNodes); // get all the childNodes of the element
  73.     score += childNodes.reduce(
  74.         function(tempScore, node) {
  75.             if (node.nodeType == 1) { // if the node is an element,
  76.                 tempScore + formatScore(node, testCopy) // call this function on it and increase the tempScore by its result
  77.             }
  78.             else { tempScore } // otherwise, don't increase the tempScore
  79.       }
  80.     , 0) // start tempScore out at 0
  81.     return score
  82. }
  83.  
  84. function offendingSignature(sig) { // determine if a signature is offending
  85.     var score = 0; // start out with a score of zero
  86.     score += contentScore(sig); // add the content score
  87.     score += formatScore(sig); // add the formatting score
  88.     score = Math.sqrt(score); // square root the score
  89.     If ($(sig).find("a").length > 0) { score *= 2; } // if there's a link in the signature, multiply the score by two
  90.     if (score > 100) { return true; } // if the score exceeds 100, declare the signature as offending
  91.     return false // otherwise, not so much
  92. }
  93.  
  94. var allSignatures = $("div .signature"); // get all divs with class 'signature'
  95. for (var i = 0; i < allSignatures.length; i++) { // loop over them
  96.     if (offendingSignature(allSignatures[i])) { // if it's an offending signature
  97.       $(allSignatures[i]).attr("style", "opacity: 0; pointer-events: none"); // make it invisible and impossible to click or tap
  98.     }
  99. }
  100. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement