简体   繁体   English

动态更新popup.html内容

[英]update popup.html content dynamically

I am sending a message from the contentscript to popup and trying to show the received message when the extension is clicked. 我正在从内容脚本发送消息到弹出窗口,并试图在单击扩展名时显示收到的消息。

在此处输入图片说明

I can see that the message is being received only when I Inspect popup 我可以看到仅当我检查弹出窗口时才收到消息
在此处输入图片说明

contentscript.js contentscript.js

    console.log("content script");      

    chrome.runtime.sendMessage("hello",function(response)
    {
        console.log("sending message");        
    });

popup.js popup.js

console.log("popup script");   

function onReq(request, sender, sendResponse)
{
  ph=request;
    console.log("msg: "+request);
  document.getElementById("para").innerHTML = "msg: "+request;
}

chrome.runtime.onMessage.addListener(onReq);

popup.html popup.html

<!doctype html>
<html>
  <head>
    <title>Example</title>

  </head>
  <body>
    <div id="status"></div>
    <p id="para">shows received message here</p>
  </body>
   <script src="popup.js"></script>
</html>

I want to receive the message and show the message when clicked without having to open the console. 我想接收该消息并在单击时显示该消息,而不必打开控制台。 How can I achieve this. 我该如何实现。

The popup window is created/destroyed each time you open/close the popup. 每次打开/关闭弹出窗口都会创建/销毁弹出窗口。 So sending messages to it while closed will not work. 因此,在关闭时向其发送消息将不起作用。 A simple solution would be to store the latest message in chrome.storage , and read the value from the popup. 一个简单的解决方案是将最新消息存储在chrome.storage中 ,然后从弹出窗口中读取值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM