【问题标题】:How do i add adsense code in chrome extension?如何在 Chrome 扩展中添加 AdSense 代码?
【发布时间】:2013-05-15 23:19:25
【问题描述】:

我想在 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);        

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

【问题讨论】:

    标签: google-chrome-extension adsense


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      相关资源
      最近更新 更多