How to get Chrome tab id?

chrome.tabs.get does not seem to work in the script content and does not send it through chrome.extension.sendRequest . I do not know how to make the background respond to the correct tab.

How can I make a content script send information to the wallpaper, then the man page will return the information to the tab, where did it come from?

+8
google-chrome-extension
source share
4 answers

EDIT : This answer is deprecated and uses deprecated features. Use other answers instead.

Ok, let me explain you dude;)

First of all, send a message from your content script as follows:

Script Content - Posting a Message

 chrome.extension.sendRequest({ action: "WhatYouWant"}); 

Background page - receive message and reply

 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" }); }); } }); 

ContentScript - adding a listener

 chrome.extension.onRequest.addListener(function(request, sender, sendResponse){ if(request.action) { alert('The response is : ' + request.action); } }); 
+8
source share

onMessage provides a sendResponse function that can be used to send a response directly back to the sender.

background.js excerpt:

 chrome.extension.onMessage.addListener( function(message, sender, sendResponse) { if ( message.type == 'getTabId' ) { sendResponse({ tabId: sender.tab.id }); } } ); 

content_scripts.js excerpt:

 var tabId; chrome.extension.sendMessage({ type: 'getTabId' }, function(res) { tabId = res.tabId; }); 
+4
source share

As a courtesy for anyone looking at this question after 2011, I gave an answer that uses the new conventions and APIs. This is a fairly popular question, and the accepted answer is somewhat dated at this stage (but nevertheless an exact answer, nonetheless), so for any beginners this, I hope, will give a clearer and more applicable answer to the question.

To get started, you can send a message from the contents of the script using chrome.runtime.sendMessage . This will randomly send a message when an execution event occurs (i.e. the browser action is clicked). It will look something like this:

content.js

 chrome.runtime.sendMessage({ from: 'content', message: 'info to send' }); 

Since you need to link to the event page (in accordance with newer conventions, the event page is the preferred choice on a constant background page) in order to capture information about the active tab (provided that you only need a tab identifier), you can actually capture this information by adding an event listener at runtime and checking the sender parameter. It will look something like this:

