Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

javascript - Passing a JSON object from the background script to the content script in a chrome extension

I have a JS array in the "background.js" file of my Chrome extension. I converted the JS object to a JSON string to pass it into the "content.js" file but am unable to do so. Can I be helped with the code to send the JSON object from the "background.js" file and the code to receive the same from the "content.js" file.

// 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...