I'm tring to get current tab id in content script. But it fails all the time. I'm not sure, what i'm doing false.
Here are some solutions from another topics, but these are not working in my extension:
CODE 1 - content.js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
alert("sent from tab.id=", sender.tab.id);
});
CODE 2 - content.js
chrome.extension.sendRequest({
action: "WhatYouWant"
});
chrome.extension.onRequest.addListener(function (request, sender, sendResponse) {
if (request.action) {
alert('The response is : ' + request.action);
}
});
background.js
chrome.extension.onRequest.addListener(function (request, sender, sendResponse) {
if (request.action) {
// Make what you want
chrome.tabs.getSelected(null, function (tabs) {
chrome.tabs.sendRequest(tabs.id, {
action: "response"
});
});
}
});
manifest.json
...
"background": {
"scripts": ["background.js"],
"persistent": true
},
"content_scripts": [{
"all_frames": true,
"js": ["content.js"],
"matches": ["<all_urls>"],
"run_at": "document_end"
}],
"web_accessible_resources": [
"content.js"
],
...
Note: This is not duplicate topic, the solutions in other questions don't work for me.