简体   繁体   English

从Chrome扩展程序注入后运行脚本

[英]Running Script after Injecting from Chrome Extension

I'm trying to inject a script into the webpage when a button in the extension option menu is clicked. 单击扩展选项菜单中的按钮时,我试图将脚本注入网页。 Injection - 注射-

document.getElementById("button_").addEventListener("click", function(){
  alert("Injecting") ;
  chrome.tabs.executeScript({
          file: 'script.js'
        });
});

However the function that I'm trying to run in script.js , foo() is not running. 但是我试图在script.js中运行的函数foo()没有运行。 I'm trying to run it via- 我正在尝试通过-

window.onload= function(){
  foo() ;
}

However this does not work . 但是,这不起作用。 How do I get foo() to run? 我如何让foo()运行?

Note- It has to run as an injected script 注意-它必须作为注入脚本运行

可以使用清单文件中的“ content_scripts”属性,而不是使用侦听器并在单击时运行“ chrome.tabs.executeScript”,并在脚本内部创建一个将运行您的函数的侦听器。

将消息从后台脚本发送到内容脚本,然后再发送到注入脚本,这为处理内容脚本以及与它们进行通信提供了很好的指导。

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

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