我试图通过Chrome sendNativeMessage函数将一些数据发送到我的原生Python应用程序,但到目前为止都没有成功。
以下是我的Javascript:
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
var email = {text:request};
alert(email["text"]) // showing that i successfully received message
chrome.runtime.sendNativeMessage('com.whatever.whatever.xyz',
email[0],
function(response) {
console.log("Received " + response);
});
});
以下是我的Python:
import win32com.client
import struct
import sys
import json
import datetime
import time
# On Windows, the default I/O mode is O_TEXT. Set this to O_BINARY
# to avoid unwanted modifications of the input/output streams.
import os, msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
# Read the message length (first 4 bytes).
#for line in sys.stdin:
text_length_bytes = sys.stdin.read(4)
# Unpack message length as 4 byte integer.
#text_length = struct.unpack('i', text_length_bytes.encode('utf-8'))[0]
text_length = struct.unpack('i', text_length_bytes)[0]
# Read the text of the message.
#text = sys.stdin.read(text_length)
text = sys.stdin.read(text_length).decode('utf-8')
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "Whatever"
newMail.HTMLBody = text
newMail.display()
奇怪的是,我可以通过稍微调整Python来成功发送硬编码值(参见注释掉的" text_length ="和#34; text ="行)并更改Javascript传递{text:" Hello world!"而不是电子邮件[0]。但是当我传递一个物体时,它失败了。
我觉得我在这里缺少一些基本的东西。有人可以帮我理解吗?
答案 0 :(得分:0)
gdb ./program
(gdb) run
<segfault happens here>
(gdb) backtrace
<offending code is shown here>
Isn真的不是一个数组吗?是吗?