Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for those curious about the code, some refactoring and annotation (no guarantee this will run):
- [code]
- function isGraphicUnicode(inChar) { // check if an input character is a graphic unicode
- var inCharCode = inChar.charCodeAt(0) // Get the character code of the input's first character
- var graphicUnicodes = [ // define an array with some unicodes to dislike
- [9472, 9583], // box drawings
- [9600, 9631], // blocks
- [9632, 9727] // various geometric shapes
- ]
- for (i = 0; i < graphicUnicodes.length; i++) { // loop over all the unicodes we dislike
- if (inCharCode > graphicUnicodes[i][0] && t <= graphicUnicodes[i][1]) { // if the character being evaluated is between one of the two values,
- return true; // declare it a graphic unicode
- }
- }
- return false // otherwise it's not a graphic unicode
- }
- function contentScore(element) {
- var score = 0; // start the score out at zero
- var childNodes = Array.prototype.slice.call(element.childNodes) // get all child nodes of the element
- 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
- if ($(node).attr("skip")) { return tempScore } // if the node has an attribute 'skip', don't add to the tempScore
- if (node.nodeType == 1) { return tempScore + contentScore(node) } // if the node is an element itself, call the contentScore function on this (recursion!)
- if (node.nodeType == 3) { // if it's plaintext
- var unicodeScore = 0;
- for (character of node.nodeValue) { // a way of looping over each element in an array, in this case characters
- if (isGraphicUnicode(character)) { unicodeScore++; } // if it's a graphic unicode character, increase the unicode score
- }
- 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)
- return tempScore + parentFontSize * parentFontSize * unicodeScore // add these four together and add that to the tempScore
- }
- return tempScore // in all other cases, don't add to the tempScore
- }, 0); // start tempScore out at 0
- return score
- }
- function formatScoreEx(element) {
- if (element.backgroundcolor) { return 1; } // if the element has a background color, score it as 1 by default
- var score = 0;
- if (element.fontsize >= 14) { // if the element's font size is over 14
- 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()
- }
- if (element.color) { score += .2) } // if the element has color, increase score
- if (element.font) { score += .03 } // if the element has a custom font, increase score
- if (element.underline) { score += .05 } // if the element has underlines, increase score
- if (element.bold) { score += .05 } // if the element uses a bold tag, increase score
- if (element.table) { score += .2 } // if the element uses a table, increase score
- return Math.min(score, 1)) // return the score, or 1, whichever is smaller.
- }
- function formatScore(element, testCopy) {
- var originalCopy = testCopy;
- if (typeof t == "undefined") {
- originalCopy = testCopy = {}
- }
- else {
- testCopy = $.extend({}, testCopy);
- }
- if (element.style["font-size"] && (element.style["font-size"] != "inherit")) { // if the element has a font-size that isn't 'inherit'
- 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)
- }
- 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
- 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
- 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
- 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
- if (element.tagName == "B") { testCopy.bold = true; } // if it's a bold tag, set the test's use of bold state to true
- if (element.tagName == "TABLE") { testCopy.table = true; } // if it's a table tag, set the test's use of table state to true
- 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
- var childNodes = Array.prototype.slice.call(element.childNodes); // get all the childNodes of the element
- score += childNodes.reduce(
- function(tempScore, node) {
- if (node.nodeType == 1) { // if the node is an element,
- tempScore + formatScore(node, testCopy) // call this function on it and increase the tempScore by its result
- }
- else { tempScore } // otherwise, don't increase the tempScore
- }
- , 0) // start tempScore out at 0
- return score
- }
- function offendingSignature(sig) { // determine if a signature is offending
- var score = 0; // start out with a score of zero
- score += contentScore(sig); // add the content score
- score += formatScore(sig); // add the formatting score
- score = Math.sqrt(score); // square root the score
- If ($(sig).find("a").length > 0) { score *= 2; } // if there's a link in the signature, multiply the score by two
- if (score > 100) { return true; } // if the score exceeds 100, declare the signature as offending
- return false // otherwise, not so much
- }
- var allSignatures = $("div .signature"); // get all divs with class 'signature'
- for (var i = 0; i < allSignatures.length; i++) { // loop over them
- if (offendingSignature(allSignatures[i])) { // if it's an offending signature
- $(allSignatures[i]).attr("style", "opacity: 0; pointer-events: none"); // make it invisible and impossible to click or tap
- }
- }
- [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement