Skip to content

Commit

Permalink
update:ad 网盟广告优化 (#910)
Browse files Browse the repository at this point in the history
* add sample logic

* update:ad 优化

* update:ad 优化

* update:ad 优化
  • Loading branch information
momofan1986 authored and dongshihao committed Nov 22, 2017
1 parent f39a257 commit f316132
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 96 deletions.
144 changes: 80 additions & 64 deletions src/mip-ad/mip-ad-baidu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* 网盟插件
*
* @file 网盟插件
* @author [email protected]
* @version 1.0
* @copyright 2016 Baidu.com, Inc. All Rights Reserved
Expand All @@ -9,100 +8,121 @@ 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 = '<div style="" id="' + s + '"></div>';
$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;

}

/**
* [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 = '<div style="" id="' + s + '"></div>';
$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);
Expand All @@ -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
}
};
});


81 changes: 49 additions & 32 deletions src/mip-ad/mip-baidu-wm-ext.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,62 @@
/**
* 广告扩展组件
*
* @author [email protected]
* @file 广告扩展组件
* @author [email protected]
* @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');
Expand All @@ -48,5 +65,5 @@ define(function (require) {

return {
render: render
}
};
});

0 comments on commit f316132

Please sign in to comment.