Answer the question
In order to leave comments, you need to log in
How to pass data from background to content script in chrome extension when navigating to a new page?
I'm writing a chrome extension. We need to catch the url change on the current tab and send some data from the background script to the content script For a simple example, I took the HSE website.
background.js:
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.url == "https://www.hse.ru/" && changeInfo.status == "complete") {
var sendObj = {
"myMessage": "this is main page"
};
chrome.tabs.sendMessage(tabId, sendObj);
} else if (changeInfo.url == "https://www.hse.ru/distant" && changeInfo.status == "complete") {
var sendObj = {
"myMessage": "this is some side page"
};
chrome.tabs.sendMessage(tabId, sendObj);
}
});
chrome.runtime.onMessage.addListener(function(data) {
console.log(data.myMessage);
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question