Google chrome extension 将函数作为内容脚本注入

Google chrome extension 将函数作为内容脚本注入,google-chrome-extension,Google Chrome Extension,我正在使用 当以编程方式注入时,我可以作为 chrome.tabs.executeScript(null, { code: "alert('hello world');"}); 或 我可以传入一个字符串或文件来执行。是否有方法注入函数 差不多 chrome.tabs.executeScript(null, {code: function1}); function function1() { alert("hi");} 我认为您不能将后台页面中定义的函数注入内容脚本。但是,您可以获取函数源

我正在使用

当以编程方式注入时,我可以作为

chrome.tabs.executeScript(null, { code:  "alert('hello world');"});

我可以传入一个字符串或文件来执行。是否有方法注入函数

差不多

chrome.tabs.executeScript(null, {code: function1});

function function1() { alert("hi");}

我认为您不能将后台页面中定义的函数注入内容脚本。但是,您可以获取函数源代码并立即调用它

function hello() { alert("hi"); }

chrome.tabs.executeScript(null, { code: "(" + hello.toString() + ")()" });

将函数定义嵌入代码段本身如何

chrome.tabs.executeScript(null, {
     code: "function function1() { alert('hi'))};
            function1();"
 });

其思想是在后台页面中定义多个函数,只注入所需的函数。这与注入一个声明了所有函数的文件是一样的。
chrome.tabs.executeScript(null, {
     code: "function function1() { alert('hi'))};
            function1();"
 });