什么是这个错误消息对我的chrome扩展,从popup.html的意思,如何修复它?

6qftjkof  于 2023-01-28  发布在  Go
关注(0)|答案(1)|浏览(140)

正如你已经在标题中看到的,我的问题与popup.html有关
我不知道是什么问题请帮帮我!
这是错误代码
未检查运行时。lastError:无法建立连接。接收端不存在。堆栈跟踪弹出窗口。html:0(匿名功能)
popup.html为:

<!DOCTYPE html>
<html>
  <head>
    <style>
      #popup-container {
        width: 600px;
        height: 800px;
      }
    </style>
    <title>DissBott</title>
  </head>
  <body>
    <h1>ChatGPT Extension</h1>
    <p>input</p>
    <textarea id="input"></textarea>
    <br>
    <button id="submitButton">Submit</button>
    <button id="clearButton">Clear</button>
    <br>
    <p>Response:</p>
    <p id="response"></p>
    <script src="popup.js"></script>
  </body>
</html>

js为:

// Popup Script
//background.js

const submitButton = document.getElementById("submitButton");
const clearButton = document.getElementById("clearButton");
const promptTextarea = document.getElementById("prompt");
const responseParagraph = document.getElementById("response");

submitButton.addEventListener("click", function() {
  // onClick's logic below:
  // Get the prompt from the textarea
  const prompt = input;
  // Send the message to the background script
  chrome.runtime.sendMessage({
    message: "clicked_browser_action",
    prompt: prompt
  }, function(response) {
    // Display the response
    responseParagraph.innerText = response;
  });
});

clearButton.addEventListener("click", function() {
  // Clear the textarea
  promptTextarea.value = "";
  // Clear the response
  responseParagraph.innerText = "";
});

我的清单. json

{
  "manifest_version": 3,
  "name": "DissBot",
  "description": "This is an extension that uses the ChatGPT model.",
  "version": "1.0",
  "host_permissions": [
    "https://api.openai.com/"
  ],
  "action": {
    "default_popup": "popup.html"
    },
    "background": {
     "script": ["bg.js"]
    }
}

我试着做一个Chrome扩展,但没有成功。

wfauudbj

wfauudbj1#

下面是chrome.runtime.sendMessage的一个示例。
manifest.json

{
  "name": "hoge",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "action": {
    "default_popup": "popup.html"
  }
}

popup.html

<html>
  <body>
    <button id="send">Send</button>
    <script src="popup.js"></script>
  </body>
</html>

popup.js

document.getElementById("send").onclick = () => {
  chrome.runtime.sendMessage("send", (respose) => {
    console.log(respose);
  });
}

background.js

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  console.log(message);
  sendResponse("response");
  return true;
});

相关问题