我正在编写一个 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)