React Chrome 扩展:从 popup.tsx 发送消息到内容脚本错误
P粉549412038
P粉549412038 2024-04-01 09:13:33
[React讨论组]

我正在尝试做一个 chrome 扩展,我想通过单击 popup.tsx 的按钮来发送消息到内容脚本,但我收到此错误: 错误处理响应:TypeError:无法读取未定义的属性(读取“再见”)

这是我的代码:

按钮组件:

    function TWCredibility() {
      const ValidateTwitterTweets = () => {
        // Send message to content script to perfom Twitter Api validation
        chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
          chrome.tabs.sendMessage(
            tabs[0].id as number,
            { sender: "www", instruction: "api" },
            function (response) {
              alert(response.farewell);
            }
          );
        });
      };
    
      return (
        
); }

Popup.tsx:

const Popup = () => {

  return (
    

T-CREo v2.0.0

{/* Plain Text Credibility */}
{" "}
{/* TW Credibility */} {/* Spinner */}
); };

内容脚本:

chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
  // If sender is 'www' and instruction is 'api', then scraping
  if (msg.sender === "www" && msg.instruction === "api") {
    console.log("Message received from www");
    alert("Message received from www");
    sendResponse({ farewell: "to-do" });
  }
});

清单 v3:

{
    "manifest_version": 3,
    "name": "T-CREo v2.0",
    "description": "sí",
    "version": "1.0.0",
    "action" : {
        "default_popup": "popup.html",
        "default_title": "T-CREo v2.0",
        "default_icon" : "icon.png"
    },
    "permissions": [
        "activeTab",
        "background",
        "contextMenus",
        "declarativeContent",
        "tabs",
        "storage",
        "scripting"
    ],
    "icons": {
        "16" : "icon.png",
        "48" : "icon.png",
        "128" : "icon.png"
    },
    "options_page": "options.html",
    "background": {
        "service_worker": "background.js"
    },
    "content_scripts": [
        {
            "matches": [""],
            "js": ["contentScript.js"]
        }
    ]
}

我得到的错误: Chrome 扩展程序出错

P粉549412038
P粉549412038

全部回复(1)
P粉366946380

更新扩展程序后您是否刷新了选项卡?您是否还尝试在 HTTP/HTTPS 网站上打开扩展程序?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号