javascript - 如何从 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;
解决方案
推荐阅读
- regex - 从列 csv 正则表达式中删除分隔符
- python - 没有名为“sklearn.externals.six”的模块
- c++ - 通用“sqr”函数
- android - 具有Unique字段的RoomDatabse,当值相同时如何处理?
- ruby-on-rails - rails has_many :通过关系查询不返回任何内容,尽管记录保存在数据库中
- python - Django:如何更改嵌套序列化程序中的字段名称
- c# - 谁能帮助我统一理解我的二维运动脚本?
- ios - 如何将值传递给正在加载反应本机应用程序的 Web 视图
- shell - 如何在 ubuntu 上的 bash 中运行 stdout 的输出
- r - ggplot2 仅在显示值的地方绘制轴值