chrome.scripting.executeScript: "function is not defined" error chrome.scripting.executeScript: "function is not defined" 错误

11 浏览
0 Comments

chrome.scripting.executeScript: "function is not defined" error chrome.scripting.executeScript: "function is not defined" 错误

我正在编写一个Chrome扩展程序,其中包含以下页面:


  
    
    
  

使用以下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;
  });
  // 这里报错:Uncaught ReferenceError: getElementByXpath is not defined
  console.log(getElementByXpath("xpath").textContent);
}
function getElementByXpath(path) {
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

为什么会报错?

0