我使用原生消息扩展。我将一个字符串发送到 native 应用程序,因此在我的 native 应用程序中对其进行加密并将加密的字符串发送到网页。
这是我的 content_script.js:
window.addEventListener("message", function(event) {
if (event.source == window &&
event.data &&
event.data.direction == "from-page-script") {
alert("send");
browser.runtime.sendMessage(event.data);
}
});
function Sign(challenge) {
var j_Data = {}
j_Data["command"] = "sign";
j_Data["challenge"] = challenge;
var j_Text = JSON.stringify(j_Data)
window.postMessage
({
direction: "from-page-script",
message: j_Text,
}, "*");
//response = document.getElementById("result").value;
}
/*
Send a message to the page script.
*/
function messagePageScript(request) {
var str_Text = request["text"];
var j_Text = JSON.parse(strText);
var str_Command = jText["command"];
if(str_Command == "signed")
{
var str_Signed_Data = jText["signeddata"];
}
}
browser.runtime.onMessage.addListener(function(request, sender, sendResponse) {
messagePageScript(request);
});
它工作正常。
当我收到消息时 messagePageScript(request);
被调用。
如何在我的 html 页面中使用 str_Signed_Data
的值?
最佳答案
从 native 应用程序获取数据后,您必须使用 JavaScript 回调来返回数据。
function KeyA3_Sign(challenge, funcSign) {
...
kHandler = getResponse.bind(null, funcSign);
window.addEventListener("message", kHandler);
window.postMessage(jsonData, "*");
}
function getResponse(func, event) {
console.log("Call Get response");
func(event.data["text"]);
}
关于javascript - 如何在 html 页面中使用从 native 应用程序接收的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50780758/