簡體   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