Skip to content
Myr

Integrate Native CPC by BuySellAds

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Replacing Carbon zone key with the direct key and install CPC ad unit on the site.

authored by Sayz Lim · committed by Sayz Lim · parent a559d62 · Browse files · 4 files+317 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+317 added−4 removed

  • other:+317 added−4 removed

4 files+317 added−4 removed

modified:docs/_static/custom.css+138 added-3 removed
added:docs/_static/native.js+131 added
1var _native = (function () {2 var _options = {}3 var _construct = function (e) {4 var defaultOptions = {5 carbonZoneKey: '',6 fallback: '',7 ignore: 'false',8 placement: '',9 prefix: 'native',10 targetClass: 'native-ad'11 }1213 if (typeof e === 'undefined') return defaultOptions14 Object.keys(defaultOptions).forEach((key, index) => {15 if (typeof e[key] === 'undefined') {16 e[key] = defaultOptions[key]17 }18 })19 return e20 }2122 var init = function (zone, options) {23 _options = _construct(options)2425 let jsonUrl = `https://srv.buysellads.com/ads/${zone}.json?callback=_native_go`26 if (_options['placement'] !== '') {27 jsonUrl += '&segment=placement:' + _options['placement']28 }29 if (_options['ignore'] === 'true') {30 jsonUrl += '&ignore=yes'31 }3233 let srv = document.createElement('script')34 srv.src = jsonUrl35 document.getElementsByTagName('head')[0].appendChild(srv)36 }3738 var carbon = function (e) {39 let srv = document.createElement('script')40 srv.src = '//cdn.carbonads.com/carbon.js?serve=' + e['carbonZoneKey'] + '&placement=' + e['placement']41 srv.id = '_carbonads_js'4243 return srv44 }4546 var sanitize = function (ads) {47 return ads48 .filter(ad => {49 return Object.keys(ad).length > 050 })51 .filter(ad => {52 return ad.hasOwnProperty('statlink')53 })54 }5556 var pixel = function (p, timestamp) {57 let c = ''58 if (p) {59 p.split('||').forEach((pixel, index) => {60 c += `<img src="${pixel.replace('[timestamp]', timestamp)}" style="display:none;" height="0" width="0" />`61 })62 }63 return c64 }6566 var options = function () {67 return _options68 }6970 return {71 carbon: carbon,72 init: init,73 options: options,74 pixel: pixel,75 sanitize: sanitize76 }77})({})7879var _native_go = function (json) {80 let options = _native.options()81 let ads = _native.sanitize(json['ads'])82 let selectedClass = document.querySelectorAll('.' + options['targetClass'])8384 if (ads.length < 1) {85 selectedClass.forEach((className, index) => {86 let selectedTarget = document.getElementsByClassName(options['targetClass'])[index]8788 if (options['fallback'] !== '' || options['carbonZoneKey'] !== '') selectedTarget.setAttribute('data-state', 'visible')89 selectedTarget.innerHTML = options['fallback']90 if (options['carbonZoneKey'] !== '') selectedTarget.appendChild(_native.carbon(options))91 })9293 // End at this line if no ads are found, avoiding unnecessary steps94 return95 }9697 selectedClass.forEach((className, index) => {98 let selectedTarget = document.getElementsByClassName(options['targetClass'])[index]99 let adElement = selectedTarget.innerHTML100 let prefix = options['prefix']101 let ad = ads[index]102103 if (ad && className) {104 let adInnerHtml = adElement105 .replace(new RegExp('#' + prefix + '_bg_color#', 'g'), ad['backgroundColor'])106 .replace(new RegExp('#' + prefix + '_bg_color_hover#', 'g'), ad['backgroundHoverColor'])107 .replace(new RegExp('#' + prefix + '_company#', 'g'), ad['company'])108 .replace(new RegExp('#' + prefix + '_cta#', 'g'), ad['callToAction'])109 .replace(new RegExp('#' + prefix + '_cta_bg_color#', 'g'), ad['ctaBackgroundColor'])110 .replace(new RegExp('#' + prefix + '_cta_bg_color_hover#', 'g'), ad['ctaBackgroundHoverColor'])111 .replace(new RegExp('#' + prefix + '_cta_color#', 'g'), ad['ctaTextColor'])112 .replace(new RegExp('#' + prefix + '_cta_color_hover#', 'g'), ad['ctaTextColorHover'])113 .replace(new RegExp('#' + prefix + '_desc#', 'g'), ad['description'])114 .replace(new RegExp('#' + prefix + '_index#', 'g'), prefix + '-' + ad['i'])115 .replace(new RegExp('#' + prefix + '_img#', 'g'), ad['image'])116 .replace(new RegExp('#' + prefix + '_small_img#', 'g'), ad['smallImage'])117 .replace(new RegExp('#' + prefix + '_link#', 'g'), ad['statlink'])118 .replace(new RegExp('#' + prefix + '_logo#', 'g'), ad['logo'])119 .replace(new RegExp('#' + prefix + '_color#', 'g'), ad['textColor'])120 .replace(new RegExp('#' + prefix + '_color_hover#', 'g'), ad['textColorHover'])121 .replace(new RegExp('#' + prefix + '_title#', 'g'), ad['title'])122123 selectedTarget.innerHTML = null124 selectedTarget.innerHTML += adInnerHtml + _native.pixel(ad['pixel'], ad['timestamp'])125 selectedTarget.setAttribute('data-state', 'visible')126 } else {127 selectedTarget.innerHTML = null128 selectedTarget.style.display = 'none'129 }130 })131}
modified:docs/_templates/hacks.html+47 added
modified:docs/_templates/sidebarintro.html+1 added-1 removed
14 unmodified lines
15</p>16<p>Sponsored by <strong><a href="https://linode.com/">Linode</a></strong> and <a href="http://docs.python-requests.org/en/master/community/sponsors/#patron-sponsors">other wonderful organizations</a>.</p>171819<script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?zoneid=1673&serve=C6AILKT&placement=pythonrequestsorg" id="_carbonads_js"></script>19<script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?zoneid=1673&serve=CKYI5K3E&placement=pythonrequestsorg" id="_carbonads_js"></script>2021<p><a href="http://www.unixstickers.com/stickers/coding_stickers/requests-shaped-sticker">Requests Stickers!</a></p>22<h3>Stay Informed</h3>23<p>Receive updates on new releases and upcoming projects.</p>
48 unmodified lines