i am working on chrome extension. my project have index.html, index.js and background.js.
"content_scripts": [ {"js": [ "index.html" ], ],
"background": { "scripts":["background.js"], }
when index.js call window.open(url), backgrund.js catch new url like below
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
alert(tab.url);
}});
so i want to pass tab.url to index.js's function(whatever) is it possible ? how can i call it ?
if you know how-to or reference-page
plz Answer, have a nice day
To do what you want, tou'll need to use message passing between your extension components.
First of all, you'll need to add the tabs
permission to your manifest.json
. I also spotted some errors in your manifest.json
, check it out, what you want is something like this:
{
"manifest_version": 2,
"name": "Extension name",
"description": "Your description...",
"version": "1",
"permissions": [
"<all_urls>",
"tabs"
],
"background": { "scripts": ["background.js"] }
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content_script.js"]
}
]
}
Then, in your background.js
, you'll send a message to your content_script.js
, like this:
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
chrome.tabs.sendMessage(tabId, {message: "do something", url: tab.url});
}
});
In your content_script.js
you'll listen to that message and behave consequently:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.message === "do something") {
alert('Got the url:', request.url); // here is the url you sent from background.js
// do what you want
}
});
Here is the documentation link for further information.