javascript - 如何在 chrome.tabs.executeScript 中执行函数
问题描述
我有这个代码:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
chrome.tabs.executeScript(
tabs[0].id,
{
code: // HERE I want much more space to write
'var to_filter = document.querySelector("iframe").contentDocument.querySelector("body").querySelectorAll("tbody")[1].innerHTML; alert(to_filter);'
},
downloadFiles
);
});
});
function downloadFiles(resultsArray){
}
我必须在标签内执行很多代码code
,然后,我必须向函数发送一个links
列表downloadFiles
。问题是我无法在该code
领域编写所有代码,我想改用一个函数。我怎样才能调用一个函数来执行而不是只写在那一行?
- - - 更新
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
chrome.tabs.executeScript(
tabs[0].id,
{
code:'(${ inContent })()' // ERROR
//'var to_filter = document.querySelector("iframe").contentDocument.querySelector("body").querySelectorAll("tbody")[1].innerHTML; alert(to_filter);'
},
downloadFiles
);
function inContent() {
alert("test")
}
});
});
我得到:VM138:1 Uncaught SyntaxError: Unexpected token '{'
在上面的注释行中。
解决方案
推荐阅读
- ruby-on-rails - 如何验证json中的唯一性?
- asp.net-core - GraphQL DotNet 遇到错误请求错误
- r - 字符串在R中将所有列拆分在一起
- android - 无法解决:support-fragment 28.0.0
- spring - 服务有时会以 localhost 的身份注册到 eureka。尤里卡复制
- scala - SparkContext 无法以 master 设置为“Yarn”启动
- python - Scrapy shell 工作正常,但在爬虫上显示 404
- python - Pandas 添加列比较结果
- .net - 使用.net mvc 4在引导程序4中没有完全弹出模态
- r - 闪亮的传单添加大量分离的折线