event.js

 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { if (request.from == 'content') { // tabId will hold the sender tab id value var tabId = sender.tab.id; sendResponse({ from: 'event', message: 'any info to send back', tabId: tabId }); }); 

However, since we need to return this information to the content script, we need to go back and add a callback handler to chrome. runtime.sendMessage . Now it will look something like this:

content.js

 chrome.runtime.sendMessage({ from: 'content', message: 'info to send' }, function(callbackResponse) { if (callbackResponse.from == 'event') { // do what you need to do in here console.log(callbackResponse.tabId); } }); 

Now this setting will allow you to send simple one-time messages from content.js to event.js , where event.js returns content.js information.

Again, this is just an updated version of the accepted answer for those who would like to show how the new approach works.

+3
source share

< Get id tab > full example β–Ό

(Truth: the clearest full definitions: examples have.)

<manifest .json >

 {"manifest_version":2, "name":"My Cool Extension", "version":"0.1", "content_scripts":[{"matches":["<all_urls>"], "js":["content.js"] } ], "background":{"scripts":["bg.js"]} } 

<B.G. .js >

 chrome.runtime.onMessage.addListener((msg, sender_info, Reply)=> { Reply(sender_info); }); 

<content .js >

 chrome.runtime.sendMessage('', (r)=> { console.log('content.js', 'r.tab.id', r.tab.id); }); 

Update the extension, go to the tab, refresh the page, wait, check <content .js > oks, wait, check cb oks, wait, check <console.log> oks, for example:

enter image description here



Check β–Ό

Full patch:

<B.G. .js >

 console.log('bg.js, start'); chrome.runtime.onMessage.addListener((msg, sender_info, Reply)=> { console.log('bg.js, start, cb chrome.runtime.onMessage.addListener'); console.log('bg.js', 'sender_info', sender_info); console.log('bg.js', 'sender_info.id', sender_info.id); console.log('bg.js', 'sender_info.url', sender_info.url); console.log('bg.js', 'sender_info.frameId', sender_info.frameId); console.log('bg.js', 'sender_info.tlsChannelId', sender_info.tlsChannelId); console.log('bg.js', 'sender_info.tab', sender_info.tab); console.log('bg.js', 'sender_info.tab.url', sender_info.tab.url); console.log('bg.js', 'sender_info.tab.favIconUrl', sender_info.tab.favIconUrl); console.log('bg.js', 'sender_info.tab.title', sender_info.tab.title); console.log('bg.js', 'sender_info.tab.incognito', sender_info.tab.incognito); console.log('bg.js', 'sender_info.tab.status', sender_info.tab.status); console.log('bg.js', 'sender_info.tab.width', sender_info.tab.width); console.log('bg.js', 'sender_info.tab.height', sender_info.tab.height); console.log('bg.js', 'sender_info.tab.id', sender_info.tab.id); console.log('bg.js', 'sender_info.tab.windowId', sender_info.tab.windowId); console.log('bg.js', 'sender_info.tab.sessionId', sender_info.tab.sessionId); console.log('bg.js', 'sender_info.tab.openerTabId', sender_info.tab.openerTabId); console.log('bg.js', 'sender_info.tab.pinned', sender_info.tab.pinned); console.log('bg.js', 'sender_info.tab.audible', sender_info.tab.audible); console.log('bg.js', 'sender_info.tab.mutedInfo', sender_info.tab.mutedInfo); console.log('bg.js', 'sender_info.tab.mutedInfo.muted', sender_info.tab.mutedInfo.muted); console.log('bg.js', 'sender_info.tab.mutedInfo.extensionId', sender_info.tab.mutedInfo.extensionId); console.log('bg.js', 'sender_info.tab.mutedInfo.reason', sender_info.tab.mutedInfo.reason); console.log('bg.js', 'sender_info.tab.highlighted', sender_info.tab.highlighted); console.log('bg.js', 'sender_info.tab.active', sender_info.tab.active); console.log('bg.js', 'sender_info.tab.discarded', sender_info.tab.discarded); console.log('bg.js', 'sender_info.tab.autoDiscardable', sender_info.tab.autoDiscardable); Reply(sender_info); console.log('bg.js, end, cb chrome.runtime.onMessage.addListener'); }); console.log('bg.js, end'); 

<content .js >

 console.log('content.js, start'); chrome.runtime.sendMessage('', (r)=> { console.log('content.js, start, cb chrome.runtime.sendMessage'); console.log('content.js', 'r', r); console.log('content.js', 'r.id', r.id); console.log('content.js', 'r.url', r.url); console.log('content.js', 'r.frameId', r.frameId); console.log('content.js', 'r.tlsChannelId', r.tlsChannelId); console.log('content.js', 'r.tab', r.tab); console.log('content.js', 'r.tab.url', r.tab.url); console.log('content.js', 'r.tab.favIconUrl', r.tab.favIconUrl); console.log('content.js', 'r.tab.title', r.tab.title); console.log('content.js', 'r.tab.incognito', r.tab.incognito); console.log('content.js', 'r.tab.status', r.tab.status); console.log('content.js', 'r.tab.width', r.tab.width); console.log('content.js', 'r.tab.height', r.tab.height); console.log('content.js', 'r.tab.id', r.tab.id); console.log('content.js', 'r.tab.windowId', r.tab.windowId); console.log('content.js', 'r.tab.sessionId', r.tab.sessionId); console.log('content.js', 'r.tab.openerTabId', r.tab.openerTabId); console.log('content.js', 'r.tab.pinned', r.tab.pinned); console.log('content.js', 'r.tab.audible', r.tab.audible); console.log('content.js', 'r.tab.mutedInfo', r.tab.mutedInfo); console.log('content.js', 'r.tab.mutedInfo.muted', r.tab.mutedInfo.muted); console.log('content.js', 'r.tab.mutedInfo.extensionId', r.tab.mutedInfo.extensionId); console.log('content.js', 'r.tab.mutedInfo.reason', r.tab.mutedInfo.reason); console.log('content.js', 'r.tab.highlighted', r.tab.highlighted); console.log('content.js', 'r.tab.active', r.tab.active); console.log('content.js', 'r.tab.discarded', r.tab.discarded); console.log('content.js', 'r.tab.autoDiscardable', r.tab.autoDiscardable); console.log('content.js, end, cb chrome.runtime.sendMessage'); }); console.log('content.js, end'); 
+1
source share

All Articles