Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从background.js调用打开并动态创建新选项卡的内容脚本_Javascript_Html_Api_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 如何从background.js调用打开并动态创建新选项卡的内容脚本

Javascript 如何从background.js调用打开并动态创建新选项卡的内容脚本,javascript,html,api,google-chrome,google-chrome-extension,Javascript,Html,Api,Google Chrome,Google Chrome Extension,我正在制作一个google chrome扩展,它显示特定情况下的通知,当单击通知时,我需要使用一个html文件打开一个新选项卡,该文件需要在api调用后进行编辑。我尝试这样做的方式是在background.js中注入一个内容脚本,但我无法让它工作。有更好/更简单的方法吗 Background.js是代码的一部分 //chrome.sendMessage() var myWindow=window.open ("index.html","mywindow"); var activeTabId; c

我正在制作一个google chrome扩展,它显示特定情况下的通知,当单击通知时,我需要使用一个html文件打开一个新选项卡,该文件需要在api调用后进行编辑。我尝试这样做的方式是在background.js中注入一个内容脚本,但我无法让它工作。有更好/更简单的方法吗

Background.js是代码的一部分

//chrome.sendMessage()
var myWindow=window.open ("index.html","mywindow");
var activeTabId;
chrome.tabs.getCurrent( function(tab){ activeTabId = tab.id } );
chrome.tabs.executeScript(activeTabId, {"file": "contentScript.js"});

(contentScript.js code)

var myWindow=window.open ("index.html","mywindow");
var cards=myWindow.document.getElementById('cards');
console.log(cards);
let new_card = '<div class="col-5 col-md-3 col-lg-2 p-0 mr-2 col"> <div class="card">  <a 
class="text-center" href=""> <div class=" f-16 text-dark mt-4 card-title"> <strong class="text- 
capitalize">NEW CARD</strong> </div> </a>  </div> </div> ';
cards.innerHTML=new_card;


//chrome.sendMessage()
var myWindow=window.open(“index.html”、“myWindow”);
活性表位变量;
getCurrent(函数(tab){activeTabId=tab.id});
executeScript(activeTabId,{“文件”:“contentScript.js”});
(contentScript.js代码)
var myWindow=window.open(“index.html”、“myWindow”);
var cards=myWindow.document.getElementById('cards');
控制台.日志(卡片);
让新卡=“”;
cards.innerHTML=新卡片;

您的index.html无法运行内容脚本,因为它是您自己的扩展页面,所以只需使用index.js之类的普通脚本,并在index.html中将其作为
加载即可。然后只需将数据传递到新窗口即可。我很确定我尝试了类似的操作,但没有成功,但我会再试一次,并给出答复。谢谢。您的index.html无法运行内容脚本,因为它是您自己的扩展页,所以只需使用像index.js这样的普通脚本,并将其作为
加载到index.html中即可。然后,只需将数据传递到新窗口,我很确定我尝试了类似的方法,但没有成功,但我会再试一次,然后回复。谢谢。