0

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

我有一个 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);        

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

4

1 回答 1

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

于 2013-05-15T15:19:25.303 回答