Ember模板和Google AdSense

Joh*_*han 7 adsense handlebars.js ember.js

在Ember(Handlebars)模板中添加Google AdSense横幅的适当方法是什么?假设我有一个看起来像这样的视图的模板:

<script type="text/x-handlebars" data-template-name="myPageWithBanner">
  <div id="mainContent"> 
    Lorem ipsum main content
  </div>
  <div id="banner">
    //Normally I would insert a script tag here, but that is not possible
  </div>
</script>
Run Code Online (Sandbox Code Playgroud)

我是否需要使用预编译模板从代码中执行此操作,还是有一种我不知道的方法?

emk*_*emk 3

我没有 Google AdSense 帐户,因此无法对此进行测试。但这里存在几个主要问题:

  1. <script>即使您使用 CDATA,也不能在 Handlebars 模板中包含标签。
  2. Google AdSense 要求 AdSense JavaScript 逐字显示在您的网页中,否则将违反 TOS。
  3. 根据StackOverflow 的回答,目前对 AJAX 网站上的 AdSense 的支持很差。
  4. Google AdSense 抓取工具将无法看到您网页上的任何内容,因此我怀疑广告定位是否会起作用。但请参阅下文,了解一些可能对爬虫有帮助的内容。

但为了简单起见,我假设您可以直接与 Google 讨论 TOS 问题,而我将尝试解决技术问题。首先,根据StackOverflow 的回答,这里有一个可能的解决方案,可以让您逐字提供 Google 的脚本:

<script type="text/x-handlebars">
    <h1>Ember AdSense</h1>
    {{outlet}}
    <div id="ads"></div>
</script>

<script type="text/x-handlebars" data-template-name="index">
    <p>Hello, world!</p>
</script>

<div id="ads-load" style="display: none">

<!--
  Apparently this needs to appear verbatim, exactly as Google gave it to
  you, or it's a TOS violation.
-->

<script type="text/javascript"><!--
google_ad_client = "ca-pub-XXXXXXXXXX";
/* Test Ad */
google_ad_slot = "XXXXXX";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

</div>
Run Code Online (Sandbox Code Playgroud)

然后,当我们的主模板加载时,我们使用 jQuery 将广告移动到我们的应用程序模板中:

window.App = Ember.Application.create();

// Explicitly declare the view class for our application view.
App.ApplicationView = Ember.View.extend({
    // Called once the view is rendered.
    didInsertElement: function () {
        $('#ads-load').appendTo("#ads").css("display", "block");
    }
});
Run Code Online (Sandbox Code Playgroud)

至于允许Google抓取工具看到您的内容,Google对AJAX应用程序有官方建议,但我不知道这是否适用于AdSense抓取工具。或者,如果您要pushState更新显示的 URL,则需要确保在爬网程序请求时,您的服务器可以呈现每个 URL。(Discourse 论坛软件正是这样做的。)

如果它让您更接近,请告诉我。