所以我有一个网页,里面有一个功能鼓掌的网页。当我从控制台调用它时,我得到了正常的返回:
applaud(3004,1935);
undefined但是,如果我使用CTG插件(运行js脚本的简单插件),则使用该代码
applaud(3004,1935);我在控制台中得到以下错误:
3VM5444:1 Uncaught ReferenceError: applaud is not defined
at <anonymous>:1:1
(anonymous) @ VM5444:1功能不起作用。
你知道我怎么用吗?
谢谢。
发布于 2019-02-01 00:11:00
我知道这有点过时,但我可以回答。(我作了有关的延期。)
默认情况下,Chrome扩展将脚本插入到与页面其他内容不同的网页中。这是出于安全原因。如果您希望在网页上下文中运行代码,则需要使用一些变通方法。
在Chrome扩展注入的脚本中,将一个<script>标记注入页面正文。然后,该脚本将被加载,并能够像在控制台中一样执行函数。
下面是一个代码演示,它可以完成我所说的内容:
//Create a new script element.
var script = document.createElement("script");
//Get the function you want to inject as a string and add it to the script.
script.innerHTML = injection.toString();
//Add a call to that injection function so it'll automatically execute once it's injected.
script.innerHTML += "injection();";
//Inject that newly created script into the body of the page.
document.body.appendChild(script);
//The contents of this script will be run inside the same context as the webpage.
function injection(){
applaud(3004, 1935);
}https://stackoverflow.com/questions/49288085
复制相似问题