如何在Chrome扩展程序中添加adsense代码?

时间:2012-08-13 11:09:08

标签: google-chrome-extension adsense

我想在Chrome扩展程序中添加adsense代码(manifest_version:2)。

我有一个popup.html页面,其中我有一个专门的广告块:

<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>

但是当它被执行时会产生以下错误:

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".

这是由于新的内容安全政策禁止内联javascript。

为了解决这个问题,我试图在popup.js文件中创建该代码

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);        

当我重新加载扩展程序并单击扩展程序图标时,浏览器挂起并且弹出窗口无法打开。

1 个答案:

答案 0 :(得分:1)

在清单中添加此

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

仅https然后添加“s” 改变

  

http://pagead2.googlesyndication.com

  

https://pagead2.googlesyndication.com