使用NaCl模块(C代码)的Chrome扩展程序不接受我的消息

时间:2014-05-06 14:31:22

标签: javascript c google-chrome-extension google-nativeclient

我已经被置于这个项目中,我需要在javascript级别捕获base64编码的pdf字符串,然后显示它。在我开始之前编写了很多代码,他已经能够将base64字符串发送到javascript级别并将其记录到控制台。我需要抓取字符串并将其存储在var中以进行解码,然后使用pdf.js来显示它。现在我已经浏览了nacl消息指南,但它似乎不起作用(我只是想让它提醒“Hello World”)以确保我得到变量。这是一些代码。

vdocs_pdf.c

    base64encode(document_buffer, document_used, base64_buf, base64_size);
    captured_base64 = CStrToVar(base64_buf);
    free(base64_buf);
    captured_flag = 1;
    struct PP_Var var = CStrToVar("hello world");
    ppb_messaging_interface->PostMessage(this_instance, var);
    LogVar(captured_base64);
    SendVar(captured_base64);

static struct PP_Var CBufToVar(const char* str, size_t len)
{
  if (ppb_var_interface) {
    return ppb_var_interface->VarFromUtf8(str, len);
  }
  return PP_MakeUndefined();
}

static struct PP_Var CStrToVar(const char* str)
{
  return CBufToVar(str, strlen(str));
}

的index.html

<!DOCTYPE html>
<html>
<!--
Copyright (c) 2012 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<head>
  <meta http-equiv="Pragma" content="no-cache">
  <meta http-equiv="Expires" content="-1">
  <title>vdocs_pdf</title>
  <script type="text/javascript" src="common.js"></script>
  <script type="text/javascript" src="vdocs_pdf.js"></script>
</head>
<body data-name="vdocs_pdf" data-tools="newlib glibc pnacl linux" data-configs="Debug Release" data-path="{tc}/{config}">
  <h1>vdocs_pdf</h1>
  <h2>Status: <code id="statusField">NO-STATUS</code></h2>
  <p>vdocs_pdf will accept the application/pdf data and pass
     it up to javascript as a base64 encoded string variable.</p>
  <h2>Output:</h2>
  <pre id="log" style="font-weight: bold"></pre>
  <!-- The NaCl plugin will be embedded inside the element with id "listener".
      See common.js.-->
  <div id="listener"></div>
</body>
</html>

vdoc_pdf.js

// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Once we load, hide the plugin
function moduleDidLoad() {
  common.hideModule();
}

// Called by the common.js module.
function handleMessage(message) {
  common.logMessage(message.data);
}

var listener = document.getElementById('listener');
listener.addEventListener
(
    "message",
  function(message) 
  { 
    alert(message.data); 
  },
  false
);

任何想法我做错了什么以及为什么我不能提醒“Hello World”?

这是我的控制台的输出,这显然意味着消息传递到javascript级别。当我发送“Hello World”字符串时它就不起作用了。

V-DOCS PDF 1.0 (JavaScript Console)! 
HandleDocumentLoad(instance(62dde6dd) url_loader(30)) 
document buffer(fed50008) used(0) max(100000) 
document buffer(fed50008) used(1193) max(100000) 
document buffer(fed50008) used(2641) max(100000) 
document buffer(fed50008) used(4089) max(100000) 
document buffer(fed50008) used(5537) max(100000) 
document buffer(fed50008) used(6985) max(100000) 
document buffer(fed50008) used(8433) max(100000) 
document buffer(fed50008) used(9881) max(100000) 
document buffer(fed50008) used(11329) max(100000) 
document buffer(fed50008) used(12777) max(100000) 
document buffer(fea40008) used(1053574) max(1600000) 
base64_size(1475013) 
JVBERi0xLjYNJeLjz9MNCjY4MiAwIG9iag08PC9MaW5lYXJpemVkIDEvTCAxMDUzNTc0L08gNjg4L0UgODMzNDYvTiAyL1QgMTA1MzA5OS9IIFsgNzA1IDM5OV0+Pg1l... 

最后一行是base64编码的pdf。还有什么能帮助你帮助我解决问题吗?我真的觉得我对Javascript代码的更改没有受到影响,也不知道为什么。

1 个答案:

答案 0 :(得分:1)

embed.addEventListener行看起来不对劲。似乎没有在任何地方定义embed。确保检查JavaScript控制台是否有错误。请参阅https://developer.chrome.com/devtools/docs/console

还要记住,当此脚本运行时,embed元素不可用;因为脚本在元素中,所以它在创建正文中的任何元素之前运行。如果要访问embed元素,则必须等到创建它。您可以在moduleDidLoad函数中执行此操作:

function moduleDidLoad() {
  common.hideModule();

  // common.naclModule is the embed element...
  common.naclModule.postMessage("foobar");
}

上面的handleMessage函数应该已经处理来自NaCl模块的消息。如果您想提醒,请尝试:

function handleMessage(message) {
  alert(message.data);
}