admin管理员组文章数量:1415467
I am writing a Chrome Extension and I have this page:
<html>
<body>
<button id="changeColor"></button>
<script src="popup.js"></script>
</body>
</html>
With this 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) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
Why?
I am writing a Chrome Extension and I have this page:
<html>
<body>
<button id="changeColor"></button>
<script src="popup.js"></script>
</body>
</html>
With this 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) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
Why?
Share Improve this question edited Dec 27, 2024 at 11:30 woxxom 74k14 gold badges156 silver badges160 bronze badges asked May 14, 2021 at 5:16 Joe AlmoreJoe Almore 4,32910 gold badges55 silver badges82 bronze badges1 Answer
Reset to default 9Solution: put all necessary code and functions inside the function you inject.
In your case getElementByXpath definition should be moved inside setPageBackgroundColor.
The reason is that executeScript injects the function as TEXT:
- it takes the function's code as plain text IIFE i.e.
(function foo() { ... })()
, - it transfers the text to the web page,
- it selects the
ISOLATED
world environment where all content scripts of your extension run by default or the unsafeMAIN
world where all page scripts run if you specified it explicitly, - it executes that text as JavaScript code.
The injected function can also use the existing global variables/functions created in the same world
by files and functions injected via executeScript/registerContentScript or manifest.json's content_scripts
that already ran in this page.
版权声明:本文标题:javascript - chrome.scripting.executeScript injected function can't call an outside function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745169005a2645856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论