动态更新 popup.html 内容

update popup.html content dynamically

我正在从 contentscriptpopup 发送消息,并尝试在单击扩展时显示收到的消息。

只有当我检查弹出窗口
时,我才能看到正在接收消息

contentscript.js

    console.log("content script");      

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

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

<!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>

我想接收消息并在单击时显示消息,而无需打开控制台。 我怎样才能做到这一点。

弹出 window 是 created/destroyed 每次你 open/close 弹出。因此,在关闭时向其发送消息将不起作用。一个简单的解决方案是将最新消息存储在 chrome.storage 中,然后从弹出窗口中读取值。