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

标签 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。还有什么其他有值(value)的东西可以帮助你们帮助我解决这个问题吗?我真的觉得我对 Javascript 代码的更改没有生效,也不知道为什么。

最佳答案

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

另请记住,当此脚本运行时,嵌入元素不可用;因为脚本位于元素中,所以它在创建主体中的任何元素之前运行。如果要访问嵌入元素,则必须等到它被创建。您可以在 moduleDidLoad 函数中执行此操作:

function moduleDidLoad() {
  common.hideModule();

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

上面的handleMessage函数应该已经在处理来自 NaCl 模块的消息。如果您想发出警报,请尝试以下操作:

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

关于javascript - 使用 NaCl 模块(C 代码)的 Chrome 扩展程序不会接受我的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23497627/

相关文章:

javascript - 在node中使用mapv(百度的 map 工具)?

javascript - NodeJS : how to read file line by line like in Python

javascript - 计算多维数组中具有最高值属性的对象

32 位和 64 位之间完全不同的输出

c - 这个声明在 C 中意味着什么

javascript - chrome.runtime.onMessage 中的 sendResponse 不起作用

javascript - 在滚动时固定/修复 Vanilla Javascript 中的多个元素时的性能 [第 2 部分]

c - 按位连接

javascript - Chrome 扩展,将消息从注入(inject)脚本发送到后台的最佳方式

google-chrome-extension - 来自 chrome 的 Postman 应用程序扩展是用哪种语言编写的?