繁体   English   中英

JS功能在chrome控制台上有效,但从插件加载时无效

[英]JS function works on chrome console but not when load from plugin

所以我有一个功能鼓掌的网页。 当我从控制台调用它时,我得到正常的回报:

applaud(3004,1935);
undefined

但是,如果我使用CTG插件(运行js脚本的简单插件),则使用该代码

applaud(3004,1935);

我在控制台中收到以下错误:

3VM5444:1 Uncaught ReferenceError: applaud is not defined
    at <anonymous>:1:1
(anonymous) @ VM5444:1

并且功能不起作用。

你知道我怎么用吗?

谢谢。

我知道这有点过时了,但是我可以回答。 (我进行了扩展)。

默认情况下,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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM