Google chrome extension Chrome扩展开发:如何从网页读取数据并显示在popup.html上?

Google chrome extension Chrome扩展开发:如何从网页读取数据并显示在popup.html上?,google-chrome-extension,Google Chrome Extension,所以我在做一个基本的例子,这是我对chrome扩展的要求:扩展应该能够从网页中读取数据,并在点击图标时显示在弹出窗口中 { "name": "Test", "version": "1.0", "description": "Testing", "permissions": ["file:///A:/Study/TestExt/webp.html"], "content_scripts": [

所以我在做一个基本的例子,这是我对chrome扩展的要求:扩展应该能够从网页中读取数据,并在点击图标时显示在弹出窗口中

{
          "name": "Test",
          "version": "1.0",
          "description": "Testing",
          "permissions": ["file:///A:/Study/TestExt/webp.html"],
           "content_scripts": [
            {
              "matches": ["file:///A:/Study/TestExt/webp.html"],
              "js": ["content.js"]
            }
          ],
          "background": {
            "persistent": true,
            "scripts": ["background.js"]
          },
          "browser_action": {
              "default_title": "Test",
              "default_icon": "icon.png",
              "default_popup": "popup.html"

          },
          "manifest_version": 2
        }
<html>

            <head>
             <script src= "background.js"></script>  
            </head>

            <body>

              <p id="writeWord"></p>

            </body>

            </html>
我有这些文件-
manifest.json
popup.html
background.js
content.js
webPage.html
(这是存储在本地机器上的html文件,数据将从此页面读取)

{
          "name": "Test",
          "version": "1.0",
          "description": "Testing",
          "permissions": ["file:///A:/Study/TestExt/webp.html"],
           "content_scripts": [
            {
              "matches": ["file:///A:/Study/TestExt/webp.html"],
              "js": ["content.js"]
            }
          ],
          "background": {
            "persistent": true,
            "scripts": ["background.js"]
          },
          "browser_action": {
              "default_title": "Test",
              "default_icon": "icon.png",
              "default_popup": "popup.html"

          },
          "manifest_version": 2
        }
<html>

            <head>
             <script src= "background.js"></script>  
            </head>

            <body>

              <p id="writeWord"></p>

            </body>

            </html>
manifest.json

{
          "name": "Test",
          "version": "1.0",
          "description": "Testing",
          "permissions": ["file:///A:/Study/TestExt/webp.html"],
           "content_scripts": [
            {
              "matches": ["file:///A:/Study/TestExt/webp.html"],
              "js": ["content.js"]
            }
          ],
          "background": {
            "persistent": true,
            "scripts": ["background.js"]
          },
          "browser_action": {
              "default_title": "Test",
              "default_icon": "icon.png",
              "default_popup": "popup.html"

          },
          "manifest_version": 2
        }
<html>

            <head>
             <script src= "background.js"></script>  
            </head>

            <body>

              <p id="writeWord"></p>

            </body>

            </html>
popup.html

{
          "name": "Test",
          "version": "1.0",
          "description": "Testing",
          "permissions": ["file:///A:/Study/TestExt/webp.html"],
           "content_scripts": [
            {
              "matches": ["file:///A:/Study/TestExt/webp.html"],
              "js": ["content.js"]
            }
          ],
          "background": {
            "persistent": true,
            "scripts": ["background.js"]
          },
          "browser_action": {
              "default_title": "Test",
              "default_icon": "icon.png",
              "default_popup": "popup.html"

          },
          "manifest_version": 2
        }
<html>

            <head>
             <script src= "background.js"></script>  
            </head>

            <body>

              <p id="writeWord"></p>

            </body>

            </html>
background.js

{
          "name": "Test",
          "version": "1.0",
          "description": "Testing",
          "permissions": ["file:///A:/Study/TestExt/webp.html"],
           "content_scripts": [
            {
              "matches": ["file:///A:/Study/TestExt/webp.html"],
              "js": ["content.js"]
            }
          ],
          "background": {
            "persistent": true,
            "scripts": ["background.js"]
          },
          "browser_action": {
              "default_title": "Test",
              "default_icon": "icon.png",
              "default_popup": "popup.html"

          },
          "manifest_version": 2
        }
<html>

            <head>
             <script src= "background.js"></script>  
            </head>

            <body>

              <p id="writeWord"></p>

            </body>

            </html>
chrome.runtime.onMessage.addListener(
          function(request, sender, sendResponse) {
            console.log(sender.tab ?
                        "from a content script:" + sender.tab.url :
                        "from the extension");
            if (request.textWord == "WordToBeRead")

            document.getElementById("writeWord").innerHTML = request.textWord;
          });

所以,理想情况下,当我点击扩展图标时,我应该得到带有单词“WordToBeRead”的弹出窗口,但我得到的只是空的弹出窗口。我不知道我到底出了什么问题。

在后台脚本和弹出窗口中使用相同的脚本是个坏主意。这应该是两个单独的脚本。当加载和执行扩展时,中到底显示了什么?您的内容脚本在加载后立即发送消息。此时您的弹出窗口不可能打开,因此您的弹出窗口不可能接收消息。您将需要内容脚本将数据存储在
存储.local
中,或者等待从弹出窗口接收请求数据的消息,然后将其作为响应发送。将相同的脚本用于后台脚本和弹出窗口是一个坏主意。这应该是两个单独的脚本。当加载和执行扩展时,中到底显示了什么?您的内容脚本在加载后立即发送消息。此时您的弹出窗口不可能打开,因此您的弹出窗口不可能接收消息。您需要内容脚本将数据存储在
storage.local
中,或者等待从弹出窗口接收请求数据的消息,然后将其作为响应发送。
{
          "name": "Test",
          "version": "1.0",
          "description": "Testing",
          "permissions": ["file:///A:/Study/TestExt/webp.html"],
           "content_scripts": [
            {
              "matches": ["file:///A:/Study/TestExt/webp.html"],
              "js": ["content.js"]
            }
          ],
          "background": {
            "persistent": true,
            "scripts": ["background.js"]
          },
          "browser_action": {
              "default_title": "Test",
              "default_icon": "icon.png",
              "default_popup": "popup.html"

          },
          "manifest_version": 2
        }
<html>

            <head>
             <script src= "background.js"></script>  
            </head>

            <body>

              <p id="writeWord"></p>

            </body>

            </html>