gpt4 book ai didi

javascript - Chrome 扩展程序 :Communicate between content script and webpage's script

转载 作者:行者123 更新时间:2023-12-03 07:19:20 24 4
gpt4 key购买 nike

我的内容脚本中有以下代码,它调用网页的 javascript 中的函数

var actualCode = '(' + function() {
functionInMyWebPage();
}
+ ')();';
var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.parentNode.removeChild(script);

这是有效的,它会触发 functionInMyWebPage() 函数。

现在我需要获取此 functionInMyWebPage() 的返回值并调用内容脚本本身中定义的另一个函数。如果我在上面的脚本 block 中调用它,它会提示,因为它尚未定义。

最佳答案

Content scripts execute in a special environment called an isolated world. They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page. It looks to each content script as if there is no other JavaScript executing on the page it is running on. The same is true in reverse: JavaScript running on the page cannot call any functions or access any variables defined by content scripts.

更多信息Content script execution environment

关于javascript - Chrome 扩展程序 :Communicate between content script and webpage's script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36276159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com