首页 > 解决方案 > 如何从 background.js 调用打开并动态创建新选项卡的内容脚本

问题描述

我正在制作一个 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;


标签: javascripthtmlapigoogle-chromegoogle-chrome-extension

解决方案


推荐阅读