相关疑难解决方法(0)

chrome.scripting.executeScript:“函数未定义”错误

我正在编写一个 Chrome 扩展程序,并且有以下页面:

<html>
  <body>
    <button id="changeColor"></button>
    <script src="popup.js"></script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

使用这个 JS (popup.js):

let changeColor = document.getElementById("changeColor");

chrome.storage.sync.get("color", ({ color }) => {
  changeColor.style.backgroundColor = color;
});

changeColor.addEventListener("click", async () => {
    let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
  
    chrome.scripting.executeScript({
      target: { tabId: tab.id },
      function: setPageBackgroundColor,
    });

});
  
function setPageBackgroundColor() {
  chrome.storage.sync.get("color", ({ color }) => {
    document.body.style.backgroundColor = color;
  });
  // Here, it says: Uncaught ReferenceError: getElementByXpath is not defined
  console.log(getElementByXpath("xpath").textContent);
}
  
function getElementByXpath(path) …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome-extension

2
推荐指数
1
解决办法
1725
查看次数