【问题标题】:Why does caja sanitization of Google Apps Script htmloutput fail on particular strings为什么 Google Apps Script htmloutput 的 caja sanitization 在特定字符串上失败
【发布时间】:2012-10-27 11:51:50
【问题描述】:

我正在尝试准备 GAS 代码示例以嵌入到 Google 协作平台和其他网站中。我使用 HtmlService.createHtmlOutput 和 HtmlService.createTemplateFromFile() 和 template.evaluate().getContent() 来提供 google 应用程序脚本内容的 html 版本。根据this post,一切正常。

现在我想使用 prettyify.js 美化代码。我使用的是embedded in caja 版本,它几乎可以工作。然而,被美化的代码中的特定方法名称会从 htmlservice 引发错误。

不支持来宾构造对象对象的驯化。期待一个 函数不是字符串:pln

具体来说,这段文字美化成功,

function xisItHtml (e) {
  return ( e.parameter.hasOwnPropertu('template')) ;
}

而这会引发错误

function xisItHtml (e) {
  return ( e.parameter.hasOwnProperty('template')) ;
}

似乎特定的方法(奇怪,因为没有执行此代码,只是美化了),导致 caja sanitation 抱怨。

模板中的代码就是这样

$(document).ready(function () {
 // any jQueryness can happen here...
  try { 
    prettyPrint();
  }
  catch(err) {
    alert("failed prettification " + err);
  }
});

我非常难过。有什么想法吗?

【问题讨论】:

  • 工作示例和抛出错误的示例看起来相同
  • 有效的 hasOwnProperty 拼写错误。如果你拼写正确,它会失败。可能会检查注入到 html 正文中的代码?
  • 马丁。如果您将 caja 示例复制到 GAS html 模板中并执行它,示例 1 可以工作。其他 2 个没有。

标签: google-apps-script google-caja google-code-prettify


【解决方案1】:

这可能是您调用美化器的方式。这里是an example which appears to render correctly in the Caja Playground

【讨论】:

【解决方案2】:

因此,当您将包含 .toString() 或 .hasOwnProperty() 的美化文本插入 GAS 中的 html 输出时,似乎出现了故障。我找不到任何其他导致问题的文本,但可能还有更多。我的技巧只是在美化之前更改文本,然后再将其更改回来。

$(document).ready(function () {
   // any jQueryness can happen here...
    var thingsThatScrewUp = [ 'toString', 'hasOwnProperty'],t;

    $('.pretty').each( function(i,elem) {
       var c = $(elem).text() ;
       // disguise
       for (var i = 0 ; i < thingsThatScrewUp.length ; i++ ) {
        c = c.replace(new RegExp("." + thingsThatScrewUp[i], 'g'),".sandw_" + i + "_ch");
       }
       try {
         t = prettyPrintOne(c);
       }
       catch (err) {
        $('#report').html(err + c);
       }
       // undisguise
       for (var i = 0 ; i < thingsThatScrewUp.length ; i++ ) {
         t = t.replace(new RegExp("sandw_" + i + "_ch", 'g'),thingsThatScrewUp[i]);
       }
       $(elem).html("<pre class='code pretty prettyprint'>" + t + "</pre>");
      });
      $('#working').html('Module:');
  });

这是Working version

【讨论】:

  • Authorization is required to perform that action.
猜你喜欢
  • 2021-02-11
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
相关资源
最近更新 更多