Skip to content

Instantly share code, notes, and snippets.

@egorvinogradov
Last active December 14, 2015 12:59
Show Gist options
  • Save egorvinogradov/5090182 to your computer and use it in GitHub Desktop.
Save egorvinogradov/5090182 to your computer and use it in GitHub Desktop.
<div id="yandex_ad"></div>
<script type="text/javascript">
(function(w, d, n, s, t) {
w[n] = w[n] || [];
w[n].push(function() {
Ya.Direct.insertInto(108006, "yandex_ad", {
site_charset: "utf-8",
ad_format: "direct",
font_size: 1,
type: "horizontal",
limit: 4,
title_font_size: 3,
site_bg_color: "FFFFFF",
header_bg_color: "FEEAC7",
title_color: "0000CC",
url_color: "006600",
text_color: "000000",
hover_color: "0066FF"
});
});
t = d.getElementsByTagName('head')[0];
s = d.createElement("script");
s.type = "text/javascript";
s.src = "http://an.yandex.ru/system/context.js";
s.setAttribute("async", "true");
t.insertBefore(s, t.firstChild);
})(window, document, "yandex_context_callbacks");
/* Parser */
var yandexAds = {
init: function(){
if ( !window.localStorage || !document.querySelector ) {
return;
}
function isEqual(obj1, obj2){
function size(obj){
var size = 0;
for ( var prop in obj ) {
size++;
}
return size;
};
if ( size(obj1) === size(obj2) ) {
for ( var prop in obj1 ) {
if ( obj1[prop] !== obj2[prop] ) {
return false;
}
}
return true;
}
return false;
}
function proxy(func, context){
return function(){
func.apply(context || this, arguments);
};
};
this.storedData = JSON.parse(localStorage.getItem('yandex-ad-data'));
this.items = document.querySelectorAll('.ya-partner__item');
if ( this.items.length ) {
this.data = this.getAdsData(this.items);
if ( isEqual(this.data, this.storedData) ) {
this.sendData(this.data);
localStorage.setItem('yandex-ad-data', JSON.stringify(this.data));
}
}
else {
setTimeout(proxy(this.init, this), 1000);
}
},
getAdsData: function(items){
var itemsArr = [];
for ( var i = 0, l = items.length; i < l; i++ ) {
itemsArr.push( this.parseBlock(items[i]) );
}
return itemsArr;
},
parseBlock: function(container){
return {
title: container.querySelector('.ya-partner__title-link-text').innerText,
text: container.querySelector('.ya-partner__text').innerText,
url: container.querySelector('.ya-partner__domain').innerText
};
},
sendData: function(data){
new Image().src = '/hc/context_tracker?data=' + encodeURIComponent(JSON.stringify(data));
},
};
yandexAds.init();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment