Advertisement
coentoro

mavefund adsense script unpack + documentation

Oct 24th, 2023 (edited)
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.86 KB | Source Code | 0 0
  1. /**
  2.  * Create custom element
  3.  * @param {object} doc : document object
  4.  * @param {boolean} isTestAds : set testing ads status [true|false|0|!0|1|!1]
  5.  * @param {array} excludePages : set excluded pages option ex : ["homepage", "blog","treemap_k"], set this value on the bottom of this function block
  6.  * @returns adsense rendered ads
  7.  */
  8. (function(doc, isTestAds, excludePages) {
  9.     /** ADSENSE CONFIGURATION OBJECT
  10.      *  class : class style for top position
  11.      *  adslot : adslot id from adsense dashboard
  12.      */
  13.     const adsenseCfg = [{
  14.             class: "top",
  15.             adslot: "1672900703"
  16.         },
  17.         {
  18.             class: "middle",
  19.             adslot: "8511440099"
  20.         },
  21.         {
  22.             class: "bottom",
  23.             adslot: "9527355901"
  24.         }
  25.     ]
  26.  
  27.     /** GET WINDOW SCREEN AND PATHNAME */
  28.     const windowScreenWidth = window.screen.width;
  29.     var pathname = window.location.pathname;
  30.     pathname = pathname === "/" ? "homepage" : pathname; // "/" = homepage
  31.  
  32.     /** EXCLUDE ADSENSE FROM SELECTED PAGES
  33.      *  excludePages : array variable fill by excluded pages path
  34.      */
  35.     if (excludePages.length > 0 && excludePages.find(e => "/" === e || e.match(new RegExp(`\\b(${pathname})\\b`, "ig")))) return;
  36.  
  37.     /** STYLING PART */
  38.     const style = `
  39.               @media screen and (max-width: 1220px) {
  40.                 .mav-leaderboard-container{display:flex;justify-content:center;width:728px;margin:40px auto 40px auto;}
  41.                 .mav-leaderboard-container>ins{width:728px;height:90px}
  42.               }
  43.               @media screen and (max-width: 750px) {
  44.                 body{display: table;}  
  45.                 .mav-leaderboard-container{display:flex;justify-content:center;width: 80%;margin:40px auto 40px auto;}
  46.                 .mav-leaderboard-container>ins{width:428px;height:90px}
  47.               }
  48.  
  49.               @media screen and (max-width: 376px) {
  50.                 .mav-leaderboard-container{display:flex;justify-content:center;width: 80%;margin:40px auto 40px auto;}
  51.                 .mav-leaderboard-container>ins{width:370px;height:90px}
  52.                 body{display: table;}  
  53.               }
  54.     `
  55.         /**
  56.          * Create custom element
  57.          * @param {string} tag
  58.          * @param {array} attr
  59.          * @returns el
  60.          */
  61.     const createContainer = function(tag, attr) {
  62.         let el = doc.createElement(tag);
  63.         Object.assign(el, attr);
  64.         if (attr.datasetAdClient !== '') {
  65.  
  66.         }
  67.         return el;
  68.     }
  69.  
  70.     /** GOOGLE ADSENSE CLIENT ID */
  71.     const adClient = "ca-pub-6404021102530936";
  72.  
  73.     /** ADNSE SCRIPT TAG LOADER */
  74.     var adsenseLoader = createContainer("script", {
  75.         async: true,
  76.         src: "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-6404021102530936",
  77.         crossOrigin: "anonymous"
  78.     })
  79.     var adStyle = createContainer("style", {}); // create style container and add with style constant value
  80.     var adsWrapper = null; // custom adsense wrapper container
  81.     var googleInsTag = null; // adnsense tag
  82.     var googleScript = null; // adsense script loader container
  83.  
  84.     adStyle.innerHTML = style;
  85.     doc.head.insertAdjacentElement("beforeend", adsenseLoader);
  86.     doc.head.insertAdjacentElement("beforeend", adStyle);
  87.  
  88.  
  89.     for (const prop of adsenseCfg) {
  90.         googleInsTag = createContainer("ins", {
  91.             className: "adsbygoogle leaderboard",
  92.             style: "display:block"
  93.         })
  94.         googleScript = createContainer("script", {})
  95.         googleScript.innerText = `(adsbygoogle = window.adsbygoogle || []).push({});`;
  96.         googleInsTag.dataset.adSlot = prop.adslot;
  97.         googleInsTag.dataset.adClient = adClient;
  98.         isTestAds ? (googleInsTag.dataset.adtest = "on") : ''; // set adtest status ( testing camapaign from google )
  99.         googleInsTag.appendChild(googleScript); // add google object container
  100.  
  101.         adsWrapper = createContainer("div", {
  102.             className: `mav-leaderboard-container  ${prop.class}`
  103.         })
  104.  
  105.         adsWrapper.appendChild(googleInsTag);
  106.  
  107.         switch (prop.class) {
  108.             case "top":
  109.                 if (pathname == "/") { // only show in homepage
  110.                     doc.querySelector(".logo-container").insertAdjacentElement("afterend", adsWrapper);
  111.                 }
  112.                 break;
  113.             case "middle":
  114.                 if (pathname == "/") { // only show in homepage
  115.                     let idTarget = windowScreenWidth > 720 ? "#indexes" : "#news-indicators";
  116.                     doc.querySelector(idTarget).insertAdjacentElement("afterend", adsWrapper);
  117.                 }
  118.                 break;
  119.             case "bottom":
  120.                 doc.querySelector("#content, body").insertAdjacentElement("beforeend", adsWrapper);
  121.                 break;
  122.  
  123.         }
  124.     }
  125. })(document, true, []);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement