Hack Forums
[Javascript] Inject Adsense Dynamically - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [Javascript] Inject Adsense Dynamically (/showthread.php?tid=5939186)



Inject Adsense Dynamically - Wilkyfreeflow - Feb 8, 2019

Hello everyone,

First off, its my first time posting here, so I apologize if I break any forum rules.

Now, I am trying to make an injector to append Adsense units into any website using javascript. My idea was to have a chrome extension that runs at document_start, and append Adsense with this code:

Code
function injectTortugaAds (pubID, adSlot)
{
    var actualCode = '(adsbygoogle = window.adsbygoogle || []).push({google_ad_client: "ca-pub-' + pubID + '", enable_page_level_ads: true});';
    var script = document.createElement('script');
    script.textContent = actualCode;
    (document.head || document.documentElement).appendChild(script);

    //------------------------------------------------------------

    var externalScript = document.createElement("script");
    externalScript.type = "text/javascript";
    externalScript.setAttribute('async', 'async');
    externalScript.src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
    document.getElementsByTagName('body')[0].appendChild(externalScript);

    var ins = document.createElement("ins");
    ins.setAttribute('class', 'adsbygoogle');
    ins.setAttribute('style', 'display:block;');/*add other styles if required*/
    ins.setAttribute('data-ad-client', 'ca-pub-' + pubID);
    ins.setAttribute('data-ad-slot', adSlot);
    ins.setAttribute('data-ad-format', 'auto');
    document.getElementsByTagName('body')[0].appendChild(ins);

    var inlineScript = document.createElement("script");
    inlineScript.type = "text/javascript";
    inlineScript.text = '(adsbygoogle = window.adsbygoogle || []).push({});'
    document.getElementsByTagName('body')[0].appendChild(inlineScript);
    //--------------------------------------------------------------
    [].forEach.call(document.querySelectorAll('.adsbygoogle'), function () {
        (adsbygoogle = window.adsbygoogle || []).push({});
    });

}
Either way, it doesn't work. Do you have any ideas on how to inject Adsense? Or is this a fool's errand?
Thank you for any help!

Note: I am aware this is against Adsense ToS. I do not intend to misuse this code :)


RE: Inject Adsense Dynamically - Lynx - Feb 9, 2019

I think your adsense account (as a monetizer) is locked to one domain as you have to specify that on account creation;

[Image: demSL8t.png]