javascript - 将 JSON 对象从后台脚本传递到 chrome 扩展中的内容脚本
问题描述
我的 Chrome 扩展程序的“background.js”文件中有一个 JS 数组。我将 JS 对象转换为 JSON 字符串以将其传递到“content.js”文件中,但无法这样做。我可以帮助我使用从“background.js”文件发送 JSON 对象的代码和从“content.js”文件接收相同对象的代码吗?
// This is the important portion of the code present in the background.js file.
console.log("Inside the buttonClicked function");
var titles = [];
var hrefs = [];
var links = document.getElementsByClassName('uqZtlf x0HGk QRiHXd MymH0d maXJsd');
for (var i = 0; i < links.length; i++)
{
hrefs.push(links[i].href); // capturing urls
titles.push(links[i].title); // capturing titles
}
jsonObjectLinks = JSON.stringify(hrefs); // converting the js object to the JSON string
console.log(jsonObjectLinks);
var jsonObjectTitles = JSON.stringify(titles);
// I now wish to pass on the jsonObjectTitles to the content.js file
解决方案
推荐阅读
- firebase - Firebase 实时数据库中的存储大小是如何计算的?
- excel - 发布 RunTime 91 数据输入
- .net - 使用 xunit 使用 .Net Framework 4.7.2 在 jenkins 中生成 .TRX 文件
- selenium - 在 Intellij 中离线配置 Selenium
- c# - 使用 .StartsWith 和正则表达式过滤列表?
- c# - 删除文本窗口窗体上的白色轮廓
- python - Python写入功能未保存所有图像
- sql-server - 努力在动态查询中使用 IN 运算符和列表参数。Jaspersoft 工作室
- javascript - 如何在运行时配置 MsAdalAngular6Module?
- python - 使用令牌对 Kubernetes 集群进行身份验证