From f316132bf385edeefdd8af70fc1325c177923ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=B5=AA=E6=B3=A2?= Date: Wed, 22 Nov 2017 00:40:38 -0600 Subject: [PATCH] =?UTF-8?q?update:ad=20=E7=BD=91=E7=9B=9F=E5=B9=BF?= =?UTF-8?q?=E5=91=8A=E4=BC=98=E5=8C=96=20(#910)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add sample logic * update:ad 优化 * update:ad 优化 * update:ad 优化 --- src/mip-ad/mip-ad-baidu.js | 144 ++++++++++++++++++--------------- src/mip-ad/mip-baidu-wm-ext.js | 81 +++++++++++-------- 2 files changed, 129 insertions(+), 96 deletions(-) diff --git a/src/mip-ad/mip-ad-baidu.js b/src/mip-ad/mip-ad-baidu.js index e399bb261..52ccd709e 100755 --- a/src/mip-ad/mip-ad-baidu.js +++ b/src/mip-ad/mip-ad-baidu.js @@ -1,6 +1,5 @@ /** - * 网盟插件 - * + * @file 网盟插件 * @author fengchuantao@baidu.com * @version 1.0 * @copyright 2016 Baidu.com, Inc. All Rights Reserved @@ -9,52 +8,70 @@ define(function (require) { var $ = require('zepto'); var jsSrc = '//dup.baidustatic.com/js/dm.js'; var scriptId = 'MIP_DUP_JS'; - - var render = function(_this, me) { + var exps = '113002'; - var $this = $(_this); - var cproID = _this.getAttribute("cproid"); - if(!cproID) { + var render = function (that, me) { + + var $this = $(that); + var MIP = window.MIP || {}; + var sample = MIP.hash.get('sample'); + if (sample === 'mip_wm_sample') { + exps = '113004'; + } + var cproID = that.getAttribute('cproid'); + if (!cproID) { return; } // 定制化 特殊处理 - var elem = _this.querySelector('script') || null; - - if(elem) { - - if(isJsonScriptTag(elem)) { - jsSrc = '//cpro.baidustatic.com/cpro/ui/c.js'; - scriptId = 'MIP_DUP_JS_EXT'; - var obj = JSON.parse(elem.textContent.toString()); - (window.cproArray = window.cproArray || []).push({ - id: cproID - }); - - (window["cproStyleApi"]=window["cproStyleApi"] || {})[cproID] = obj; - - } - + var elem = that.querySelector('script') || null; + + // 判断 preload 逻辑 + var scripts = document.querySelector('script[mip-preload="mip-script-wm"]'); + if (scripts && !elem && sample === 'mip_wm_sample') { + var s = '_' + Math.random().toString(36).slice(2); + var html = '
'; + $this.append(html); + var apiStr = '__container_api_'; + (window[apiStr] = window[apiStr] || []).push({ + containerId: s, + exps: exps, + slotId: cproID + }); } - - var script = initJs(); - if (!elem) { - initadbaidu($this, cproID, me, script); + else { + if (elem) { + if (isJsonScriptTag(elem)) { + jsSrc = '//cpro.baidustatic.com/cpro/ui/c.js'; + scriptId = 'MIP_DUP_JS_EXT'; + var obj = JSON.parse(elem.textContent.toString()); + (window.cproArray = window.cproArray || []).push({ + id: cproID + }); + (window.cproStyleApi = window.cproStyleApi || {})[cproID] = obj; + } + } + var script = initJs(); + if (!elem) { + initadbaidu($this, cproID, me, script); + } } }; /** * [initJs JS初始化函数] - * - * @return + * + * @return {Object} */ function initJs() { var MIPDUPJS = document.getElementById(scriptId); - if(MIPDUPJS) return MIPDUPJS; + if (MIPDUPJS) { + return MIPDUPJS; + } var script = document.createElement('script'); script.src = jsSrc; script.id = scriptId; - document.body.appendChild(script); + document.body.appendChild(script); return script; @@ -62,47 +79,50 @@ define(function (require) { /** * [initadbaidu 广告组件初始化] - * + * * @param {Object} $elemID mip对象 - * @param {String} cproID 广告id - * @return + * @param {string} cproID 广告id + * @param {Object} me dom对象 + * @param {Object} script script对象 + * */ function initadbaidu($elemID, cproID, me, script) { - var s = "_" + Math.random().toString(36).slice(2); + var s = '_' + Math.random().toString(36).slice(2); var html = '
'; $elemID.append(html); - (window.slotbydup=window.slotbydup || []).push({ + (window.slotbydup = window.slotbydup || []).push({ id: cproID, container: s, display: 'inlay-fix', + exps: exps, async: true }); - if(script) { + if (script) { var fixedElement = require('fixed-element'); var layer = fixedElement._fixedLayer; var child = document.getElementById(s); - child.addEventListener("DOMSubtreeModified", function(e) { + child.addEventListener('DOMSubtreeModified', function (e) { var elem = window.getComputedStyle(child, null); - var pos = elem && elem.getPropertyValue('position') ? - elem.getPropertyValue('position') : ''; - if(layer && layer.querySelector('#'+s)){ - return; + var pos = elem && elem.getPropertyValue('position') + ? elem.getPropertyValue('position') : ''; + if (layer && layer.querySelector('#' + s)) { + return; } - if(pos == 'fixed') { + if (pos === 'fixed') { $elemID.append(document.getElementById(s)); - if(layer) { - var idx = document.querySelectorAll('mip-fixed').length; - var data = { - element: child.parentElement, - id: 'Fixed'+ idx - }; - fixedElement.moveToFixedLayer(data, parseInt(idx)); + if (layer) { + var idx = document.querySelectorAll('mip-fixed').length; + var data = { + element: child.parentElement, + id: 'Fixed' + idx + }; + fixedElement.moveToFixedLayer(data, parseInt(idx, 10)); } } - },false); + }, false); } me.applyFillContent(document.getElementById(s), true); @@ -112,24 +132,20 @@ define(function (require) { /** * [isJsonScriptTag 判断是否是定制化script标签] - * - * @param {Object} element 节点对象 - * @return {Boolean} + * + * @param {Object} element 节点对象 + * + * @return {boolean} */ - function isJsonScriptTag(element){ - - return element.tagName == 'SCRIPT' && - - element.getAttribute('type') && - - element.getAttribute('type').toUpperCase() == 'APPLICATION/JSON'; + function isJsonScriptTag(element) { + return element.tagName === 'SCRIPT' + && element.getAttribute('type') + && element.getAttribute('type').toUpperCase() === 'APPLICATION/JSON'; } return { render: render - } + }; }); - - diff --git a/src/mip-ad/mip-baidu-wm-ext.js b/src/mip-ad/mip-baidu-wm-ext.js index 0a62360c7..3cf541672 100644 --- a/src/mip-ad/mip-baidu-wm-ext.js +++ b/src/mip-ad/mip-baidu-wm-ext.js @@ -1,45 +1,62 @@ /** - * 广告扩展组件 - * - * @author wangpei07@baidu.com + * @file 广告扩展组件 + * @author lilangbo@baidu.com * @version 1.0 * @copyright 2016 Baidu.com, Inc. All Rights Reserved */ define(function (require) { - - var render = function(_this, me) { + var render = function (that, me) { - var self = _this; + var self = that; var domain = self.getAttribute('domain'); var token = self.getAttribute('token'); + var exps = '113002'; + var MIP = window.MIP || {}; + var sample = MIP.hash.get('sample'); + if (sample === 'mip_wm_sample') { + exps = '113004'; + } - if(domain && token) { - - var script = document.createElement('script'); - script.src = document.location.protocol + '//' + domain + '/' + token + '.js'; - document.body.appendChild(script); + if (domain && token) { + // 判断 preload 逻辑 + var scripts = document.querySelector('script[mip-preload="mip-script-wm"]'); + if (scripts && sample === 'mip_wm_sample') { + var apiStr = '__container_api_'; + (window[apiStr] = window[apiStr] || []).push({ + containerId: token, + exps: exps, + token: token + }); + } + else { + window['_' + token] = { + exps: exps + }; + var script = document.createElement('script'); + script.src = document.location.protocol + '//' + domain + '/' + token + '.js'; + document.body.appendChild(script); - var fixedElement = require('fixed-element'); - var layer = fixedElement._fixedLayer; - var child = document.getElementById(token); - - child.addEventListener("DOMSubtreeModified", function(e) { - var elem = window.getComputedStyle(child, null); - var pos = elem && elem.getPropertyValue('position') ? - elem.getPropertyValue('position') : ''; - if(layer && layer.querySelector('#'+token)){ - return; - } - if(pos == 'fixed' && layer) { - var idx = document.querySelectorAll('mip-fixed').length; - var data = { - element: child.parentElement, - id: 'Fixed'+ idx - }; - fixedElement.moveToFixedLayer(data, parseInt(idx)); - } - },false); + var fixedElement = require('fixed-element'); + var layer = fixedElement._fixedLayer; + var child = document.getElementById(token); + child.addEventListener('DOMSubtreeModified', function (e) { + var elem = window.getComputedStyle(child, null); + var pos = elem && elem.getPropertyValue('position') + ? elem.getPropertyValue('position') : ''; + if (layer && layer.querySelector('#' + token)) { + return; + } + if (pos === 'fixed' && layer) { + var idx = document.querySelectorAll('mip-fixed').length; + var data = { + element: child.parentElement, + id: 'Fixed' + idx + }; + fixedElement.moveToFixedLayer(data, parseInt(idx, 10)); + } + }, false); + } } else { console.error('请输入正确的 domain 或者 token'); @@ -48,5 +65,5 @@ define(function (require) { return { render: render - } + }; });