Advertisement
Guest User

Untitled

a guest
May 26th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1. window.ve_dom = (function () {
  2. /** Attributes * */
  3. var COMMON_ANALYSIS_OPTIONS = ['font', 'border', 'background']
  4. var TAGNAME_TO_FILTER = ['TITLE', 'STYLE', 'META', 'LINK', 'SCRIPT', 'BASE', 'AUDIO', 'VIDEO', 'HEAD']
  5. var CSS_VALUE_CACHE_STORAGE
  6.  
  7. var DEFINITION_OPTION_PROPERTIES = {
  8. font: [
  9. 'font-family',
  10. 'font-size',
  11. 'font-weight',
  12. 'color'
  13. ],
  14. border: [
  15. 'border-bottom-color',
  16. 'border-bottom-left-radius',
  17. 'border-bottom-right-radius',
  18. 'border-bottom-style',
  19. 'border-bottom-width',
  20. 'border-left-color',
  21. 'border-left-style',
  22. 'border-left-width',
  23. 'border-right-color',
  24. 'border-right-style',
  25. 'border-right-width',
  26. 'border-top-color',
  27. 'border-top-left-radius',
  28. 'border-top-right-radius',
  29. 'border-top-style',
  30. 'border-top-width'],
  31. background: [
  32. 'background-color'
  33. ]
  34. }
  35. var RGBA_FORCED_PROPERTIES = ['background-color', 'border-left-color', 'border-right-color', 'border-top-color',
  36. 'border-bottom-color', 'color']
  37. var HEX_REGEX = /^#(?:[A-Fa-f0-9]{3}){1,2}$/
  38. var RGB_REGEX = /^rgb[(](?:\s*0*(?:\d\d?(?:\.\d+)?(?:\s*%)?|\.\d+\s*%|100(?:\.0*)?\s*%|(?:1\d\d|2[0-4]\d|25[0-5])(?:\.\d+)?)\s*(?:,(?![)])|(?=[)]))){3}[)]$/
  39.  
  40. /** Cache functions * */
  41.  
  42. function clearCache () {
  43. CSS_VALUE_CACHE_STORAGE = {}
  44. }
  45.  
  46. function addCache (option, value) {
  47. CSS_VALUE_CACHE_STORAGE[option].push(value)
  48. }
  49. function getCacheIndex (option, value) {
  50. if (typeof CSS_VALUE_CACHE_STORAGE[option] === 'undefined') {
  51. CSS_VALUE_CACHE_STORAGE[option] = []
  52. addCache(option, value)
  53. return 0
  54. } if (CSS_VALUE_CACHE_STORAGE[option].indexOf(value) === -1) {
  55. addCache(option, value)
  56. }
  57. return CSS_VALUE_CACHE_STORAGE[option].indexOf(value)
  58. }
  59. /** Loop functions * */
  60.  
  61. function loopThroughAllElementAndPseudo (resultTemplate, allElements, cssProps) {
  62. cssProps.forEach(function (cssProp) {
  63. var optionAndResultsObject = {
  64. option: cssProp,
  65. results: []
  66. }
  67. resultTemplate.push(optionAndResultsObject)
  68. })
  69.  
  70. allElements.forEach((element) => {
  71. if (TAGNAME_TO_FILTER.includes(element.tagName)) { return }
  72. loopThroughAllPseudoElement(resultTemplate, cssProps, element)
  73. })
  74. return resultTemplate
  75. }
  76.  
  77. function loopThroughAllPseudoElement (resultTemplate, cssProps, element) {
  78. var pseudoElement = ['', ':before', ':after']
  79. pseudoElement.forEach((pseudo, i) => {
  80. let content = window.getComputedStyle(element, pseudo).content
  81. if (content === 'none') { return }// Si pas de pseudo element, passer au suivant
  82. fillPropertiesWithCssProps(resultTemplate, cssProps, element, pseudo)
  83. })
  84. }
  85.  
  86. /** Analysis functions * */
  87. function fillPropertiesWithCssProps (resultTemplate, cssProps, element, pseudo) {
  88. cssProps.forEach(function (cssProp, i) {
  89. if (Array.isArray(DEFINITION_OPTION_PROPERTIES[cssProp])) { // si c'est un array
  90. cssProp = DEFINITION_OPTION_PROPERTIES[cssProp]
  91. pushIntoPropertiesByCssPropArray(cssProp, element, pseudo, resultTemplate[i])
  92. } else {
  93. pushIntoPropertiesByCssPropString(cssProp, element, pseudo, resultTemplate[i])
  94. }
  95. })
  96. }
  97.  
  98. function pushIntoPropertiesByCssPropArray (cssProp, element, pseudo, optionAndResultsObject) {
  99. let resultTemp = {
  100. score: 0,
  101. strValue: '',
  102. properties: []
  103. }
  104. cssProp.forEach(function (property) {
  105. var elementCssPropValue = window.getComputedStyle(element, pseudo)[property]
  106. if (RGBA_FORCED_PROPERTIES.includes(property)) { elementCssPropValue = forcedColorToRgba(elementCssPropValue) };
  107. resultTemp.properties.push({ property: property, value: elementCssPropValue })
  108. resultTemp.strValue += getCacheIndex(cssProp, elementCssPropValue)
  109. // resultTemp.strValue += elementCssPropValue
  110. })
  111. if (optionAndResultsObject.results.length === 0) {
  112. optionAndResultsObject.results.push(resultTemp)// premier push
  113. } else {
  114. for (var i = 0; i < optionAndResultsObject.results.length; i++) {
  115. if (optionAndResultsObject.results[i].strValue === resultTemp.strValue) {
  116. optionAndResultsObject.results[i].score++
  117. break
  118. } else if (i === optionAndResultsObject.results.length - 1) {
  119. optionAndResultsObject.results.push(resultTemp)
  120. }
  121. }
  122. }
  123. }
  124.  
  125. function pushIntoPropertiesByCssPropString (cssProp, element, pseudo, optionAndResultsObject) {
  126. var elementCssPropValue = window.getComputedStyle(element, pseudo)[cssProp]
  127. if (RGBA_FORCED_PROPERTIES.includes(cssProp)) { elementCssPropValue = forcedColorToRgba(elementCssPropValue) };
  128. let resultTemp = {
  129. score: 0,
  130. strValue: '',
  131. properties: [{ property: cssProp, value: elementCssPropValue }]
  132. }
  133. if (optionAndResultsObject.results.length === 0) {
  134. optionAndResultsObject.results.push(resultTemp) // premier push pour avoir de quoi comparer
  135. } else {
  136. for (var i = 0; i < optionAndResultsObject.results.length; i++) {
  137. if (optionAndResultsObject.results[i].strValue === resultTemp.strValue) {
  138. optionAndResultsObject.results[i].score++
  139. break
  140. } else if (i === optionAndResultsObject.results.length - 1) {
  141. optionAndResultsObject.results.push(resultTemp)
  142. }
  143. }
  144. }
  145. }
  146.  
  147. /** Color converter functions * */
  148.  
  149. function forcedColorToRgba (colorProp) {
  150. if (HEX_REGEX.test(colorProp)) {
  151. colorProp = hexToRgba(colorProp)
  152. } else if (RGB_REGEX.test(colorProp)) {
  153. colorProp = rgbToRgba(colorProp)
  154. }
  155. return colorProp
  156. }
  157.  
  158. // RGB to RGBA
  159. function rgbToRgba (rgb) {
  160. function insert (string, index, stringToInsert) {
  161. if (index > 0) { return string.substring(0, index) + stringToInsert + string.substring(index, string.length) }
  162. return stringToInsert + string
  163. }
  164. rgb = insert(rgb, 3, 'a')
  165. rgb = insert(rgb, rgb.length - 1, ',1')
  166. return (rgb)
  167. }
  168.  
  169. // HEX to RGBA
  170. function hexToRgba (hex) {
  171. var r = 0; var g = 0; var
  172. b = 0
  173. if (hex.length === 4) { // Si format #FFF
  174. r = `0x${hex[1]}${hex[1]}`
  175. g = `0x${hex[2]}${hex[2]}`
  176. b = `0x${hex[3]}${hex[3]}`
  177. } else if (hex.length === 7) { // Si format #FFFFFF
  178. r = `0x${hex[1]}${hex[2]}`
  179. g = `0x${hex[3]}${hex[4]}`
  180. b = `0x${hex[5]}${hex[6]}`
  181. }
  182. return (`rgba(${parseInt(r)},${parseInt(g)},${parseInt(b)},1)`)
  183. }
  184.  
  185. /** Sort functions * */
  186. function orderByScore (a, b) {
  187. if (a.score > b.score) {
  188. return -1
  189. }
  190. if (a.score < b.score) {
  191. return 1
  192. }
  193. return 0
  194. }
  195.  
  196. function sortResultTemplate (resultTemplate) {
  197. for (var i = 0; i < resultTemplate.length; i++) {
  198. resultTemplate[i].results.sort(orderByScore)
  199. }
  200. }
  201.  
  202. /** Public functions * */
  203.  
  204. /** J'implémente les functions publique directement dans mon objet return, on sait mieux ce qui est public et ce qui ne l'est pas * */
  205. return {
  206. commonStyleAnalysis: function commonStyleAnalysis () {
  207. var start = Date.now()
  208. clearCache()
  209. /** this permet de pointer sur ton objet courant, en résumé, ton objet avec les méthodes publiques * */
  210. var resultTemplate = []
  211. var allElements = document.querySelectorAll('*')
  212. console.log(allElements.length + ' elements')
  213. loopThroughAllElementAndPseudo(resultTemplate, allElements, COMMON_ANALYSIS_OPTIONS)
  214. sortResultTemplate(resultTemplate)
  215. console.log(Date.now() - start)
  216.  
  217. return resultTemplate
  218. },
  219. customStyleAnalysis: function customStyleAnalysis (cssProps) {
  220. var resultTemplate = []
  221. var start = Date.now()
  222. clearCache()
  223.  
  224. var allElements = document.querySelectorAll('*')
  225. /** ICI il faut voir si tes cssProps sont des options, ou des attributs CSS seuls avant de continuer * */
  226. console.log(allElements.length + ' elements')
  227. loopThroughAllElementAndPseudo(resultTemplate, allElements, cssProps)
  228. sortResultTemplate(resultTemplate)
  229. console.log(Date.now() - start)
  230.  
  231. return resultTemplate
  232. }
  233. }
  234. }())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement