Pankaj Khurana
Pankaj Khurana

Reputation: 3271

How do i add adsense code in chrome extension?

I want to add adsense code in chrome extension (manifest_version:2).

I have a popup.html page in which i have a dedicated a block for ads:

<div id="adblock">
            <script type="text/javascript"><!--google_ad_client = "ca-pub-xxxxxx";/* Getfiles Chrome Extn Banner 468x60 */google_ad_slot = "xxxxxx"; google_ad_width = 468;google_ad_height = 60;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
        </div>

But when it is executed it generates following error:

Refused to load the script 'http://pagead2.googlesyndication.com/pagead/show_ads.js' because it violates the following Content Security Policy directive: "script-src 'self' https://pagead2.googlesyndication.com/pagead/show_ads.js".

It is due to the new content security policy which prohibits inline javascript .

To overcome this i have tried to create that code in popup.js file

document.addEventListener('DOMSubtreeModified', function() {

                var x = chrome.extension.getViews({type:"popup"});
                rowOutput='<script type="text/javascript"><!--google_ad_client = "ca-pub-xxxx";/* Getfiles Chrome Extn Banner 468x60 */google_ad_slot = "xxxx"; google_ad_width = 468;google_ad_height = 60;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';
                if (x.length>0){

                  x[0].document.getElementById('adblock').innerHTML=rowOutput;
                 }
}, true);        

When i reload the extension and click the extension icon the browser hangs and popup doesnot open.

Upvotes: 0

Views: 599

Answers (1)

Leonardo Ciaccio
Leonardo Ciaccio

Reputation: 3126

In manifest add this

"content_security_policy": "object-src 'unsafe-eval'; script-src 'unsafe-eval' https://pagead2.googlesyndication.com; connect-src *"

only https then add "s" change

http://pagead2.googlesyndication.com

with

https://pagead2.googlesyndication.com

Upvotes: 1

Related Questions