Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Create custom element
- * @param {object} doc : document object
- * @param {boolean} isTestAds : set testing ads status [true|false|0|!0|1|!1]
- * @param {array} excludePages : set excluded pages option ex : ["homepage", "blog","treemap_k"], set this value on the bottom of this function block
- * @returns adsense rendered ads
- */
- (function(doc, isTestAds, excludePages) {
- /** ADSENSE CONFIGURATION OBJECT
- * class : class style for top position
- * adslot : adslot id from adsense dashboard
- */
- const adsenseCfg = [{
- class: "top",
- adslot: "1672900703"
- },
- {
- class: "middle",
- adslot: "8511440099"
- },
- {
- class: "bottom",
- adslot: "9527355901"
- }
- ]
- /** GET WINDOW SCREEN AND PATHNAME */
- const windowScreenWidth = window.screen.width;
- var pathname = window.location.pathname;
- pathname = pathname === "/" ? "homepage" : pathname; // "/" = homepage
- /** EXCLUDE ADSENSE FROM SELECTED PAGES
- * excludePages : array variable fill by excluded pages path
- */
- if (excludePages.length > 0 && excludePages.find(e => "/" === e || e.match(new RegExp(`\\b(${pathname})\\b`, "ig")))) return;
- /** STYLING PART */
- const style = `
- @media screen and (max-width: 1220px) {
- .mav-leaderboard-container{display:flex;justify-content:center;width:728px;margin:40px auto 40px auto;}
- .mav-leaderboard-container>ins{width:728px;height:90px}
- }
- @media screen and (max-width: 750px) {
- body{display: table;}
- .mav-leaderboard-container{display:flex;justify-content:center;width: 80%;margin:40px auto 40px auto;}
- .mav-leaderboard-container>ins{width:428px;height:90px}
- }
- @media screen and (max-width: 376px) {
- .mav-leaderboard-container{display:flex;justify-content:center;width: 80%;margin:40px auto 40px auto;}
- .mav-leaderboard-container>ins{width:370px;height:90px}
- body{display: table;}
- }
- `
- /**
- * Create custom element
- * @param {string} tag
- * @param {array} attr
- * @returns el
- */
- const createContainer = function(tag, attr) {
- let el = doc.createElement(tag);
- Object.assign(el, attr);
- if (attr.datasetAdClient !== '') {
- }
- return el;
- }
- /** GOOGLE ADSENSE CLIENT ID */
- const adClient = "ca-pub-6404021102530936";
- /** ADNSE SCRIPT TAG LOADER */
- var adsenseLoader = createContainer("script", {
- async: true,
- src: "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-6404021102530936",
- crossOrigin: "anonymous"
- })
- var adStyle = createContainer("style", {}); // create style container and add with style constant value
- var adsWrapper = null; // custom adsense wrapper container
- var googleInsTag = null; // adnsense tag
- var googleScript = null; // adsense script loader container
- adStyle.innerHTML = style;
- doc.head.insertAdjacentElement("beforeend", adsenseLoader);
- doc.head.insertAdjacentElement("beforeend", adStyle);
- for (const prop of adsenseCfg) {
- googleInsTag = createContainer("ins", {
- className: "adsbygoogle leaderboard",
- style: "display:block"
- })
- googleScript = createContainer("script", {})
- googleScript.innerText = `(adsbygoogle = window.adsbygoogle || []).push({});`;
- googleInsTag.dataset.adSlot = prop.adslot;
- googleInsTag.dataset.adClient = adClient;
- isTestAds ? (googleInsTag.dataset.adtest = "on") : ''; // set adtest status ( testing camapaign from google )
- googleInsTag.appendChild(googleScript); // add google object container
- adsWrapper = createContainer("div", {
- className: `mav-leaderboard-container ${prop.class}`
- })
- adsWrapper.appendChild(googleInsTag);
- switch (prop.class) {
- case "top":
- if (pathname == "/") { // only show in homepage
- doc.querySelector(".logo-container").insertAdjacentElement("afterend", adsWrapper);
- }
- break;
- case "middle":
- if (pathname == "/") { // only show in homepage
- let idTarget = windowScreenWidth > 720 ? "#indexes" : "#news-indicators";
- doc.querySelector(idTarget).insertAdjacentElement("afterend", adsWrapper);
- }
- break;
- case "bottom":
- doc.querySelector("#content, body").insertAdjacentElement("beforeend", adsWrapper);
- break;
- }
- }
- })(document, true, []);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement